C# - 如何将文本文件中的文本加载到listbox中并显示在richTextBox中?

3
现在问题已经解决。感谢您的答案!
这是我现在的代码:
  //Listbox scripts is the name of my folder
    private void Form1_Load(object sender, EventArgs e)
    {
        foreach (var file in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + @"Listbox scripts"))
        {
            string file2 = file.Split('\\').Last();
            listBox1.Items.Add(file2);
        }
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        webBrowser1.Document.InvokeScript("SetText", new object[]
        {
            File.ReadAllText(string.Format("./Listbox scripts/{0}", listBox1.SelectedItem.ToString()))
        });
    }

我刚开始学习 C# 编程,我有一个文本框,其中包含一个目录中文本文件的名称。当我在列表框中单击文本文件时,它应该将文本加载到我的文本框中(名为“ScriptBox”)。
以下是我的代码:
    private void Form1_Load(object sender, EventArgs e)
    {
            string User = System.Environment.MachineName;
        textBox1.Text = "{CONSOLE} Welcome to Linst, " + User + "!";
        directory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + @"Scripts");
        files = directory.GetFiles("*.txt");
        foreach (FileInfo file in files)
        {
            listBox1.Items.Add(file.Name);
        }
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        var selectedFile = files[listBox1.SelectedIndex];

        ScriptBox.Text = File.ReadAllText(selectedFile.FullName); //these parts are the parts that dont work
    }

提前致谢!


1
这个问题具体是怎么出错的?有错误吗?还是输出结果不符合预期? - David
在您的SelectedIndexChanged()处理程序中添加一个断点,并确保它实际上被触发。如果没有,那么您需要连接该事件。此外,“ScriptBox”是从工具箱拖到表单中的吗?还是在代码中声明的? - Idle_Mind
3个回答

2
将以下内容添加到您的Form1.cs中。当用户单击列表框项时,它将调用(触发事件)“listBox1_MouseClick”方法并将文本框的文本设置为列表框项的文本。我快速创建了一个应用程序并实现了以下内容,它可以正常工作。"最初的回答"
private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
    textBox1.Text = listBox1.Text;
}

在Form1.Designer.cs中添加以下内容,放在其余的列表框属性下面。以下是订阅事件的代码,即在Form1.cs中的listBox1_MouseClick方法,因此当用户单击列表框项时,listBox1_MouseClick方法将运行。

Original Answer翻译成"最初的回答"

 this.listBox1.MouseClick += new MouseEventHandler(this.listBox1_MouseClick);

我希望你能够理解上述内容。
最初的回答

1

你的代码很好,几乎完美,但在列表索引选择中只需要添加一些验证检查。尝试在listbox_selectedIndexChanged中进行。

 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listBox1.SelectedIndex!=-1)
        {
            FileInfo selectedFile = files[listBox1.SelectedIndex];
            ScriptBox.Text = File.ReadAllText(selectedFile.FullName);
        }
    }

1

我实际上没有看到你的代码有任何问题,可能是某个地方打错字了吗?我按照以下方式做,它对我起作用:

private void Form1_Load(object sender, EventArgs e)
{
    foreach (var file in System.IO.Directory.GetFiles(@"c:\"))
    {
        listBox1.Items.Add(file);
    }
}


private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (listBox1.SelectedItem != null)
    {
        textBox1.Text = System.IO.File.ReadAllText(listBox1.SelectedItem.ToString());
    }
}

1
我知道这个答案有点老了,很抱歉没有及时回复,但是你的代码确实帮了我很大的忙。 - LukeD524

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接