Simplify Programatic Selection in Combos

Here’s a useful procedure to position a ComboBox according to a value of the ItemData property or the List property. It’s useful to position a ComboBox with values taken from a database, and this way, become independent of the index property. For example, you might fill a ComboBox with the serial port’s baud speeds, including a description in List and the value in bauds in ItemData:

 Public Sub LlenarCombo(pCombo As ComboBox)	With pCombo		.Clear		.AddItem "1200 bps"		.ItemData(0) = 1200		.AddItem "2400 bps"		.ItemData(1) = 2400		.AddItem "4800 bps"		.ItemData(2) = 4800		.AddItem "9600 bps"		.ItemData(3) = 9600		.AddItem "14400 bps"		.ItemData(4) = 14400		.AddItem "28800 bps"		.ItemData(5) = 28800		.ListIndex = 0	End WithEnd SubPublic Sub PosicionarCombo(pCombo As _	ComboBox, ByVal pValor As Variant)	Dim i As Integer	If IsNumeric(pValor) Then		' Search by ItemData		For i = 0 To pCombo.ListCount - 1			If pCombo.ItemData(i) = pValor Then				pCombo.ListIndex = i				Exit For			End If		Next i	Else		' Search by List 		For i = 0 To pCombo.ListCount - 1			If pCombo.List(i) = pValor Then				pCombo.ListIndex = i				Exit For			End If		Next i	End IfEnd SubPrivate Sub cmdPosItemData_Click()	PosicionarCombo cboTest, 9600End SubPrivate Sub cmdPosList_Click()	PosicionarCombo cboTest, "4800 bps"End Sub
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS