0

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?

Coral Doe
  • 1,925
  • 3
  • 19
  • 36
  • 1
    To clarify your request: you say that sometimes `selectionForm.ShowDialog()` returns immediately ? – Benoit Blanchon Sep 17 '13 at 11:00
  • 1
    it is difficult to find the bug from this piece of code. But as an alternative, you can add the codes which must be executed after the selectionform gets closed in the closed event of the selectionform before calling showdialog. – Victor Mukherjee Sep 17 '13 at 11:04
  • 1
    @CoralDoe: in that case, it most likely comes from `SelectTransaction` implementation. For instance, where do you call `Close()` or set `DialogResult` ? Also do you have buttons with the `DefaultValue` set to something ? – Benoit Blanchon Sep 17 '13 at 11:09
  • 1
    The `SelectTransaction` contains a `combobox`. At the `indexchanged` event I get the selection and call `Close()`. – Coral Doe Sep 17 '13 at 11:20
  • 1
    @CoralDoe: I think `SelectedIndexChanged` is raised when you programmatically set `SelectedIndex` or `SelectedItem`, do you set one of them ? – Benoit Blanchon Sep 17 '13 at 11:22
  • I do not make an programmatically set. – Coral Doe Sep 17 '13 at 11:32
  • You should put a break point on this `Close()` to see it pass on it. – Benoit Blanchon Sep 17 '13 at 11:34
  • I agree with @BenoitBlanchon. Furthermore you could have a look a `InitializeComponent()` or paste this methods content here. I'd assume somewhere within this method, there is call to add/bind data to combobox1! – Pilgerstorfer Franz Sep 17 '13 at 12:38

1 Answers1

1

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
Uwe
  • 316
  • 1
  • 7