从文件加载到列表框?
因此,在我的程序中,我试图做到这一点,以便当您按下按钮时,它会打开一个“选择文件”弹出窗口(用户可以在其中选择一个文本文件),然后在用户选择它之后,程序将自动加载该文件的每一行文本文件放入列表框中。
但我一直在研究它,我唯一能找到的是文件>打开的东西。所以我让它在按下按钮时打开一个“打开”对话框有多冷
,因为我无法在打开的对话框中找到任何内容,所以我没有在将其每一行加载到列表框中时寻找任何内容,所以如果有人想帮助我,那就太好了。
我没有任何代码可以向您展示,因为我的程序的其余部分与此无关
So in my program i am trying to make it so that when you press a button it opens a "choose file" popup(where the user can choose a text file) then after the user chooses it the program will automatically load each line of the textfile into a listbox.
But i have been researching it and the only thing i have been able to find is a file>open thing. So how cold i make it open a "open" dialogue on the press of a button
and since i haven't been able to find anything on the open dialogue i haven't looked for anything on the loading each line of it into a listbox, so if anyone wants ot help me with that it would be great.
and i do not have any code to show you as the rest of my program has no relevance to this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:回答评论:
如果您可以使用 LINQ,那么只需一行代码即可从列表框中读取所有行并将其写入文件:
使用 SaveFileDialog 和 LINQ 保存< /strong>
如果您无法使用 LINQ,那么您可以这样做:
使用 SaveFileDialog 和 FOR/EACH 进行保存
EDIT: Answer on the comment:
If you can use LINQ, then its a one row of code to read all lines from the listbox and write it to a file:
Save using SaveFileDialog and LINQ
If you can't use LINQ, then you can do this instead:
Save using SaveFileDialog and FOR/EACH
基本上,这里有几个部分。首先,您要创建一个“打开文件”对话框,提示用户文件所在位置。操作方法如下:
http://www.homeandlearn.co.uk/net/ nets4p6.html
接下来,您想要将文本文件逐行读取到列表框中。以下是读取文本文件的方式(您需要修改代码以将行添加到列表框中,而不是执行 Console.WriteLine:
http://msdn.microsoft.com/en-us/library/db5x7c0d.aspx
Basically, there are a couple parts here. First, you want to create an Open File Dialog box that prompts the user for where the file is. Here is how you do that:
http://www.homeandlearn.co.uk/net/nets4p6.html
Next, you want to read the text file in line by line into your listbox. Here is how you read your text file (you will need to modify the code to have it add the lines to the listbox instead of doing a Console.WriteLine:
http://msdn.microsoft.com/en-us/library/db5x7c0d.aspx
来自 MSDN 的 OpenFileDialog:
如您所见注释,您可以在用户打开文件后读取流。然后,您可以使用 StreamReader。这将为您提供用户选择的文件中的数据。根据您的需要,您可以解析该数据并将其添加到列表框中。
OpenFileDialog from MSDN:
As you can see from the comments, you can read a stream after the user has opened the file. You can then read this stream using, for example, a StreamReader. That will give you the data within the file the user chose. Depending on what you want, you can then parse that data and add it to your listbox.