简体   繁体   English

如何使用StreamReader在C#中从一个WPF窗口向另一个窗口输入文本?

[英]How do I use a StreamReader to input text from one WPF window to another in C#?

My problem is this, I am writing some software in WPF C# and I need to makeit so that the MainWindow will parse the txt file I have made and store the information in a data structure, the data should be passed to the second Window when it is opened. 我的问题是,我正在WPF C#中编写一些软件,我需要makeit,以便MainWindow可以解析我制作的txt文件并将信息存储在数据结构中,数据应在传递给第二个Window时传递给它。打开。 I have the StreamReader code working fine, it can locate the txt file, but it won't show the information in the listbox on the second window (I apologize if I have screwed up the formatting, very new to the website) 我的StreamReader代码可以正常工作,它可以找到txt文件,但是它不会在第二个窗口的列表框中显示信息(如果我搞砸了格式,我感到抱歉,这对网站来说是很新的)

namespace ACW2
{

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void inventoryButton_Click(object sender, RoutedEventArgs e)
    {
        InventoryWindow wnd = new InventoryWindow();
        wnd.ShowDialog();



        string line;                                                                     
        StreamReader file = new StreamReader(@"G:\P1\txt_files\inventory.txt");
        List<int> list = new List<int>();
        while ((line = file.ReadLine()) != null) ;
        {
            ListBox.Items.Add.(Line);
            list.Add(int.Parse(line));
        }
    }

you have few problems here: 您在这里有几个问题:

  • semicolon on while ((line = file.ReadLine()) != null) ; while ((line = file.ReadLine()) != null) ;分号while ((line = file.ReadLine()) != null) ; line which means that next code block (in curly braces) will not be executed in the loop, but after the loop ends. 这意味着下一个代码块(花括号)将不会在循环中执行,而是在循环结束之后执行。
  • What is Line (with capital L) which you are adding to ListBox.Items collection? 您要添加到ListBox.Items集合的Line (大写L)是什么? Where is that Line defined and what is it's value? 那条Line在哪里定义,它的价值是什么?
  • what is line (lowercase L this time)? 什么是line (这次是小写L)?

try to fix those errors and we'll see what's next to be done... 尝试修复这些错误,我们将看到下一步要做的事情...

What do you want to reach with 您想接触什么

ListBox.Items.Add.(Line);

Maybe you mean this: 也许你的意思是这样的:

ListBox.Items.Add.(line);

You also dont need the semicolon at the end of your while statement. 在while语句的末尾,您也不需要分号。

Edit: Specify your ListBox on the second window with a x:Name = "myListBox" tag. 编辑:使用x:Name =“ myListBox”标记在第二个窗口上指定ListBox。 After you did that you should be able to add an element to the listbox with wnd.myListBox.Items.Add(line); 完成之后,您应该可以使用wnd.myListBox.Items.Add(line);向列表框中添加元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在WPF中将对象从一个窗口绑定到另一个窗口中的另一个控件? (C#) - How to bind an object from one window to another control in another window in wpf? (c#) 当我使用 C# 单击 WPF 窗口上的按钮时,如何将文本发送到活动窗口? - How do I send text to an active window when I click a button on a WPF window using C#? wpf c#在一个窗口中放置标签,将另一个窗口中的值 - wpf c# put in label in one window a value from another 将数据从一个 window 发送到另一个 c# xaml wpf - send data from one window to another c# xaml wpf 将变量从一个WPF窗口传递到另一个(C#) - Passing Variable from one WPF Window to Another (C#) 如何将类数组从一个WPF窗口返回到另一个窗口? - How do I return a class array from one WPF window to another window? C#如何在另一个函数中使用一个值? - C# How do I use a value from one function in another? 如何从一个视图访问TextBox中的文本,并在WPF C#中在另一个视图的TextBlock中显示文本 - How to access text from a TextBox from one view and display it in a TextBlock on another view in WPF C# 来自标签的C#StreamReader输入文件? - C# StreamReader input files from labels? 如何在C#中启动另一个应用程序? - How do I launch application one from another in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM