Remove selection from ComboBox item

jhasty

Newcomer
Joined
Feb 23, 2003
Messages
5
I am trying to implement a ComboBox control with 6 items and no editing by the user. When I select an item, I want it to display in normal (black on white), not selected (white on blue) colors. I have tried several ugly kludges, none of which work. Does anyone know how to do this?
 
Yeah, I think that works, but I would like to have the text display in normal color immediately, as soon as the item is selected. All of the ComboBox events seem to fire before the text is selected, so I don't have a hook to automatically deselect the text. I'm thinking about placing some other control where it will be overlapped by the combobox's dropdown box, and using the overlapped control's paint event.

Maybe I just shouldn't be so picky, and learn to like white on blue text!
 
You should, really. The white on blue is configuration in the display settings, so any user that doesn't like that highlight colour is free to change it, and probably already will have.
 
This is ugly, but it works. Open a new Windows Form project, put a button and a combobox on the form, put a timer in the tray, and paste in the code below. When the combobox gets control, the timer is enabled. When the user selects a combobox item, or the mouse leaves the combobox, a flag is set. The timer tick event checks the flag, and when it finds the flag set, the focus is moved to the button (thus unselecting the text), and the timer is disabled. Here's the code:

Visual Basic:
    Dim FirstTime = True
    Dim IndexChanged = True

    Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
        ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
        ComboBox1.Items.Add("Apple")
        ComboBox1.Items.Add("Banana")
        ComboBox1.Items.Add("Cherry")
        ComboBox1.Items.Add("Date")
        ComboBox1.SelectedIndex = 0
        Timer1.Enabled = True
    End Sub

    Private Sub ComboBox1_Enter(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles ComboBox1.Enter
        Timer1.Enabled = True
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If Not FirstTime Then
            IndexChanged = True
        Else
            FirstTime = False
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Timer1.Tick
        If IndexChanged Then
            Button1.Focus()
            Timer1.Enabled = False
            IndexChanged = False
        End If

    End Sub

    Private Sub ComboBox1_MouseLeave(ByVal sender As Object, _
 ByVal e As System.EventArgs) Handles ComboBox1.MouseLeave
        Button1.Focus()
        Timer1.Enabled = False
    End Sub
 
Last edited by a moderator:
Back
Top