Setting text in ComboBox, when style set as DropDownList

wcsloco

Member
Joined
Feb 7, 2005
Messages
14
Programming Experience
5-10
I have an issue that I've not yet been able to resolve. I've spent a couple weeks reviewing several forums and help sites and have not yet been able to get exactly what I need. I've had great luck in the past in this forum, and I'm hoping you all can help again.

I need to be able to set the text of a ComboBox when the style of that ComboBox is set to DropDownList. By definition, I understand that the DropDownList only allows the selected text to be one of the possible values in the dropdown box. Really, all I want to do is have the text "(choose)" as the default text of my ComboBox. Currently, the only way I can get this to work is set the style to be DropDown. However, as I'm developing on a Tablet PC, I don't want the PenInputPanel to show when I click in my ComboBox. The only way I can consistently get the PenInputPanel to stay hidden is to set the style to DropDownList.

I've seen several posts from various users suggesting to set the Autoshow property to false, which doesn't exactly work for ComboBoxes. After a bit of work, I was finally able to hide the PenInputPanel for my ComboBox "cbPT" by doing the following code:

VB.NET:
Dim hwnd As IntPtr = GetWindow(cbPT.Handle, 5)
myPip.AttachedEditWindow = hwnd
myPip.AutoShow = False

My only remaining problem is that if the user clicks in any other control that allows the PenInputPanel to display, it then displays again in my ComboBox, even though it originally was hidden.

I know there must be a way of doing this. I downloaded some new controls created by Infragistics that did exactly what I'm looking for. That package included a ComboBox that has a NullText parameter that could be set. I'm just looking for help to create that same kind of ComboBox on my own. Can anyone help?
 
Why not just set item 0 on the combo to be the text "(Choose)". Upon validation, if cbPT.SelctedIndex = 0 then fail validation and request the user pick a value
 
It would appear that setting element 0 of my ComboBox to "(choose)" won't work because I need to have my ComboBox sorted. I don't want "(choose)" to show up anywhere else in the Drop-down list other than the very first item. Or am I missing something?
 
then i recommend that you take whatever youre going to put into the combo, as an array or something, sort it, then:

Dim mySortedArrayOfItems() as String
mySortedArrayOfItems = "cat,dog,ball,apple".Split(","c)

'double check this call.. im on a computer with no VStudio
Arrays.Sort(mySortedArrayOfItems)

myCombo.Items.Clear()
myCombo.Items.Add("choose")
myCombo.Items.AddRange(mySortedArrayOfItems)
 
Not sure why I never thought to sort the array myself and leave the ComboBox unsorted. This now works well for me. Thanks so much for your input.
 
I am interested in what jmcilhinney suggested. Can anyone provide code samples of how to inherit a control, and how to override the OnPaint Method?
 
Here's the sort of thing I thought would work but it appears not to and I'm not sure why:
VB.NET:
Public Class MyComboBox
    Inherits ComboBox

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)

        If Me.SelectedIndex = -1 Then
            e.Graphics.DrawString("(Select a Proxy Server)", Me.Font, Brushes.Black, 3, 3)
        End If
    End Sub

End Class
 
The Paint event is not relevant for the Combobox class. Instead you have to set the DrawMode to for instance OwnerDrawFixed and handle the DrawItem event. Note that this means you draw all items. Example:
VB.NET:
Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) _
Handles ComboBox1.DrawItem
  Dim cbo As ComboBox = DirectCast(sender, ComboBox)
  If e.Index = -1 Then
    e.Graphics.DrawString("Choose:", cbo.Font, New SolidBrush(cbo.ForeColor), e.Bounds.X, e.Bounds.Y)
  Else
    e.DrawBackground()
    If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
      e.Graphics.DrawString(cbo.Items(e.Index), cbo.Font, Brushes.White, e.Bounds.X, e.Bounds.Y)
    Else
      e.Graphics.DrawString(cbo.Items(e.Index), cbo.Font, New SolidBrush(cbo.ForeColor), e.Bounds.X, e.Bounds.Y)
    End If
    e.DrawFocusRectangle()
  End If
End Sub
Do this and "Choose:" is displayed initally in runtime (when DropDownStyle is set to DropDownList). If you do the same with an inherited Combobox it also draws in Designer. (I usually add a Usercontrol, then if need to change its inheritance to a specific control, this helps me because the control is added directly to the Toolbox in Designer.)
 
Back
Top