Setting the Text property to an empty string sets the SelectedIndex to -1

A

Academia

I let the use modify the text of a combobox and then I replace the selected
item with the new text (in Keyup event).

But if he sets the Text property to an empty string ("") that sets the
SelectedIndex to -1.

Do you have any suggestion for getting around this problem?



Thanks
 
C

Cor Ligthert[MVP]

Hi,

Without telling us how you use the combobox, it seems for me almost
impossible that you get a right answer (you never know some people can win
millions in a lottery).

A combobox has a lot of combinations how to use it.

Cor
 
P

Patrice

It looks like expected to me as SelectedIndex indicates which entry in the
list is selected (-1 if none). You could set the index to the empty string
item (if you have one ?) if you really need to have something selected.

Note also that depending on what you are trying to do, you could perhaps
also use a texbox with AutoCompletion capabilities (new in 2.0)...
 
A

Academia

Patrice said:
It looks like expected to me as SelectedIndex indicates which entry in the
list is selected (-1 if none). You could set the index to the empty string
item (if you have one ?) if you really need to have something selected.

DropDownStyle is DropDown
AutoCompleteMode is None

I need to know which item is selected when the user changes the text box so
that I can then replaced the selected entry's text with the user input. But
if while he is typing he clears the box the selectedIndex changes to -1. I
can't figure why they change the SelectedIndex because the text box has
changed. Can you?

Does the textbox drive the selection in some way? I know selecting can fill
the textbox but can changing text box change the SelectionIndex?

I tried saving SelectedIndex and resetting it but I think that may cause
side effects such as raised events.

Thanks
 
A

Academia

DropDownStyle is DropDown
AutoCompleteMode is None

I need to know which item is selected when the user changes the text box so
that I can then replaced the selected entry's text with the user input. But
if while he is typing he clears the box the selectedIndex changes to -1. I
can't figure why they change the SelectedIndex because the text box has
changed. Can you?

Does the textbox drive the selection in some way? I know selecting can fill
the textbox but can changing text box change the SelectionIndex?

I tried saving SelectedIndex and resetting it but I think that may cause
side effects such as raised events.

Thanks

This is a copy of text sent in reply to a different responder.
I don't know if responders always read replies to other responders.
 
P

Patrice

AFAIK the dropdowncontrol works this way (with the DropDown style) :
- either the user selects an entry in the list and the selectedindex is then
the index of the selected item
- if the user enters some text, then by definition he didn't select an item
so selectedindex is -1

If you want the dropdown style use the Text property to know the value of
the selected entry or the text entered by the user (that match or not a
possible entry).

If you want to constraint the user to one of those entries, use the
DropDownList style.

You could check yourself to which entry the text entered by the user match
and reselect the corresponding item but it wuld defeat the purpose of the
dropdown style and of course it could match or not depending on what the
user entered.

For now I'm not sure to understand. You may want to explain what you want to
do (that is to let the user select between several items or only those or to
let the user select between items OR enter some text freely that could match
or not one of these items).
 
C

Cor Ligthert[MVP]

Academia,

I mean, are you using a datasource with a datamember and a displaymember or
are you just filling the itemcollection in the combobox?

Cor
 
A

Academia

Just filling the itemcollection with strings. Then the user can select an
item which puts it into the textbox so the user can modify it. As he changes
the text in the textbox KeyUp changes the SelectedItem's text. But if he
clears the textbox while changing it, the selectedindex no longer points to
the item being worked on.

thanks
 
A

Academia

I fill the itemcollection with strings. Then the user can select an item
which puts it into the textbox so the user can modify it. As he changes the
text in the textbox KeyUp changes the SelectedItem's text. But if he clears
the textbox while changing it, the selectedindex no longer points to the
item being worked on.

Briefly, I want the user to be able to update any item in the conbobox's
itemcollection.

thanks
 
S

Stephany Young

When the value of the Text property of a ComboBox control, (DropDown style),
EXACTLY matches an entry in the Items property then the SelectedIndex
property reflects the ordinal position of that entry in the Items property
and the SelectedItem reflects that entry.

When the value of the Text property of a ComboBox control, (DropDown style),
boes NOT EXACTLY match any entries in the Items property then the
SelectedIndex property is -1 and the SelectedItem property is Nothing.

You will be aware, (or you should be), that when you change selections, the
SelectedIndexChanged event is raised twice. The first time is when the
current item becomes unselected (SelectedIndex = -1 and SelectedItem Is
Nothing). The second time is when the new item becomes selected
(SelectedIndex = <ordinal position> and SelectedItem Is <item>.

When you attempt to use the Text property in the manner you appear to be
describing you must forget about using the KeyDown and KeyUp events and
instead react to a signal that the user has finished typing what he wants to
type.

This is easily down by using the Enter (Return) key and trapping it in the
KeyPress event.

In that event handler you need to locate the value of the Text property in
the Items property and set the SelectedIndex or SelectedItem accordingly.

You will, of course need to add any necessary code to take care of any case
sensitivity/insensitivity issues introduced by your business rules.

This, of course, is all very well for selecting items, but what about
renaming an existing selection.

This can get complicated because you have to know whetehr your user is
typing to locate an item or typing to rename an item. The Text property of
the ComboBox is not well suited for this purpose.

In my opinion, it would be better to have a seperate TextBox control for
this purpose.
 
C

Cor Ligthert[MVP]

Just filling the itemcollection with strings. Then the user can select an
item which puts it into the textbox so the user can modify it. As he
changes the text in the textbox KeyUp changes the SelectedItem's text. But
if he clears the textbox while changing it, the selectedindex no longer
points to the item being worked on.
Seems to me exactly as it should be. The Index should then be -1.

However you can of course save forever the latest index as the textbox is
not spaces. (The VB static declaration is great for that).

Cor
 
P

Patrice

Ah ok ! I didn't catch the unusal use of this dropdown.

You could just save the selected index (when the user selects an item) so
that you can use this saved index when the user starts editing instead of
using the ListIndex property that will now be latered by design.

That said I would agreee with Stephany. Using the dropdown as a UI that
allows to select and then edit an item is perhaps not that a good idea.

I would also use a separate text box for editing...
 
A

Academia

Cor Ligthert said:
Seems to me exactly as it should be. The Index should then be -1.

However you can of course save forever the latest index as the textbox is
not spaces. (The VB static declaration is great for that).


I do that now

thanks
 
A

Academia

Thanks for the complete answer.
I didn't know there was such a connection between the textbox and the items.
I guess I assumed that selection meant the user clicked an item.

Thanks
 
A

Academia

Patrice said:
Ah ok ! I didn't catch the unusal use of this dropdown.

You could just save the selected index (when the user selects an item) so
that you can use this saved index when the user starts editing instead of
using the ListIndex property that will now be latered by design.

That said I would agreee with Stephany. Using the dropdown as a UI that
allows to select and then edit an item is perhaps not that a good idea.

I would also use a separate text box for editing...
I didn't know there was such a connection between the textbox and the items.
I guess I assumed that selection meant the user clicked an item.

I suppose I could use a Textbox with a ListBox to make my own ComboBox in
which changing the textbox does not affect the selected index.

I'm miss the Button with the unside-down ^ that users are use to seeing.

Maybe I could generate a control inheriting ComboBox and override something
to remove the behavior of changing selection when the textbox is changed.

Got any suggestions?


Thanks
 
A

Academia

Maybe I could generate a control inheriteing ComboBox and override someting
to remove the behavior of changing selection when the textbox is changed.

Got any suggestions?


thanks for the help
 
P

Patrice

As already suggested you could just save the SelectedIndex value :

Dim SavedIndex As Integer = 0

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

ComboBox1.Items.Add("")

ComboBox1.Items.Add("ABC")

ComboBox1.Items.Add("DEF")

End Sub



Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

SavedIndex = ComboBox1.SelectedIndex

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

ComboBox1.Items(SavedIndex) = ComboBox1.Text

End Sub
 
A

Academia

Thanks
Actually I did try your suggestion and it worked OK but I kept running into
changing something ( can remember right now what they were) would causing an
event to run that I didn't want run. So I'd create a Boolean field that
could be used to prevent the code being executed and set that before I
changed that thing.

Then I tried generating a control inheriting ComboBox and overriding
something to remove the behavior of changing selection when the textbox is
changed. Actually that was what I was asking suggestions for. I tried things
like Keyup. But couldn't get the effect I wanted.

Next I created a control using a Textbox and a Listbox and that was straight
forward and took very little code.

So it took some time but I now know much more about what a ComboBox is used
for.

Thanks for your interest.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top