devxlogo

Simplify Programatic Selection in Combos

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
See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist