473,394 Members | 1,870 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,394 software developers and data experts.

Inherit ComboBox / Windows Vista

Why when I inherit a ComboBox and make no code changes, when I set the new
control DropDownStyle to dropDownList it appears different than the standard
ComboBox control on Vista?

In Windows Vista when this mode is set, the control is all one color
indicating that you can't type in it and looks really slick. How do i
reproduce this behavior?

Apr 21 '07 #1
6 5575
Nathan Laff wrote:
Why when I inherit a ComboBox and make no code changes, when I set the
new control DropDownStyle to dropDownList it appears different than the
standard ComboBox control on Vista?

In Windows Vista when this mode is set, the control is all one color
indicating that you can't type in it and looks really slick. How do i
reproduce this behavior?
Are you overriding or modifying any behavior. Just tried it here with
my own combobox inheriting from System.Windows.Forms.ComboBox and the
display renders exactly the same as a System.Windows.Forms.ComboBox when
the DropDownStyle is set to DropDownList.
--
Tom Porterfield
Apr 21 '07 #2
Are you overriding or modifying any behavior. Just tried it here with my
own combobox inheriting from System.Windows.Forms.ComboBox and the display
renders exactly the same as a System.Windows.Forms.ComboBox when the
DropDownStyle is set to DropDownList.
Ok, here is what happened, I forgot to override OnPaint and pass in
base.OnPaint.

So that fixed the problem of a blank control not working. However I am doing
DrawMode of OwnerDrawFixed.
Basically it's drawing images next to my comboBox items. Any idea how to
maintain the look that it should, while drawing these images?

Apr 21 '07 #3
Basically it's drawing images next to mycomboBoxitems. Any idea how to
maintain the look that it should, while drawing these images?
I'm having the same issue with Vista, here's the quick code I posted
trying to get help(maybe someone in this group has some better
suggestions).

public class ColoredComboBox : ComboBox
{
public ColoredComboBox()
{
this.DrawMode = DrawMode.OwnerDrawFixed;
this.DrawItem += new
DrawItemEventHandler(ColoredComboBox_DrawItem);
}
void ColoredComboBox_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
// don't even care about text, my icon, or anything else at this
point, just want the background to work.
}
}

May 1 '07 #4
Here's what it comes down to. The Windows shell developers screwed us...

If you want to do this you have to handle WM_PAINT and paint the thing
yourself using DrawThemeBackground.

Here's the long explanation of it:

http://www.eggheadcafe.com/software/...st-with-c.aspx
<nx*****@winvoice.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
>Basically it's drawing images next to mycomboBoxitems. Any idea how to
maintain the look that it should, while drawing these images?

I'm having the same issue with Vista, here's the quick code I posted
trying to get help(maybe someone in this group has some better
suggestions).

public class ColoredComboBox : ComboBox
{
public ColoredComboBox()
{
this.DrawMode = DrawMode.OwnerDrawFixed;
this.DrawItem += new
DrawItemEventHandler(ColoredComboBox_DrawItem);
}
void ColoredComboBox_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
// don't even care about text, my icon, or anything else at this
point, just want the background to work.
}
}
May 2 '07 #5
If you want to do this you have to handle WM_PAINT and paint the thing
yourself using DrawThemeBackground.
Here's the long explanation of it:
http://www.eggheadcafe.com/software/...dropdown-list-...
Sure would be nice if the shell developers provided some code as an
example of how to work around the situation they put us in. Maybe
then they'd understand and release a patch rather than the hack that
by their own admission may not work on future releases of windows.

The combobox seems to always get the shaft, the project I'm upgrading
was from .NET v1.x and I'm cutting out all the hacks that where put in
place to allow windows 95 style auto-complete to now find that I get
to put in another series of hacks until v3.5 or whenever.

May 2 '07 #6
Here's what I put together over the last few hours of trial and error
and a bunch of searching. I originally was using DrawThemeBackground
and so forth but eventually dumped it and used the easier
VisualStyleRender's(which are just wrappers, I know, but at least they
are included). Its got a lot of specific stuff to the app I'm using
but should at least give someone a lot of cut-n-paste if they find
this topic in the future.
public class ColoredComboBox : ComboBox
{
#region Private Members
private bool IsVista;
private ComboBoxState cb_State = ComboBoxState.Normal;
#endregion
#region Public Members
private Image ListaImg1 = null;
public Image ImageData1
{
set
{
ListaImg1 = value;
if (value != null)
{
if (this.DrawMode !=
System.Windows.Forms.DrawMode.OwnerDrawFixed)
{
this.DrawMode =
System.Windows.Forms.DrawMode.OwnerDrawFixed;
if (this.DropDownStyle !=
ComboBoxStyle.DropDownList)
this.DropDownStyle =
ComboBoxStyle.DropDownList;
}
}
}
get { return (ListaImg1); }
}
private Image ListaImg2 = null;
public Image ImageData2
{
set
{
ListaImg2 = value;
if (value != null)
{
if (this.DrawMode !=
System.Windows.Forms.DrawMode.OwnerDrawFixed)
{
this.DrawMode =
System.Windows.Forms.DrawMode.OwnerDrawFixed;
if (this.DropDownStyle !=
ComboBoxStyle.DropDownList)
this.DropDownStyle =
ComboBoxStyle.DropDownList;
}
}
}
get { return (ListaImg2); }
}
#endregion
#region Constructor
public ColoredComboBox()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
IsVista = ((Environment.OSVersion.Version >= new
Version("6.0.0")) && (Environment.OSVersion.Platform ==
PlatformID.Win32NT));
this.DropDownStyle = ComboBoxStyle.DropDownList;
this.FlatStyle = FlatStyle.System;
this.DrawMode =
System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.DrawItem += new
DrawItemEventHandler(ColoredComboBox_DrawItem);
}
#endregion
#region DrawIcon
private Rectangle DrawIcon(Graphics g,Rectangle r, object
iconData1, object iconData2)
{
Rectangle newBounds = r;
if (iconData2 != null && iconData1 != null)
{
int x = r.X;
int w = this.Height - 4;
newBounds.X += w;
newBounds.Width -= w;
if (iconData2.ToString() == "True" && ImageData2 !=
null) // draw icon
{
w -= 4;
g.DrawImage(ImageData2, r.X + 1, r.Y + 1, w, w);
}
else if (iconData1.ToString() == "True" && ImageData1 !
= null) // draw icon
{
w -= 4;
g.DrawImage(ImageData1, r.X + 1, r.Y + 1, w, w);
}
}
return (newBounds);
}
#endregion
#region DrawItem
protected void ColoredComboBox_DrawItem(object sender,
DrawItemEventArgs e)
{
e.DrawBackground();
DrawItemText(e.Graphics, e.Bounds, e.Index);
e.DrawFocusRectangle();
}
private void DrawItemText(Graphics g,Rectangle r,int Index)
{
if (Index == -1 || Items.Count == 0)
return;
Color c = ForeColor;
Descriptor d = new Descriptor(Items[Index].ToString(),
Items[Index].ToString(), null, null, null);
try
{
d = (Descriptor)Items[Index];
if (d.Data3 != null)
c = (Color)d.Data3;
}
catch { }
SolidBrush b = new SolidBrush(c);
g.DrawString(d.Name, this.Font, b, DrawIcon(g,r, d.Data1,
d.Data2));
b.Dispose();
}
private void DrawArrow(Graphics g, Rectangle rect)
{
Point p1 = new Point(rect.X + 3, rect.Y + rect.Height / 2
- 2);
Point p2 = new Point(rect.X + rect.Width - 3, rect.Y +
rect.Height / 2 - 2);
Point p3 = new Point(rect.X + rect.Width / 2, rect.Y +
rect.Height / 2 + 2);
g.FillPolygon(new
SolidBrush(System.Drawing.SystemColors.ControlText ), new Point[]{p1,
p2, p3});
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);

if (Enabled == false)
cb_State=ComboBoxState.Disabled;
Rectangle r = e.ClipRectangle;
if (IsVista)
{
PushButtonState pb_State = PushButtonState.Normal;
if (cb_State == ComboBoxState.Disabled)
pb_State = PushButtonState.Disabled;
else if (cb_State == ComboBoxState.Hot)
pb_State = PushButtonState.Hot;
else if (cb_State == ComboBoxState.Pressed)
pb_State = PushButtonState.Pressed;
ButtonRenderer.DrawButton(e.Graphics, r,pb_State);
r = new Rectangle(r.Right -
System.Windows.Forms.SystemInformation.VerticalScr ollBarWidth+2,
e.ClipRectangle.Top,
System.Windows.Forms.SystemInformation.VerticalScr ollBarWidth-4,
e.ClipRectangle.Height);
DrawArrow(e.Graphics, r);
}
else
{
ComboBoxRenderer.DrawTextBox(e.Graphics, r, cb_State);
r = e.ClipRectangle;
r = new Rectangle(r.Right -
System.Windows.Forms.SystemInformation.VerticalScr ollBarWidth,
e.ClipRectangle.Top,
System.Windows.Forms.SystemInformation.VerticalScr ollBarWidth,
e.ClipRectangle.Height);
ComboBoxRenderer.DrawDropDownButton(e.Graphics, r,
cb_State);
}
r = e.ClipRectangle;
r.Inflate(0, -2);
r.Offset(0, 1);
DrawItemText(e.Graphics,r,SelectedIndex);
}
#endregion
#region Mouse Movement
// Draw the smaller pressed button image.
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
cb_State = ComboBoxState.Pressed;
Invalidate();
}

// Draw the button in the hot state.
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
cb_State = ComboBoxState.Hot;
Invalidate();
}

// Draw the button in the unpressed state.
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
cb_State = ComboBoxState.Normal;
Invalidate();
}

// Draw the button hot if the mouse is released on the
button.
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
OnMouseEnter(e);
}

// Detect when the cursor leaves the button area while
// it is pressed.
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);

// Detect when the left mouse button is down and
// the cursor has left the pressed button area.
if ((e.Button & MouseButtons.Left) == MouseButtons.Left &&
!ClientRectangle.Contains(e.Location) &&
cb_State == ComboBoxState.Pressed)
{
OnMouseLeave(e);
}
}

#endregion
}

May 3 '07 #7

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

Similar topics

0
by: Jax | last post by:
I am using a class that inherits from the DataGridTextBoxColumn. It adds a combo box into the column where it displays a selection of choices. The problem I have is that when this comboBox loses...
1
by: Spurry Moses | last post by:
Hi, I'm converting a project from C++Builder to C# and I just need to copy my class design across. It's looking good so far. To stick with the design I want to inherit from a ComboBox control...
6
by: dbuchanan | last post by:
VS2005 I've been reading all the help I can on the topic (MSDN, other) but I can't make sense of this. Desired behavior; The user is to choose from the displayed list of the databound combobox...
9
by: salad | last post by:
Due to an earlier posting I read in this newsgroup regarding Office 2007 beta, I downloaded it. After I DL'd it, I got an invitation from MS to get WinVista. I am now wondering if, since both...
6
by: j2ee.singh | last post by:
Hi, I'm looking to buy a new laptop primarily to learn & practice .NET and C#. My Question is: Is there any requirement for .NET and C# in terms of the following Operating Systems: -...
4
by: =?Utf-8?B?Q2FybGFKYWRl?= | last post by:
Can anyone help me? I need to use picture it 2000 every day and use to use it on Windows XP and had NO problems at all. If I would open up windows explorer i could be in my folder that has all of...
10
by: Academia | last post by:
I'm making my own ComboBox and would like the DropDown button to look like the system combo box. Do you know the Font for the "v" that is used in the regular combobox. Is that the letter vee...
2
by: Andrus | last post by:
SWF DataGridView contains combobox control. Activating combobox and pressing F4 to open dropdown menu causes strange NRE (see below). When I enter some valid value to combobox, press tab,...
1
by: Frank van Eijkelenburg | last post by:
Hi, I have to develop an application which must run on .net 2.0 on a Windows XP SP2 and Windows Vista Business OS. Is .net 2.0 supported at Windows Vista? If I see the supported OS at the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.