[英]Fill Listbox with values from TextFile
i have a issue with my code. 我的代码有问题。 My code has to read out values from a textfiles, which it does.
我的代码必须从文本文件中读取值。 But when i put the values of the textfile in my listbox it comes out like this:
但是,当我将文本文件的值放在列表框中时,结果如下:
So the commands or values are coming in one line. 因此,命令或值合而为一。 I want the commands to go like this, I've changed the picture so you can see:
我希望命令像这样运行,我已经更改了图片,因此您可以看到:
So you see? 所以你看? I want the commands under each other.
我想要命令彼此。 This is my code for reading the textfile:
这是我读取文本文件的代码:
private void CommandFileSelectButton_Click(object sender, EventArgs e)
{
Stream mystream;
OpenFileDialog commandFileDialog = new OpenFileDialog();
if (commandFileDialog.ShowDialog() == DialogResult.OK)
{
if ((mystream = commandFileDialog.OpenFile())!= null)
{
string fileName = commandFileDialog.FileName;
CommandListTextBox.Text = fileName;
string fileText = File.ReadAllText(fileName);
_commandList.Add(fileText);
CommandListListBox.DataSource = _commandList;
}
}
}
_commandList
is an local functions which my co worker has made. _commandList
是我的同事制作的局部函数。
This is how to TextFile
looks: 这是
TextFile
外观:
RUN
RUNW
STOP
RUN
RUN
STOP
Thanks in advance for the help. 在此先感谢您的帮助。
CommandListListBox.DataSource = File.ReadAllLines(fileName);
If _commandList
is of type System.Collection.Generic.List<string>
you can use the following snippet: 如果
_commandList
的类型为System.Collection.Generic.List<string>
,则可以使用以下代码段:
_commandList.AddRange(System.IO.File.ReadAllLines(fileName));
Full code: 完整代码:
private void CommandFileSelectButton_Click(object sender, EventArgs e)
{
Stream mystream;
OpenFileDialog commandFileDialog = new OpenFileDialog();
if (commandFileDialog.ShowDialog() == DialogResult.OK)
{
if ((mystream = commandFileDialog.OpenFile())!= null)
{
string fileName = commandFileDialog.FileName;
CommandListTextBox.Text = fileName;
_commandList.AddRange(System.IO.File.ReadAllLines(fileName));
CommandListListBox.DataSource = _commandList;
}
}
}
Try this ! 尝试这个 !
// Open the file to read from.
string[] readText = File.ReadAllLines(fileName);
foreach (string fileText in readText)
{
_commandList.Add(fileText);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.