PC Review


Reply
Thread Tools Rate Thread

ComboBox : display different string than the selected list item

 
 
tohear
Guest
Posts: n/a
 
      21st Aug 2003
What I'm trying to do :
- Add a form to a new project
- Add a comboBox to the form with DropDownStyle = DropDown
- In form load :
ComboBox1.Items.Add("01 - USA")
ComboBox1.Items.Add("31 - Holland")
ComboBox1.Items.Add("33 - France")
ComboBox1.Items.Add("41 - Switzerland")

The desired behaviour is that when a user selects a list item only the
number is displayed in the textbox part of the comboBox. So if "01 -
USA" is selected only "01" should be displayed.

Roughly translating the way this would be done in VB6 I tried the
following code :

Private Sub ComboBox1_SelectionChangeCommitted( _
ByVal sender As Object, ByVal e As System.EventArgs) _
Handles ComboBox1.SelectionChangeCommitted

Dim listItemText As String
If ComboBox1.SelectedIndex >= 0 Then
'Calculate and store the value we want to display
listItemText = ComboBox1.SelectedItem.ToString
If listItemText.Length >= 2 Then
listItemText = listItemText.Substring(0, 2)
End If
'Cancel the selection
ComboBox1.SelectedIndex = -1
'Update the textbox part
ComboBox1.Text = listItemText
End If

End Sub


However this doesn't work. I've also tried experimenting with the
SelectedValueChanged, SelectedIndexChanged and TextChanged events,
with no better results. It seems that changing the selectedIndex in
any of these routines always cancels out changes made via the text
property, whatever the order the operations are carried out in.


I've finally resorted to using a timer to update ComboBox1.Text after
all these events have finished firing, but it's a very shoddy
solution.


Surely there must be a way to do this properly?
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
combobox display item when not in list Nick X Microsoft Access Forms 1 3rd Nov 2009 09:18 PM
Display Selected Item in a List Box Melissa M Microsoft Access Form Coding 3 5th Jun 2008 10:56 PM
Combobox not display selected item Mark Boettcher Microsoft C# .NET 3 21st Dec 2007 07:43 PM
List Box does not display selected item until refreshed =?Utf-8?B?V2F5bmU=?= Microsoft Access 0 13th Nov 2006 06:42 PM
display one string and store another in same combobox item mdb Microsoft C# .NET 2 30th Jul 2005 12:18 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:42 AM.