如何在Visual Studio Windows窗体C#中从组合框中选择选项后,在文本框中显示文本

时间:2016-04-09 20:02:24

标签: c# windows forms visual-studio combobox

我正在制作一个Windows窗体,其中我已经阅读了一些文本文件,我有一个组合框,我想要的是当我选择选项"游戏&#34 ;在组合框中,richtextbox2用读取文件中的文本填充" game.txt"。 对于其他选项和文本文件也是如此。我已尝试过这个if语句,但是我收到一条错误声明"我无法将字符串转换为bool"。

     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
                {

                }

                private void richTextBox2_TextChanged(object sender, EventArgs e)
                {

                }

                private void Form1_Load(object sender, EventArgs e)
                {
                    //Fill combobox with names. 
                    comboBox1.Items.Add("Games");
                    comboBox1.Items.Add("Operating");
                    comboBox1.Items.Add("Information");
                        try
                    {
                   //read text from text files.
                        string game = File.ReadAllText(@"game.txt");
                        string operate = File.ReadAllText(@"operate.txt");
                        string information = File.ReadAllText(@"info.txt");

//if you select Games option in combobox, fill text box with text from read file "game.txt".
    if (comboBox1.Text = "Games")
                    {
                        richTextBox2.Text = game;
                    }
                    }
                    catch (Exception ex)
                    {
                        //display error if files not found. 
                        MessageBox.Show(" " + ex.Message); 
                    }

2 个答案:

答案 0 :(得分:0)

在C#中检查一个值是否等于另一个值,您需要使用==运算符。 因此,您的if语句应如下所示:

if (comboBox1.Text == "Games")

修改 以上将使您的代码编译。为了使您的程序按预期工作,您需要将try/catch块移动到SelectedChangedIndex的{​​{1}},它可能如下所示:

ComboBox

答案 1 :(得分:0)

    //Fill the default comboBox item.
    private void Form1_Load(object sender, EventArgs e)
    {
        comboBox1.Items.Add("Games");
        comboBox1.Items.Add("Operating");
        comboBox1.Items.Add("Information");
    }

    public void Write(string category)
    {

        switch (category)
        {
            //if you select Games option in combobox, do work Games() method.
            case "Games": Games(); break;
            case "Operating": Operating(); break;
            case "Information": Information(); break;
            default:
                break;
        }
    }

    //if you select Games , read file "game.txt" with together Read() method.
    public void Games()
    {
        richTextBox1.Text = Read("games");
    }

    //if you select Operating , read file "operating.txt" with together Read() method.
    public void Operating()
    {
        richTextBox1.Text = Read("operating");
    }

    //if you select Information , read file "information.txt" with together Read() method.
    public void Information()
    {
        richTextBox1.Text = Read("information");
    }

    //File reading process
    public string Read(string fileName)
    {
        string data = File.ReadAllText(Application.StartupPath + @"\" + fileName + ".txt");
        return data;
    }

    //Selected item process
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Write(comboBox1.SelectedItem.ToString());
    }