KnowDotNet

Empowering the CF ComboBox

Getting Around the Limitations of the CF ComboBox

by Les Smith

The Compact Framework ComboBox does not allow entering or editing the contents of the Text property.  This article will show a simple way to get around this limitation.

If you have used the ComboBox in developing with the Compact Framework, you immediately learn that tapping in the Text property of the ComboBox causes the ComboBox to drop down its list.  If you select an item from the list and then try to edit the selected value, you will be experated because the drop down list will appear again as soon as you place the stylus in the Text portion of the ComboBox.

The form shown, in Figure 1 below, has three ComboBoxes, but they are special, in that you can edit, what appears to be the Text property of the boxes.  This is not rocket science; rather it is done with programatic "smoke and mirrors."  

Figure 1 - Empowered ComboBoxes.

Empowered ComboBoxes


Although the ComboBoxes look unaltered, I have placed TextBoxes over the Text portion of the ComboBox.  In order to get them to look like they are part of the ComboBox, I set the Font Size of the Text boxes to Tahoma, 9pt, and the Font Size of the ComboBoxes to Tahoma 10pt.  Otherwise, the TextBox will be higher that the area of the ComboBox that houses the drop down arrow.  

In addition to placing the the TextBox over the ComboBox, I did two additional things to make the text editable.  First, I place the following code in the SelectedIndexChange events of the respective ComboBoxes.  This code simply places the selected item from the ComboBox into the Text property of the corresponding TextBox.

    Private Sub cbSubject_SelectedIndexChanged(ByVal sender As Object, _
      
ByVal e As System.EventArgs) Handles cbSubject.SelectedIndexChanged
        
Me.txtSubject.Text = Me.cbSubject.Text
    
End Sub

    Private Sub cbCategory_SelectedIndexChanged(ByVal sender As Object, _
      
ByVal e As System.EventArgs) Handles cbCategory.SelectedIndexChanged
        
Me.txtCategory.Text = MecbCategory.Text
    
End Sub

    Private Sub cbLocation_SelectedIndexChanged(ByVal sender As Object, _
      
ByVal e As System.EventArgs) Handles cbLocation.SelectedIndexChanged
        
Me.txtLocation.Text = MecbLocation.Text
    
End Sub

Now the user can select an item from the ComboBox, and then immediately add to the selection in the TextBox.  Obviously, when you get ready to use the selected item, you must pick up the value from the TextBox, not the ComboBox.  You could create a control using this same type of logic, and I may do that as I continue to use the CF ComboBox that is somewhat limited.