VB.Net ComboBox Control with Highlighted Items

1,465 views
Skip to first unread message

Arpaket

unread,
Dec 18, 2008, 1:16:57 PM12/18/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
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.

Sharp ArunKumar

unread,
Dec 19, 2008, 11:54:48 AM12/19/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting

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

Arpaket

unread,
Dec 19, 2008, 2:12:56 PM12/19/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
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?

sundar irene

unread,
Dec 19, 2008, 7:20:17 AM12/19/08
to DotNetDe...@googlegroups.com
s u can do it like this
<h1>sundar<h>
<h2>sundar<h2>
by using html code between the text

santhosh vs

unread,
Dec 20, 2008, 7:47:02 AM12/20/08
to DotNetDe...@googlegroups.com
For windows forms app you have to custom paint items
Google will help great regarding this
--
My Web Site
http://everlovingyouth.googlepages.com
My Technical Blog
http://acutedeveloper.blogspot.com
Skype :santhoshnta
Orkut :everlovingyouth

ARUN KUMAR

unread,
Dec 20, 2008, 12:41:05 PM12/20/08
to DotNetDe...@googlegroups.com
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 As System.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 <rick.p...@gmail.com>



--
Regards
*******************
*C.Arun Kumar *
*******************


Arpaket

unread,
Dec 22, 2008, 2:43:39 PM12/22/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
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" <carunkumar1...@gmail.com> 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 <rick.paque...@gmail.com>
Reply all
Reply to author
Forward
0 new messages