ComboBox Custom OnPaint
-
I have a control that is derived from the standard ComboBox. I have an override for the OnPaint method that paints a border, drop down arrow button, background, and foreground. The only problem is that something (some other event/subcontrol) is drawing a black text box with a large font over top of the text box region of the combo box control. I am drawing a background in my OnPaint event, and it is being draw on top of. I can see that the background is drawn because there is a border around the text box region. My code looks like the following:
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); DrawBorder(); DrawBackground(); DrawArrowButton(); DrawForegroundText(); }
What am I missing here??? By the way, this control works just fine if the DropDownStyle is set to DropDownList, however I'm need it to work as a DropDown (being able to edit the text box). --Ian; -
I have a control that is derived from the standard ComboBox. I have an override for the OnPaint method that paints a border, drop down arrow button, background, and foreground. The only problem is that something (some other event/subcontrol) is drawing a black text box with a large font over top of the text box region of the combo box control. I am drawing a background in my OnPaint event, and it is being draw on top of. I can see that the background is drawn because there is a border around the text box region. My code looks like the following:
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); DrawBorder(); DrawBackground(); DrawArrowButton(); DrawForegroundText(); }
What am I missing here??? By the way, this control works just fine if the DropDownStyle is set to DropDownList, however I'm need it to work as a DropDown (being able to edit the text box). --Ian;In calling all of your painting functions, are you passing the Graphics object sent inside the PaintEventArgs or are you creating another one?? You should be passing the Graphics given to you by the PaintEventArgs. Dave Kreskowiak Microsoft MVP - Visual Basic
-
In calling all of your painting functions, are you passing the Graphics object sent inside the PaintEventArgs or are you creating another one?? You should be passing the Graphics given to you by the PaintEventArgs. Dave Kreskowiak Microsoft MVP - Visual Basic
-
Sorry for my simple example. Yes, I am passing the PaintEventArgs that is passed to the event. So the GC is being used to do all the painting. --Ian;
That's why we always suggest Copy-'n-Paste the code instead of retyping it. You said that this text is being painted OVER what your code painted? Dave Kreskowiak Microsoft MVP - Visual Basic
-
That's why we always suggest Copy-'n-Paste the code instead of retyping it. You said that this text is being painted OVER what your code painted? Dave Kreskowiak Microsoft MVP - Visual Basic
I'm painting the background a green with a FillRectangle, and when it is displayed to the screen, I see a 2 or 3 pixel border of green surrounding a black text box region (that was not drawn by me). This is what I mean when I say that it was drawn over top of what I had drawn. The sample class below does not contain all the code, however it will give you an idea of the problem. Just drop this class into your a project, compile, and drop it the ComboDraw control onto a form.
using System; using System.Collections.Generic; using System.Drawing; using System.Text; using System.Windows.Forms; namespace ComboTest { class ComboDraw : ComboBox { public ComboDraw() { DrawMode = DrawMode.OwnerDrawFixed; DropDownStyle = ComboBoxStyle.DropDown; FlatStyle = FlatStyle.Flat; SetStyle(ControlStyles.UserPaint, true); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.FillRectangle(new SolidBrush(Color.Green), ClientRectangle); } } }
--Ian; -- modified at 9:19 Thursday 13th July, 2006 -
I'm painting the background a green with a FillRectangle, and when it is displayed to the screen, I see a 2 or 3 pixel border of green surrounding a black text box region (that was not drawn by me). This is what I mean when I say that it was drawn over top of what I had drawn. The sample class below does not contain all the code, however it will give you an idea of the problem. Just drop this class into your a project, compile, and drop it the ComboDraw control onto a form.
using System; using System.Collections.Generic; using System.Drawing; using System.Text; using System.Windows.Forms; namespace ComboTest { class ComboDraw : ComboBox { public ComboDraw() { DrawMode = DrawMode.OwnerDrawFixed; DropDownStyle = ComboBoxStyle.DropDown; FlatStyle = FlatStyle.Flat; SetStyle(ControlStyles.UserPaint, true); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.FillRectangle(new SolidBrush(Color.Green), ClientRectangle); } } }
--Ian; -- modified at 9:19 Thursday 13th July, 2006OK. I see what you're looking at. You're seeing the TextBox being drawn by the underlying ComboBox class. The bad news is, as far as I can tell, you can't get rid of it and can't override it. It's completely hidden behind "private" everything. I've also never had to custom draw the entire Combobox. I've only done the drop down list and that turned out to be pretty easy. Why are you trying to custom paint the entire thing? Dave Kreskowiak Microsoft MVP - Visual Basic
-
OK. I see what you're looking at. You're seeing the TextBox being drawn by the underlying ComboBox class. The bad news is, as far as I can tell, you can't get rid of it and can't override it. It's completely hidden behind "private" everything. I've also never had to custom draw the entire Combobox. I've only done the drop down list and that turned out to be pretty easy. Why are you trying to custom paint the entire thing? Dave Kreskowiak Microsoft MVP - Visual Basic
Why? I ask myself that question all the time. The answer is the same old story of having all controls behave consistently to the color scheme that has been chosen. Sometimes you can't choose your battles, you just have to fight. I have a few more hacks to try in the meantime. --Ian;
-
Why? I ask myself that question all the time. The answer is the same old story of having all controls behave consistently to the color scheme that has been chosen. Sometimes you can't choose your battles, you just have to fight. I have a few more hacks to try in the meantime. --Ian;
I have no idea how to get past this one. If it wasn't such a PITA composite control, it'd be real easy! Dave Kreskowiak Microsoft MVP - Visual Basic