首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >label.Text = ComboBox.SelectedText();总是null?

label.Text = ComboBox.SelectedText();总是null?
EN

Stack Overflow用户
提问于 2018-06-21 14:03:22
回答 1查看 192关注 0票数 0

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

代码语言:javascript
复制
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,以及为什么单击重置时,ComboBoxes不再被重置为空白,有什么想法吗?

谢谢你的帮助,

-Arthur

编辑*我将使用Items.Clear();然后在重置函数中调用InitializeDropDownMenu()。应该为我的预期用途工作。谢谢你们所有人。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-21 14:18:00

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

相反,尝试使用SelectedItem属性。

代码语言:javascript
复制
label1.Text = comboBox1.SelectedItem.ToString();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50970498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档