Reputation: 1941
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. 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.
The dialog result is set in SelectTransaction
form. In there I have a combobox
and when I select an item, I return the result.
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?
Upvotes: 0
Views: 1165
Reputation: 316
Sometimes the SelectedIndexChanged-Event can fire while you fill the combobox. Instead use the SelectionChangeCommitted-Event http://msdn.microsoft.com/en-us/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
Upvotes: 1