Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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?
Posted

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!

Posted (edited)

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:

 

   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

Edited by divil

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...