How to make ComboBox look like Fixed2D BorderStyle?

How to make ComboBox look like Fixed2D BorderStyle?

Post by RGF2ZSBMZW » Thu, 22 Mar 2007 06:05:11


I am developing a Windows Forms-based application using VS 2003 in C#.

I have several TextBox and ComboBox controls on a form. For a particular
scenario, I need both control types to have the same appearance. The desired
appearance is like that of a TextBox whose BorderStyle is set to FixedSingle.
This allows the controls to be arranged to look very much like a table.
(No, I cannot use a DataGrid or other similar control for various reasons.)

Is there a way to make the ComboBox look like the FixedSingle TextBox?
Does .NET 2.0 provide this capability?

I only need the ComboBox to have this appearance when the DropDownStyle is
set to Simple (no DropDown behavior).

Thanks,
Dave
--
Dave Leach
Agilent Technologies, Inc.
 
 
 

How to make ComboBox look like Fixed2D BorderStyle?

Post by v-lli » Thu, 22 Mar 2007 16:00:38

i Dave,

Thank you for posting here.

I noticed that you have posted the same question in this newsgroup, which I
have already responded. So please check my answer there and if you need any
further assistance on this particular issue, please reply to me in that
thread so I can follow up with you in time.

For your convenience, I have included my reply as follows:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - -
Based on my understanding, you'd like a ComboBox to have a FixedSingle
border appearance like a TextBox. If I'm off base, please feel free to let
me know.

I think a possible way is to derive the ComboBox and override the WndProc
method. In the override WndProc method, catch the WM_PAINT message and draw
the fixed single border of the derived ComboBox.

The following is is the sample code.

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;

public class FlatComboBox: ComboBox
{
public const int WM_PAINT = 0xF;
[DllImport("user32")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);

[DllImport("user32")]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
public FlatComboBox()
: base()
{
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.IntegralHeight = false;
this.DropDownStyle = ComboBoxStyle.Simple;
}

protected override void WndProc(ref Message m)
{
IntPtr hDC = IntPtr.Zero;
Graphics gdc = null;
switch (m.Msg)
{
case WM_PAINT:
base.WndProc(ref m);
hDC = GetWindowDC(this.Handle);
gdc = Graphics.FromHdc(hDC);
PaintFlatControlBorder(this, gdc);
ReleaseDC(m.HWnd, hDC);
gdc.Dispose();

break;
default:
base.WndProc(ref m);
break;
}
}
private void PaintFlatControlBorder(Control ctrl, Graphics g)
{
Rectangle rect1 = new Rectangle(0, 0, ctrl.Width, 20);
Rectangle rect2 = new Rectangle(0, 21, ctrl.Width, ctrl.Height - 21);

ControlPaint.DrawBorder(g, rect1, Color.Black, ButtonBorderStyle.Solid);
ControlPaint.DrawBorder(g, rect2, Color.Black,
ButtonBorderStyle.Solid);
}
}

Use the derived ComboBox instead of the standard ComboBox on your form.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - -

Thank you and have a nice day!

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution.