Cursor Position in the combo box text

uma_thangaraj

New member
Joined
Jan 18, 2008
Messages
3
Programming Experience
Beginner
Hello everyone.

I have written the code to populate the combo box on the keypress event.The combo box gets populated correctly but I found that the cursor position in the combo box text gets set to 0 after every keypress event as a result of which it would display the combo box text in the reverse order. To solve this problem, I had explicitly set the selectionstart property of the combobox to the length of the combo box text +1.

But if the user positions the cursor at any given position in the combo box text and then starts typing, then it appends the newly typed text to the end of the text rather than at the required position. So can I have any method by which I can get the position of the cursor specified by the user and append the newly entered text at the required position??

Thanx in advance for any help.
 
When you set the Text property the caret automatically gets set to the beginning of the text. If the user just entered a character then the caret cannot possibly be at the beginning of the text. You can just handle the TextChanged event and, if the caret is at the beginning of the text, move it to the end.
 
Jmcilhinney thanx 4 replying.You are right when you said that the default behavior is to append the newly typed text to the starting position. But since I had written the sumthing like:

VB.NET:
cmbBox.SelectionStart=cmbBox.Text.Length+1
on the keypress event, it was appended to the end of the text.

But the requirement I have is that even if the user tries to edit the entered text ( for eg. If the user had initially typed ‘Minrity Report’ and then realizes that the combo box was not populated because of the typo, if he gets the cursor to move to the 4th position and enters ‘o’, then in my case, cmbBox.Text becomes’Minrity Reporto’ instead of the required ‘Minority Report’.)

Is there any workaround for this where I can replace the statement :
VB.NET:
cmbBox.SelectionStart=cmbBox.Text.Length+1
with something like: ??
VB.NET:
cmbBox.SelectionStart=CurrentCursorPositionInComboBoxText+1
 
Last edited by a moderator:
I would suggest upgrading to VB 2008 if it's practical. Auto-complete functionality was added to the TextBox and ComboBox classes in .NET 2.0. You can get VB 2008 Express for free, so if there aren't features in VS.NET 2003 that you can't do without, like Setup projects, then I'd suggest upgrading. You'll find that the language, the Framework and the IDE are much improved.
 
Back
Top