我正在制作一个应用程序,该应用程序在下拉菜单中具有一些选项,这些菜单从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以及为什么在单击重置时为何将ComboBox不再重置为空白的任何想法?
谢谢您的帮助,
-亚瑟
编辑*我将使用Items.Clear();然后只需在reset函数中调用InitializeDropDownMenu()即可。应该适合我的预期用途。谢谢大家。
答案 0 :(得分:0)
我认为问题出在使用SelectedText
。 SelectedItem
属性“获取或设置在System.Windows.Forms.ComboBox的可编辑部分中选择的文本”。
请尝试使用label1.Text = comboBox1.SelectedItem.ToString();
属性。
{{1}}