Thanks Arun, especially, and others for replying. I tried your
method, Arun, and it will change the font and style of the selected
item as you suggest, but I guess I still wasn't clear, because I
wanted the dropdown to contain mixed fonts. This is my solution after
a struggle using google. I guess my major problem was how to ask the
question!
First, I have to set the drawmode of the combobox in my form_load
event:
CurComboBox.DrawMode = DrawMode.OwnerDrawVariable
Then, every time CurComboBox is selected, the code in
CurComboBox_DrawItem, if it exists, is called. I do this:
Private Sub CurComboBox_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles CurComboBox.DrawItem
Dim myFont As System.Drawing.Font
If CONDITION = TRUE Then
myFont = New Font(FontFamily.Families(25), 11,
FontStyle.Italic)
Else
myFont = New Font(FontFamily.Families(25), 11,
FontStyle.Regular)
End If
e.Graphics.DrawString(L3Names(e.Index + 1), myFont,
Brushes.Black, e.Bounds.Height, e.Bounds.Top)
End Sub
Note a couple of things:
1) This routine is called for every item you have loaded in the
combobox. I only have 15 items loaded, so it's pretty quick. If you
have a lot, I guess it might be a handicap, but you might not use a
combox in that situation.
2) CONDITION is anything I want to test. if the item in the list
contains "a", for example. In my case, I want to display the 15 items
differently, depending on the condition of something else in the
form. If I'm looking at young animals, I want to highlight items 1,3,
6 and 7 for example. If I'm looking at older animals, I will want to
highlight 2, 4, 5 and all the other items.
On Dec 20, 9:41 am, "ARUN KUMAR" <[email protected]> wrote:
> Hai Arpaket,
>
> Now I am clear ie what u need
>
> First set the properties for the combo box
>
> DropDownStyle = DropDownList
>
> Add the item dynamically as previous but don't change font in that loop
> instead of that you can write the procedure in n
> SelectionChangeCommitted event
> (See in event properties)
>
> Private Sub CurComboBox_SelectionChangeCommitted(ByVal sender AsSystem.Object,
> ByVal e As System.EventArgs) Handles CurComboBox.SelectionChangeCommitted
>
> If CurComboBox.Text.Contains("a") Then
>
> CurComboBox.Font = New Font("Arial", 8, FontStyle.Italic)
>
> CurComboBox.BackColor = Color.Wheat
>
> Else
>
> CurComboBox.Font = New Font("Arial", 8, FontStyle.Regular)
>
> CurComboBox.BackColor = Color.White
>
> End If
>
> End Sub
>
> This event occurs when user select the item from combo box
>
> When the even t fire check the current text contains "a" if it true then
> change the font style and color so this will highlighted as your desire
>
> I think this is satisfy you
>
> I am wait for your reply
>
> 2008/12/20 Arpaket <[email protected]>
>
>
>
>
>
> > Thanks for you response, Sharp. Sorry I was obtuse. Using your code,
> > what I would like to be able to do is something like this:
>
> > CurComboBox.Items.Clear()'clear the previous Items
> > For Each s In CurItems 's is an string CurItems is array of string
> > CurComboBox.Items.Add(s) 'add item
> > If s.contains("a") then
> > CurComboBox.SelectedItem(s).Font.Italic
> > else
> > CurComboBox.SelectedItem(s).Font.Regular
> > end if
> > Next
>
> > OK, so I'm making some things up, but what I'm driving at is: If my
> > string contains an a, then I want that item in the ComboBox to be
> > italicized (or Bold, or a different Font, etc.). If it doesn't then
> > that element would be, whatever, not italic. Can I do this with
> > ComboBox? Of course SelectedItem(s).Font doesn't work. Is there
> > another way?
>
> > On Dec 19, 8:54 am, Sharp ArunKumar <[email protected]> wrote:
> > > Hai, Arpaket
> > > I can't able to fully understand what you need any way
>
> > > See the following code for dynamically add item to combobox
>
> > > CurComboBox.Items.Clear()'clear the previous Items
> > > For Each s In CurItems 's is an string CurItems is array of string
> > > CurComboBox.Items.Add(s) 'add item
> > > Next
> > > Do you want to occur the event when user select the item in combobox
> > > Then write in DropDown closed event
>
> > > On Dec 18, 11:16 pm, Arpaket <[email protected]> wrote:
>
> > > > Using VB.Net: I need to fill in a ComboBox with items that can be
> > > > highlighted according to their kind. Like BIG or small. Can I do
> > > > this with code dynamically in the ComboBox? If so, I don't know how
> > > > and sorry to be so stupid but you could help me. TIA.
>
> --
> Regards
> *******************
> *C.Arun Kumar *
> *******************