C# label.Text=ComboBox.SelectedText();总是空的?

C# label.Text=ComboBox.SelectedText();总是空的?,c#,combobox,C#,Combobox,我正在制作一个应用程序,它在下拉菜单中有一些选项,这些选项可以从App.Config文件中填充。当程序停止执行重置时,我正在测试重置功能。我的Form1代码如下: public Form1() { InitializeComponent(); InitializeDropDownMenu(); } private void InitializeDropDownMenu() { //Populate all the menus from app.config fo

我正在制作一个应用程序,它在下拉菜单中有一些选项,这些选项可以从App.Config文件中填充。当程序停止执行重置时,我正在测试重置功能。我的Form1代码如下:

public Form1()
{
    InitializeComponent();
    InitializeDropDownMenu();
}

private void InitializeDropDownMenu()
{
    //Populate all the menus from app.config
    foreach (string s in Properties.Settings.Default.Box1Contents)
    {
        comboBox1.Items.Add(s);
    }

    foreach (string s in Properties.Settings.Default.Box2Contents)
    {
        comboBox2.Items.Add(s);
    }

    foreach (string s in Properties.Settings.Default.Box3Contents)
    {
        comboBox3.Items.Add(s);
    }

    //Controls for drop down menus
    this.Controls.Add(comboBox1);
    comboBox1.SelectedIndexChanged +=
        new System.EventHandler(comboBox1_SelectedIndexChanged);

    this.Controls.Add(comboBox2);
    comboBox2.SelectedIndexChanged +=
        new System.EventHandler(comboBox2_SelectedIndexChanged);

    this.Controls.Add(comboBox3);
    comboBox3.SelectedIndexChanged +=
        new System.EventHandler(comboBox3_SelectedIndexChanged);

    //Begin Program with all dDMenus enabled.
    comboBox1.Enabled = true;
    comboBox2.Enabled = true;
    comboBox3.Enabled = true;
}

private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    DialogResult result = MessageBox.Show(
        "Change Viewer to: \r\n" + comboBox1.Text + "\r\n\r\n" + "Confirm?",
        "Menu",
        MessageBoxButtons.YesNo,
        MessageBoxIcon.Information);

    if (result == DialogResult.Yes)
    {
        label3.Text = comboBox1.SelectedText;
    }
    else if( result == DialogResult.No)
    {
        comboBox1.ResetText();
    }
}

private void comboBox2_SelectedIndexChanged(object sender, System.EventArgs e)
{
    DialogResult result = MessageBox.Show(
    "Change Viewer to: \r\n" + comboBox2.Text + "\r\n\r\n" + "Confirm?",
    "Menu",
    MessageBoxButtons.YesNo,
    MessageBoxIcon.Information);

    if (result == DialogResult.Yes)
    {
        label3.Text = comboBox2.SelectedText;
    }
    else if (result == DialogResult.No)
    {
        comboBox2.ResetText();
    }
}

private void comboBox3_SelectedIndexChanged(object sender, System.EventArgs e)
{
    DialogResult result = MessageBox.Show(
    "Change Viewer to: \r\n" + comboBox3.Text + "\r\n\r\n" + "Confirm?",
    "Menu",
    MessageBoxButtons.YesNo,
    MessageBoxIcon.Information);

    if (result == DialogResult.Yes)
    {
        label3.Text = comboBox3.SelectedText;
    }
    else if (result == DialogResult.No)
    {
        comboBox3.ResetText();
    }
}

private void ResetApp()
{
    comboBox1.ResetText();
    comboBox2.ResetText();
    comboBox3.ResetText();
}

private void button1_Click(object sender, EventArgs e)
{
    ResetApp();
    label3.Text = "ResetApp Ran";
}
关于为什么label3总是设置为null,以及为什么单击reset时组合框不再被重置为空白,有什么想法吗

谢谢你的帮助

-亚瑟


编辑*我将使用Items.Clear();然后只需在重置函数中调用InitializeDropDownMenu()。应该适合我的预期用途。谢谢大家。

我想问题出在使用
SelectedText
上。
SelectedText
属性“获取或设置在System.Windows.Forms.ComboBox的可编辑部分中选择的文本”

请尝试使用
SelectedItem
属性

label1.Text = comboBox1.SelectedItem.ToString();

要清除组合框,只需使用:
comboBox1.Items.Clear()
在if语句中,不要使用
else if
,只需使用
else
如果使用SelectedValue设置标签文本?项,结果会是什么。Clear()将使其必须重新初始化其中的值,该程序的最终目标是根据一个输入进行选择,然后为另一个输入重置。该程序修复了标签更新,谢谢!但是ResetApp()仍然不会将组合框恢复为空state@ArthurAznive使用
comboBox1.Items.Clear()ResetApp()
方法中的code>。