[英]ShowDialog() does not wait for input
Maybe this problem is very easy for some, but it's bugging me. 也许对于某些人来说,这个问题很容易,但是却困扰着我。
The behaviour I am looking for is: - when I select an menu item, a dialog appears; 我正在寻找的行为是:-当我选择一个菜单项时,会出现一个对话框; - after the dialog selection is made, a new form is rendered, according to the selection made -进行对话框选择后,根据所做的选择呈现新表单
This is the code: 这是代码:
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();
}
The code work as I want in general. 一般而言,代码工作正常。 But from time to time, mostly when making the selection twice in a row, the ShowDialog()
does not wait for my input. 但是有时,通常在连续两次进行选择时, ShowDialog()
并不等待我的输入。 It goes righ to showing the form. 显示表格很重要。
I tried to create and dispose the selection object myself (instead of using using
) but the same problem occurs. 我尝试自己创建和处理选择对象(而不是使用using
),但是发生了同样的问题。
The dialog result is set in SelectTransaction
form. 对话框结果在SelectTransaction
表单中设置。 In there I have a combobox
and when I select an item, I return the result. 在那里,我有一个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();
}
}
Can someone tell me what I am doing wrong? 有人可以告诉我我在做什么错吗?
Sometimes the SelectedIndexChanged-Event can fire while you fill the combobox. 有时,当您填充组合框时,可能会触发SelectedIndexChanged-Event。 Instead use the SelectionChangeCommitted-Event http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectionchangecommitted.aspx 而是使用SelectionChangeCommitted-Event http://msdn.microsoft.com/zh-cn/library/system.windows.forms.combobox.selectionchangecommitted.aspx
Or another solution: Don't add the event listener in your form, just add it after you populate the combobox (after you finished the For-Loop) 或其他解决方案:不要在表单中添加事件侦听器,只需在填充组合框后(在完成前循环之后)添加事件侦听器
selectTransactionComboBox.SelectedIndexChanged += comboBox1_SelectedIndexChanged
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.