set selected index of a ComboBox

  • Thread starter Thread starter Steffen Laser
  • Start date Start date
S

Steffen Laser

Hi group,
I have a problem that I already have posted to the german C# newsgroup.
Since nobody could help me there, I'd like to try it here again:

I set the selected item of a combobox like this:

myCombo.SelectedItem = 3;

But after that the ComboBox displays only the first entry of the list.
When I add the line

myCombo.DroppedDown = true;

before, and then step trough the code, I can see the list of the combo
dropping down, then the index (in this case 3) gets selected. After that,
the method is finished, no other code is processed. And then, when the form
comes up, there is the first item selected!

Funny, that the same code works some weeks ago - at this place nothing
changed. Does anybody have an idea what there happens? Or could it be, that
there is a problem with a MS patch or something like that?

Greetings,
Steffen.
 
Hi Steffen,
You should use SelectedIndex property!

Example
[Visual Basic, C#, C++] The following example illustrates the usage of the FindString method and SelectedIndex property. The example is part of a runnable code sample in the ComboBox class overview.

[Visual Basic]
Private Sub findButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim index As Integer
index = comboBox1.FindString(textBox2.Text)
comboBox1.SelectedIndex = index
End Sub
[C#]
private void findButton_Click(object sender, System.EventArgs e) {
int index = comboBox1.FindString(textBox2.Text);
comboBox1.SelectedIndex = index;
 
Hi Juan,
thank you very much, but I tried all of the following:

SelectedIdex = 3;

SelectedItem = object; // since the datasource contains an ArrayList of
objects with overridden ToString()-Method. I also tried an ArrayList with
Strings as DataSource - without success, also with SelectedIndex and the
followwing

FindStringExact("searchstring");

Thanks,
Steffen.



Hi Steffen,
You should use SelectedIndex property!

Example
[Visual Basic, C#, C++] The following example illustrates the usage of the
FindString method and SelectedIndex property. The example is part of a
runnable code sample in the ComboBox class overview.
[Visual Basic]
Private Sub findButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim index As Integer
index = comboBox1.FindString(textBox2.Text)
comboBox1.SelectedIndex = index
End Sub
[C#]
private void findButton_Click(object sender, System.EventArgs e) {
int index = comboBox1.FindString(textBox2.Text);
comboBox1.SelectedIndex = index;
}
 
Hi Juan,
thank you very much, but I tried all of the following:

(1) SelectedIndex = 3;

(2) SelectedItem = object;
// since the datasource contains an ArrayList of
//objects with overridden ToString()-Method.
//I also tried an ArrayList with Strings as DataSource
// with all here mentioned methods/Properties - with the same result
// as reported in my first mail.

(3) FindStringExact("searchstring");

Thanks,
Steffen.

Hi Steffen,
You should use SelectedIndex property!

Example
[Visual Basic, C#, C++] The following example illustrates the usage of the
FindString method and SelectedIndex property. The example is part of a
runnable code sample in the ComboBox class overview.
[Visual Basic]
Private Sub findButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim index As Integer
index = comboBox1.FindString(textBox2.Text)
comboBox1.SelectedIndex = index
End Sub
[C#]
private void findButton_Click(object sender, System.EventArgs e) {
int index = comboBox1.FindString(textBox2.Text);
comboBox1.SelectedIndex = index;
}
 
You mussn't change the data source after the DataSource assignment (At least
i think)
Try to add the DataSourceChanged event and add a breakpoint inside it.


Steffen Laser said:
Hi Juan,
thank you very much, but I tried all of the following:

SelectedIdex = 3;

SelectedItem = object; // since the datasource contains an ArrayList of
objects with overridden ToString()-Method. I also tried an ArrayList with
Strings as DataSource - without success, also with SelectedIndex and the
followwing

FindStringExact("searchstring");

Thanks,
Steffen.



Hi Steffen,
You should use SelectedIndex property!

Example
[Visual Basic, C#, C++] The following example illustrates the usage of the
FindString method and SelectedIndex property. The example is part of a
runnable code sample in the ComboBox class overview.
[Visual Basic]
Private Sub findButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim index As Integer
index = comboBox1.FindString(textBox2.Text)
comboBox1.SelectedIndex = index
End Sub
[C#]
private void findButton_Click(object sender, System.EventArgs e) {
int index = comboBox1.FindString(textBox2.Text);
comboBox1.SelectedIndex = index;
}

Steffen Laser said:
Hi group,
I have a problem that I already have posted to the german C# newsgroup.
Since nobody could help me there, I'd like to try it here again:

I set the selected item of a combobox like this:

myCombo.SelectedItem = 3;

But after that the ComboBox displays only the first entry of the list.
When I add the line

myCombo.DroppedDown = true;

before, and then step trough the code, I can see the list of the combo
dropping down, then the index (in this case 3) gets selected. After that,
the method is finished, no other code is processed. And then, when the form
comes up, there is the first item selected!

Funny, that the same code works some weeks ago - at this place nothing
changed. Does anybody have an idea what there happens? Or could it be, that
there is a problem with a MS patch or something like that?

Greetings,
Steffen.
 
Hello/Grüezi Zürcher See,

very good point - not the DataSourceChanged-event but the events

SelectedValueChanged
SelectedindexChanged
and SelectionChangeCommitted

were fired. Do you have an idea how this could be? It happens after all code
I wrote is executed after leaving the last method of my code that was
called. The caller list shows only a method, that started the whole process.
Must be some code inside the .net classes. How can I track this code? Do I
have to derive the gui classes and override their methods to trace, which
methods are called, or is their a smarter method? Where can I find a
detailled description of gui processing in .net?

Thanx,
Steffen.

Zürcher See said:
You mussn't change the data source after the DataSource assignment (At least
i think)
Try to add the DataSourceChanged event and add a breakpoint inside it.


Steffen Laser said:
Hi Juan,
thank you very much, but I tried all of the following:

SelectedIdex = 3;

SelectedItem = object; // since the datasource contains an ArrayList of
objects with overridden ToString()-Method. I also tried an ArrayList with
Strings as DataSource - without success, also with SelectedIndex and the
followwing

FindStringExact("searchstring");

Thanks,
Steffen.



Hi Steffen,
You should use SelectedIndex property!

Example
[Visual Basic, C#, C++] The following example illustrates the usage of the
FindString method and SelectedIndex property. The example is part of a
runnable code sample in the ComboBox class overview.
[Visual Basic]
Private Sub findButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim index As Integer
index = comboBox1.FindString(textBox2.Text)
comboBox1.SelectedIndex = index
End Sub
[C#]
private void findButton_Click(object sender, System.EventArgs e) {
int index = comboBox1.FindString(textBox2.Text);
comboBox1.SelectedIndex = index;
}

Steffen Laser said:
Hi group,
I have a problem that I already have posted to the german C# newsgroup.
Since nobody could help me there, I'd like to try it here again:

I set the selected item of a combobox like this:

myCombo.SelectedItem = 3;

But after that the ComboBox displays only the first entry of the list.
When I add the line

myCombo.DroppedDown = true;

before, and then step trough the code, I can see the list of the combo
dropping down, then the index (in this case 3) gets selected. After that,
the method is finished, no other code is processed. And then, when the form
comes up, there is the first item selected!

Funny, that the same code works some weeks ago - at this place nothing
changed. Does anybody have an idea what there happens? Or could it be, that
there is a problem with a MS patch or something like that?

Greetings,
Steffen.
 
To track the code is very simple ... just add an bug :)
try{int a=1/0;}
catch(System.Exception e){e.StackTrace;}

but without the code is difficult to say what is wrong :(


Steffen Laser said:
Hello/Grüezi Zürcher See,

very good point - not the DataSourceChanged-event but the events

SelectedValueChanged
SelectedindexChanged
and SelectionChangeCommitted

were fired. Do you have an idea how this could be? It happens after all code
I wrote is executed after leaving the last method of my code that was
called. The caller list shows only a method, that started the whole process.
Must be some code inside the .net classes. How can I track this code? Do I
have to derive the gui classes and override their methods to trace, which
methods are called, or is their a smarter method? Where can I find a
detailled description of gui processing in .net?

Thanx,
Steffen.

Zürcher See said:
You mussn't change the data source after the DataSource assignment (At least
i think)
Try to add the DataSourceChanged event and add a breakpoint inside it.


Steffen Laser said:
Hi Juan,
thank you very much, but I tried all of the following:

SelectedIdex = 3;

SelectedItem = object; // since the datasource contains an ArrayList of
objects with overridden ToString()-Method. I also tried an ArrayList with
Strings as DataSource - without success, also with SelectedIndex and the
followwing

FindStringExact("searchstring");

Thanks,
Steffen.



Hi Steffen,
You should use SelectedIndex property!

Example
[Visual Basic, C#, C++] The following example illustrates the usage of the
FindString method and SelectedIndex property. The example is part of a
runnable code sample in the ComboBox class overview.
[Visual Basic]
Private Sub findButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim index As Integer
index = comboBox1.FindString(textBox2.Text)
comboBox1.SelectedIndex = index
End Sub
[C#]
private void findButton_Click(object sender, System.EventArgs e) {
int index = comboBox1.FindString(textBox2.Text);
comboBox1.SelectedIndex = index;
}

Hi group,
I have a problem that I already have posted to the german C# newsgroup.
Since nobody could help me there, I'd like to try it here again:

I set the selected item of a combobox like this:

myCombo.SelectedItem = 3;

But after that the ComboBox displays only the first entry of the list.
When I add the line

myCombo.DroppedDown = true;

before, and then step trough the code, I can see the list of the combo
dropping down, then the index (in this case 3) gets selected. After that,
the method is finished, no other code is processed. And then, when the
form
comes up, there is the first item selected!

Funny, that the same code works some weeks ago - at this place nothing
changed. Does anybody have an idea what there happens? Or could it be,
that
there is a problem with a MS patch or something like that?

Greetings,
Steffen.
 
Hello Zürcher See,
thank you, your hint lead me to the right solution. I could solve the
problem, that was generated by the specific imlementation of the
application - concerning to this, the method that queries the data can not
be located in the constructor of the form. In fact, I do not know exactly,
why this behaviour originates - but I redesigned the order of code calls and
now it is working properly! So thanks again for your help and assistance!

Steffen.

Zürcher See said:
To track the code is very simple ... just add an bug :)
try{int a=1/0;}
catch(System.Exception e){e.StackTrace;}

but without the code is difficult to say what is wrong :(


Steffen Laser said:
Hello/Grüezi Zürcher See,

very good point - not the DataSourceChanged-event but the events

SelectedValueChanged
SelectedindexChanged
and SelectionChangeCommitted

were fired. Do you have an idea how this could be? It happens after all code
I wrote is executed after leaving the last method of my code that was
called. The caller list shows only a method, that started the whole process.
Must be some code inside the .net classes. How can I track this code? Do I
have to derive the gui classes and override their methods to trace, which
methods are called, or is their a smarter method? Where can I find a
detailled description of gui processing in .net?

Thanx,
Steffen.

Zürcher See said:
You mussn't change the data source after the DataSource assignment (At least
i think)
Try to add the DataSourceChanged event and add a breakpoint inside it.


Hi Juan,
thank you very much, but I tried all of the following:

SelectedIdex = 3;

SelectedItem = object; // since the datasource contains an ArrayList of
objects with overridden ToString()-Method. I also tried an ArrayList with
Strings as DataSource - without success, also with SelectedIndex and the
followwing

FindStringExact("searchstring");

Thanks,
Steffen.



Hi Steffen,
You should use SelectedIndex property!

Example
[Visual Basic, C#, C++] The following example illustrates the usage
of
the
FindString method and SelectedIndex property. The example is part of a
runnable code sample in the ComboBox class overview.
[Visual Basic]
Private Sub findButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim index As Integer
index = comboBox1.FindString(textBox2.Text)
comboBox1.SelectedIndex = index
End Sub
[C#]
private void findButton_Click(object sender, System.EventArgs e) {
int index = comboBox1.FindString(textBox2.Text);
comboBox1.SelectedIndex = index;
}

Hi group,
I have a problem that I already have posted to the german C# newsgroup.
Since nobody could help me there, I'd like to try it here again:

I set the selected item of a combobox like this:

myCombo.SelectedItem = 3;

But after that the ComboBox displays only the first entry of the list.
When I add the line

myCombo.DroppedDown = true;

before, and then step trough the code, I can see the list of the combo
dropping down, then the index (in this case 3) gets selected. After
that,
the method is finished, no other code is processed. And then, when the
form
comes up, there is the first item selected!

Funny, that the same code works some weeks ago - at this place nothing
changed. Does anybody have an idea what there happens? Or could it be,
that
there is a problem with a MS patch or something like that?

Greetings,
Steffen.
 
Nothing :) tschüss

Steffen Laser said:
Hello Zürcher See,
thank you, your hint lead me to the right solution. I could solve the
problem, that was generated by the specific imlementation of the
application - concerning to this, the method that queries the data can not
be located in the constructor of the form. In fact, I do not know exactly,
why this behaviour originates - but I redesigned the order of code calls and
now it is working properly! So thanks again for your help and assistance!

Steffen.

Zürcher See said:
To track the code is very simple ... just add an bug :)
try{int a=1/0;}
catch(System.Exception e){e.StackTrace;}

but without the code is difficult to say what is wrong :(


all
code
Do
I
have to derive the gui classes and override their methods to trace, which
methods are called, or is their a smarter method? Where can I find a
detailled description of gui processing in .net?

Thanx,
Steffen.

You mussn't change the data source after the DataSource assignment (At
least
i think)
Try to add the DataSourceChanged event and add a breakpoint inside it.


Hi Juan,
thank you very much, but I tried all of the following:

SelectedIdex = 3;

SelectedItem = object; // since the datasource contains an
ArrayList
of
objects with overridden ToString()-Method. I also tried an ArrayList
with
Strings as DataSource - without success, also with SelectedIndex
and
the
followwing

FindStringExact("searchstring");

Thanks,
Steffen.



Hi Steffen,
You should use SelectedIndex property!

Example
[Visual Basic, C#, C++] The following example illustrates the
usage
of
a
runnable code sample in the ComboBox class overview.
[Visual Basic]
Private Sub findButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim index As Integer
index = comboBox1.FindString(textBox2.Text)
comboBox1.SelectedIndex = index
End Sub
[C#]
private void findButton_Click(object sender, System.EventArgs e) {
int index = comboBox1.FindString(textBox2.Text);
comboBox1.SelectedIndex = index;
}

Hi group,
I have a problem that I already have posted to the german C#
newsgroup.
Since nobody could help me there, I'd like to try it here again:

I set the selected item of a combobox like this:

myCombo.SelectedItem = 3;

But after that the ComboBox displays only the first entry of the list.
When I add the line

myCombo.DroppedDown = true;

before, and then step trough the code, I can see the list of the combo
dropping down, then the index (in this case 3) gets selected. After
that,
the method is finished, no other code is processed. And then,
when
the
form
comes up, there is the first item selected!

Funny, that the same code works some weeks ago - at this place nothing
changed. Does anybody have an idea what there happens? Or could
it
be,
that
there is a problem with a MS patch or something like that?

Greetings,
Steffen.
 
Back
Top