【问题标题】:label.Text = ComboBox.SelectedText(); always null?label.Text = ComboBox.SelectedText();总是为空?
【发布时间】:2018-06-21 23:21:44
【问题描述】:

我正在制作一个应用程序,该应用程序在从 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 以及为什么单击重置时组合框不再重置为空白的任何想法?

感谢您的帮助,

-亚瑟

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

【问题讨论】:

  • 要清除组合框,只需使用:comboBox1.Items.Clear(); 并在您的 if 语句中,而不是使用 else if,只需使用 else
  • 如果使用 SelectedValue 设置标签文本,结果如​​何?
  • Items.Clear() 将使它必须重新初始化其中的值,程序的最终目标是根据输入进行选择,然后为另一个输入重置跨度>

标签: c# combobox


【解决方案1】:

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

尝试使用SelectedItem 属性。

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

【讨论】:

  • 这修复了标签更新,谢谢!但是 ResetApp() 仍然没有将组合框恢复为空状态
  • @ArthurAznive 在您的 ResetApp() 方法中使用 comboBox1.Items.Clear();
猜你喜欢
  • 2013-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-28
  • 1970-01-01
  • 2017-06-27
相关资源
最近更新 更多