473,414 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Custom Drawn ComboBox: DrawFocusRectangle doesn't work properly?

Hi there,

I made a custom drawn ComboBox. The DrawItem event handler is (explanation
follows)

<code>
private void OnDrawItem (
object sender,
System.Windows.Forms.DrawItemEventArgs e )
{
ComboBox theCombo = (ComboBox) sender;
DataRowView drv = (DataRowView) theCombo.Items[ e.Index ];
string comboTxt = (string) drv[ theCombo.DisplayMember ];

bool configOk = PropertyValidation.ValidateEntries( comboTxt );
Brush bBrush = configOk ? Brushes.LimeGreen : Brushes.Tomato;

e.Graphics.FillRectangle (
bBrush,
e.Bounds
);
e.Graphics.DrawString (
comboTxt,
e.Font,
new SolidBrush( e.ForeColor ),
e.Bounds,
StringFormat.GenericDefault
);
e.DrawFocusRectangle( );
}
</code>

The code retrieves the text that is displayed in the combo box, and checks
whether the configuration for that item is ok. Depending on this I use a
green color when the configuration for the item is ok, and a red color if the
configuration is not ok (in my case, colors LimeGreen and Tomato). So what I
do is fill the display rectangle with a color depending on configuration,
draw the text and then call DrawFocusRectangle.

The problem is that sometimes the Focus Rectangle is not drawn (say 50% of
the time). Is there an issue with the DrawFocusRectangle method call? Is
there something I'm doing wrong? Examples on the net all do it like that...

Kind regards,
--
Tom Tempelaere.
Nov 17 '05 #1
1 5597
"TT (Tom Tempelaere)" wrote:

[...]
<code>
private void OnDrawItem (
object sender,
System.Windows.Forms.DrawItemEventArgs e )
{
ComboBox theCombo = (ComboBox) sender;
DataRowView drv = (DataRowView) theCombo.Items[ e.Index ];
string comboTxt = (string) drv[ theCombo.DisplayMember ];

bool configOk = PropertyValidation.ValidateEntries( comboTxt );
Brush bBrush = configOk ? Brushes.LimeGreen : Brushes.Tomato;

e.Graphics.FillRectangle (
bBrush,
e.Bounds
);
e.Graphics.DrawString (
comboTxt,
e.Font,
new SolidBrush( e.ForeColor ),
e.Bounds,
StringFormat.GenericDefault
);
e.DrawFocusRectangle( );
}
</code>

[...]

I replaced the call to DrawFocusRectangle with the following:

if( ( e.State & DrawItemState.Selected ) != 0 )
ControlPaint.DrawFocusRectangle( e.Graphics, e.Bounds );

Thanks to MVP Herfried K. Wagner for pointing that out.

I really wonder why the regular call to DrawFocusRectangle doesn't work.
Would this be a .NET 1.1 issue?

Thanks,
Tom Tempelaere.
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Richard Bateman | last post by:
I need to make a combo box that doesn't look like the standard windows controls. I thought that extending the combobox control would make the most sense, so I could draw it myself. However,...
7
by: Martin Schulze | last post by:
Hello, i tried to compose myself a custom usercontrol which is derieved from System.Windows.Forms.UserControl. It contains 2 comboboxes and one textbox (which are also custom controls, but...
0
by: Peder Y | last post by:
How can I set e.DrawFocusRectangle() correct in an owerdrawn multiselect listbox? I do all my drawing (e.g. OnMouseMove) calling OnDrawItem and constructing the DrawItemEventArgs. The problem is...
1
by: Brian Henry | last post by:
I am trying to make a custom user control that gets a list of users from our database and populates the list, its an owner drawn control also, the problem is, I placed the item onto a form and...
2
by: Don | last post by:
I've been trying to create a custom collection that can be bound to a combobox via the DataSource property, but I can't seem to get it to work. I've created a class that implements IList, and it...
1
by: Lars Netzel | last post by:
Hi! In VB.NET want to draw my own items in a ComboBox I have added a ComboBox to the Form with this. -----------------------------------------------------------------------------------...
2
by: AMDRIT | last post by:
Hello Everyone, I would like to format the Display Members of a combobox's datasource. Is there a way to apply a format without subclassing the original datasource? For example, given a list of...
0
by: Brian Henry | last post by:
Ok whats going on here... got a couple problems... this is relevant now to this group since .NET 2.0 is finally RTM 1) draws really slow when making it a large form (resizing speed is jerky) to...
0
by: Brian Henry | last post by:
I cant seem to remember this one... I have a list view, and im owner drawing the items, i dont want the selection to hide when it loses focus so i have hideselection = false, but when i owner draw...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.