[英]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: 您在这里有几个问题:
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. 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
在哪里定义,它的价值是什么? 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.