Solved

Owner Drawn Combobox OnDrawItem()

Posted on 2004-08-16
9
1,558 Views
Last Modified: 2008-04-03
I have an owner drawn combobox.  When dropped down the FocusRectangle highlights an item in the combobox... however with the owner drawn control the text under the FocusRectangle is still the original color and therefore cannot be seen well.  In order to get the color to change under the highlight there needs to be a test to see if this is the highlighted item...   If Me.SelectedIndex = e.Index Then ... so using this i change the brush color for the drawstring method.  

Protected Overrides Sub OnDrawItem(ByVal e As DrawItemEventArgs)
        Dim brsh As Brush
        e.DrawBackground()
        e.DrawFocusRectangle()

            If Me.SelectedIndex = e.Index Then
                brsh = Brushes.Red
            Else
                brsh = Brushes.Black
            End If

            e.Graphics.DrawString(CType(ARList(e.Index), String), Me.Font, brsh, _
                    New RectangleF(0, e.Bounds.Y, _
                    e.Bounds.Width, e.Bounds.Height))
end sub

this works to change the color of the text under the focusrectangle however when you move the rectangle elsewhere the next item's color is changed without the original being changed back.  It was suggested to me to use a loop to redraw all of the controls but that is lost on me because you cannot change the e.index which is the index of the current item that is being drawn. So ultimately i need a way to redraw all rows of the combobox on every call of ondrawitem()

a correct answer here also gets half of the points at http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_21093096.html
0
Comment
Question by:BangorCC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 3

Accepted Solution

by:
DJ_Back-Q earned 250 total points
ID: 11812355
Call back the draw method of the whole combobox prior to call change the color of the item
0
 
LVL 3

Assisted Solution

by:DJ_Back-Q
DJ_Back-Q earned 250 total points
ID: 11812410
Forgot to mention, you can redraw by calling the method refresh.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11812489
You could also force a redraw by Invalidating the entire control, which should force a Paint, and then each OnDrawItem would need to be called for each item.

Bob
0
Veeam gives away 10 full conference passes

Veeam is a VMworld 2017 US & Europe Platinum Sponsor. Enter the raffle to get the full conference pass. Pass includes the admission to all general and breakout sessions, VMware Hands-On Labs, Solutions Exchange, exclusive giveaways and the great VMworld Customer Appreciation Part

 

Author Comment

by:BangorCC
ID: 11812517
im not sure if i understand what you are saying do you mean add combobox.Refresh() somewhere in the code above?  That would not work i have already tried it.  What method are you talking about when you say the draw method for the whole combobox.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11812554
Something like Me.Invalidate

Bob
0
 
LVL 3

Expert Comment

by:DJ_Back-Q
ID: 11812560
You to call a combobox.Refresh() before its calls your OnDrawItem()
, all you items will then redraw.

By calling your Refresh it should call OnDrawItem for each item in your list.
0
 

Author Comment

by:BangorCC
ID: 11812991
ok the invalidate and refresh dont seem to work when i add them to the top of the OnDrawItem sub... I cannot invalidate or refresh outside of this sub because all of my problems occur when the combobox is dropped down before an item is actually selected.  Everything is fine when you click the dropdown

here is what is happening in sequence
when you first dropdown the combobox you get this
http://bangorcc.dnsalias.com/combopics/combo1.jpg

then you move the selection down to the next item in the list
http://bangorcc.dnsalias.com/combopics/combo2.jpg

and again
http://bangorcc.dnsalias.com/combopics/combo3.jpg

interestingly the OnDrawItem fires for the item you are leaving however e.index is still pointing to it at that point so the test fails
0
 
LVL 3

Expert Comment

by:DJ_Back-Q
ID: 11813043
Can you put up some kind for flag, which will almost follow the e.index, but will be accurate for you?

And ar eyou from Bangor Maine?
0
 

Author Comment

by:BangorCC
ID: 11813429
DJ_Back-Q   I am from Bangor ME are you local?
Well I cannot think of a way to keep a flag without having a different sub fire when the selection changes in the dropdown... Otherwise my flag will keep loading the same values that i am trying to use now
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question