473,749 Members | 2,486 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
Oct 29 '07 #1
18 2188
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

Oct 30 '07 #2
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)...

--
Patrice

"Academia" <ac************ @a-znet.coma écrit dans le message de news:
ef************* *@TK2MSFTNGP04. phx.gbl...
>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

Oct 30 '07 #3

"Patrice" <http://www.chez.com/scribe/wrote in message
news:Ov******** ******@TK2MSFTN GP05.phx.gbl...
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
AutoCompleteMod e 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
>
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)...

--
Patrice

"Academia" <ac************ @a-znet.coma écrit dans le message de news:
ef************* *@TK2MSFTNGP04. phx.gbl...
>>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
SelectedInde x to -1.

Do you have any suggestion for getting around this problem?

Thanks


Oct 30 '07 #4

DropDownStyle is DropDown
AutoCompleteMod e 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.


"Cor Ligthert[MVP]" <no************ @planet.nlwrote in message
news:C8******** *************** ***********@mic rosoft.com...
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

Oct 30 '07 #5
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).

--
Patrice

"Academia" <ac************ @a-znet.coma écrit dans le message de news:
eQ************* *@TK2MSFTNGP06. phx.gbl...
>
"Patrice" <http://www.chez.com/scribe/wrote in message
news:Ov******** ******@TK2MSFTN GP05.phx.gbl...
>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
AutoCompleteMod e 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
>>
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)...

--
Patrice

"Academia" <ac************ @a-znet.coma écrit dans le message de news:
ef************* *@TK2MSFTNGP04. phx.gbl...
>>>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
SelectedInd ex to -1.

Do you have any suggestion for getting around this problem?

Thanks



Oct 30 '07 #6
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

Oct 30 '07 #7
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

"Cor Ligthert[MVP]" <no************ @planet.nlwrote in message
news:CC******** *************** ***********@mic rosoft.com...
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

Oct 30 '07 #8
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

"Patrice" <http://www.chez.com/scribe/wrote in message
news:OJ******** ******@TK2MSFTN GP04.phx.gbl...
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).

--
Patrice

"Academia" <ac************ @a-znet.coma écrit dans le message de news:
eQ************* *@TK2MSFTNGP06. phx.gbl...
>>
"Patrice" <http://www.chez.com/scribe/wrote in message
news:Ov******* *******@TK2MSFT NGP05.phx.gbl.. .
>>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.

DropDownStyl e is DropDown
AutoCompleteMo de 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
>>>
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)...

--
Patrice

"Academia" <ac************ @a-znet.coma écrit dans le message de news:
ef************* *@TK2MSFTNGP04. phx.gbl...
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
SelectedInde x to -1.

Do you have any suggestion for getting around this problem?

Thanks



Oct 30 '07 #9
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
SelectedIndexCh anged 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 positionand 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.
"Academia" <ac************ @a-znet.comwrote in message
news:u0******** ******@TK2MSFTN GP06.phx.gbl...
>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

"Patrice" <http://www.chez.com/scribe/wrote in message
news:OJ******** ******@TK2MSFTN GP04.phx.gbl...
>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).

--
Patrice

"Academia" <ac************ @a-znet.coma écrit dans le message de news:
eQ************* *@TK2MSFTNGP06. phx.gbl...
>>>
"Patrice" <http://www.chez.com/scribe/wrote in message
news:Ov****** ********@TK2MSF TNGP05.phx.gbl. ..
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.

DropDownSty le is DropDown
AutoCompleteM ode 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
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)...

--
Patrice

"Academia" <ac************ @a-znet.coma écrit dans le message de news:
ef************* *@TK2MSFTNGP04. phx.gbl...
>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
SelectedInd ex to -1.
>
Do you have any suggestion for getting around this problem?
>
>
>
Thanks
>



Oct 31 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1998
by: Mike | last post by:
The following (C#) code snippet sets the text property of a label to null, then tests it for null, only to find out that is isn't. Can anyone explain why? Label1.Text = null; if (Label1.Text == null) { Label2.Text = "Is null"; } else { Label2.Text = "Is not null"; }
1
2170
by: Alexander | last post by:
I am building a little unique dialog editor and have derived some new classes like DButton, DLabel and DTextBox. The user adds these Controls to a panel which is part of a class Document which is not a control. The controls in the dialog can be edited with a PropertyGrid. When the user closes the dialog he edited it was first disposed, because it was not part of an Control anymore. I changed that by overriding the Dispose function of the...
0
1079
by: abhishekjha22 | last post by:
Hi, I have a CEdit control. I can accept only 4 characters and that should be Numeric and no spaces allowed. I want to set the contents of the Editbox empty. But when i try to do edit.SetText(" "); it asserts. Is there any possible way of emptying the text of the edit control
3
1430
by: eljainc | last post by:
Hello, I am trying to figure out how to efficiently do the following: Say I have 10 TextBox fields on a form. From each of those, I would like to have a right click context menu. There are 10 menu items on the context menu. Once one of those items are selected, I would like the text to be copied into the text field
5
4471
by: cyrak | last post by:
This should be fairly trivial I think..but is not working. I have a textbox that i add at runtime TextBox tagText = new TextBox(); then i put it in a panel' PanelWODetails.Controls.Add(tagText); once the user enters some text and click on a button I try to grab that text, but the Text property is always empty.
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.