1

私のコードに問題があります。私のコードは、テキストファイルから値を読み取る必要があります。しかし、テキストファイルの値をリストボックスに入れると、次のようになります。

コマンドリストボックス

したがって、コマンドまたは値は 1 行で入力されます。コマンドを次のようにしたいので、画像を変更しました。

ここに画像の説明を入力

ご覧のように?コマンドを相互に配置したい。これは、テキストファイルを読み取るための私のコードです:

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私の同僚が作ったローカル関数です。

これはどのようにTextFile見えるかです:

RUN 
RUNW
STOP
RUN
RUN
STOP

助けてくれてありがとう。

4

3 に答える 3

3
CommandListListBox.DataSource = File.ReadAllLines(fileName);
于 2013-10-23T07:18:02.100 に答える
2

_commandListタイプの場合はSystem.Collection.Generic.List<string>、次のスニペットを使用できます。

_commandList.AddRange(System.IO.File.ReadAllLines(fileName));

完全なコード:

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;
        }

    }
}
于 2013-10-23T07:18:32.197 に答える
2

これを試して !

// Open the file to read from. 
 string[] readText = File.ReadAllLines(fileName);
        foreach (string fileText in readText)
        {
            _commandList.Add(fileText);
        }
于 2013-10-23T07:19:53.297 に答える