对某些人来说,这个问题可能很容易,但这让我烦恼。
我正在寻找的行为是: - 当我选择菜单项时,会出现一个对话框; - 在进行对话框选择后,根据所做的选择呈现新表单
这是代码:
this.panel1.Controls.Clear();
if (this.childForm != null)
{
this.childForm.Dispose();
this.childForm = null;
}
using (var selectionForm = new SelectTransaction())
{
var result = selectionForm.ShowDialog();
childForm = new TransactionForm(selectionForm.transactionName);
childForm.TopLevel = false;
this.panel1.Controls.Add(childForm);
childForm.Location = new Point(0, 0);
childForm.Show();
}
代码按照我的要求工作。但有时候,大多数情况下连续两次进行选择时,ShowDialog()
不会等待我的输入。它很好地展示了表格。
我尝试自己创建和处理选择对象(而不是使用using
)但是会出现同样的问题。
对话框结果以SelectTransaction
形式设置。在那里我有一个combobox
,当我选择一个项目时,我会返回结果。
public partial class SelectTransaction : Form
{
public string transactionName;
public SelectTransaction()
{
InitializeComponent();
// The data set is retrieving from a database
selectTransactionComboBox.Text = " - SELECT TRANSACTION - ";
selectTransactionComboBox.Items.Clear();
for (int i = 0; i < dataSet.Tables["TransactionInfo"].Rows.Count; i++)
{
selectTransactionComboBox.Items.Add(dataSet.Tables["TransactionInfo"].Rows[i].ItemArray.GetValue(0).ToString());
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.transactionName = selectTransactionComboBox.Items[selectTransactionComboBox.SelectedIndex].ToString();
this.Close();
}
}
有人能告诉我我做错了吗?
答案 0 :(得分:1)
有时,当您填充组合框时,SelectedIndexChanged-Event可能会触发。而是使用SelectionChangeCommitted-Event http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectionchangecommitted.aspx
或另一种解决方案:不要在表单中添加事件监听器,只需在填充组合框后添加它(在完成For循环后)
selectTransactionComboBox.SelectedIndexChanged += comboBox1_SelectedIndexChanged