0
votes

I am trying to set the selectedindex on another form (form2) based on the user selected index on the first form (form1).

I am getting the value with this code but it returns a negative number.

public int SelectedComboIndex
{
   get { return comboBox1.SelectedIndex; }
}

And I am trying to set the comboBox Index by

comboBox1.SelectedIndex = form1.SelectedComboIndex;

Can anyone point me in the right direction as to how to do this?

Edit: More code for where its calling the code on form1

Form1 form1 = null;
public Form2(Form1 parentForm1) : this()
{
    form1 = parentForm1;
}
3
A SelectedIndex value of -1 means that nothing is selected, are you sure something is selected? The default will be -1 for the combBox1.SelectedIndexSwDevMan81
thats exactly the negative number Im getting. I am sure Im selecting it, but just trying to carry over the selection to the other combobox on form2heinst
post your code for form1 and form2 where you are setting the property SelectedComboIndex and where you are using itHabib
It was posted above. Ill add moreheinst
Can you show the method that calls this code : comboBox1.SelectedIndex = form1.SelectedComboIndex; ?T. Fabre

3 Answers

2
votes

A combobox returns a negative value (-1 normally) if no index is selected.

So I believe (I have not checked) that if you set a negative value for the SelectedIndex property, all you are going to do is clear the selection in your combobox.

Hope that helps.

1
votes

Best practice is to usually leave any sort of UI alterations in the Load method of the form, that way the form has a chance to initialize properly and all bindings are setup before you actually make changes. The Constructors should only be used to set internal state.

private Form1 _parentForm;
public Form2(Form1 parentForm) : this()
{
    _parentForm = parentForm;
}

public Form2()
{
    InitializeComponents();
}

private void Form2_Load(object sender, EventArgs e)
{
    richTextBox1.Font = new Font("Times New Roman", 12f, FontStyle.Regular); 
    dropdown(); 
    if(_parentForm != null)
        comboBox1.SelectedIndex = _parentForm.SelectedComboIndex; 
    comboBox1.DropDownStyle = ComboBoxStyle.DropDownList; 
}

Try that out and see if it works. Just be sure to add the Load handler to the form properly (either through the designer or in code via this.Load += new EventHandler(Form2_Load)

0
votes

Note: You should rename all your components to something more useful than controlType1, controlType2 etc. This is good for both you and us.

As other people said if form1.SelectedComboIndex returns -1 (because form1.comboBox1 has nothing selected) then the line comboBox1.SelectedIndex = form1.SelectedComboIndex will (correctly) set comboBox1's value to nothing.

Additionally, just because there's text present in your ComboBox doesn't mean it's got a selected value. Make sure you combo box has actually selected a value (not just changed its text). You can force picking a value by setting the DropDownStyle to DropDownList. Both other styles allow user entry of custom values.

If you want users to be able to type, then consider setting AutoCompleteMode to SuggestAppend and AutoCompleteSource to ListItems. This makes it easier to correctly select values from the combobox rather than just change the text (accidentally leaving it null).