Hi Mamatha
The standard .NET Framework ComboBox does not support an icon in the text
box section of the control. This article presents a class which can be used
to show an icon, a control or a custom-drawn image into any TextBox or
ComboBox (VB.NET and C# code provided).
Making Space in a TextBox
One feature of the Windows text box control which isn't exposed in any way
in the .NET Framework is the ability to set a left or right margin to the
control using the EM_SETMARGINS message. By taking advantage of this you
can move the text over and therefore make space for an object on the left
or right hand side of the control. The same technique can also be applied
to a combo box control with the DropDown style, since internally that
includes a standard TextBox. All that needs to be done to set the margins
is to find the handle to the control.
This article presents a TextBoxMarginCu stomise class which provides both
the ability to set the margins on any control along with a series of
methods which make drawing into the new margin area or placing a control
over it straightforward .
Using the Class
The simplest thing you can do with the class is to set the margin of a text
box. Various static methods are exposed to set the margin on either a class
(or class derived from) the Framework TextBox and ComboBox classes:
// Set the near margin of comboBox1:
TextBoxMarginCu stomise.NearMar gin(comboBox1, 16);
// Set the far margin of textBox1
TextBoxMarginCu stomise.FarMarg in(textBox, 4);
Note that if you change the font of the control you will need to reset the
margin.
If you want to use the class to place something into the margin then you'll
need to create an instance of the class and use one of the three modes.
Drawing an Icon from an ImageList
Using this method will cause the margin to be automatically set from the
size of the images in the ImageList, and setting the Icon property to the
zero-based index of the image in the ImageList control will display the
icon. To blank the area, set the Icon property to -1.
using vbAccelerator.C omponents.Contr ols;
...
private TextBoxMarginCu stomise textBox1Icon;
...
// Assign a text box customiser to show an
// icon in the near margin of textBox1:
textBox1Icon = new TextBoxMarginCu stomise();
textBox1Icon.Im ageList = ilsIcons;
textBox1Icon.Ic on = 1;
textBox1Icon.At tach(textBox1);
Showing a Control
This is performed in the same way as setting an icon, except that you use
the Control property to assign the control:
private TextBoxMarginCu stomise comboBox3Icon;
// Set to display a CheckBox control in the near
// margin of a combo box:
comboBox3Icon = new TextBoxMarginCu stomise();
comboBox3Icon.C ontrol = checkBox2;
comboBox3Icon.A ttach(comboBox3 );
Drawing a Customised Image
To customise painting you need to create a class which implements the
ITextBoxMarginC ustomisePainter interface. Then you can connect the class
using the CustomPainter property.
Implementing the ITextBoxMarginC ustomisePainter interface is a matter of
providing a concrete implementation of the GetMarginWidth and Draw methods:
public class MyMarginPainter : ITextBoxMarginC ustomisePainter
{
///
/// Called to obtain the width of the margin.
///
/// Width of the margin
public int GetMarginWidth( )
{
return 18;
}
///
/// Called whenever the margin area needs to
/// be repainted.
///
/// Graphics object to paint on.
/// Boundary of margin area.
/// Whether the control is right to left
/// or not
public void Draw(Graphics gfx, Rectangle rcDraw, bool rightToLeft)
{
// example fills the rectangle with the highlight colour
gfx.FillRect(Sy stemBrushes.Hig hlight, rcDraw);
}
}
Once done you attach a new instance of the class:
private TextBoxMarginCu stomise customPaint;
customPaint = new TextBoxMarginCu stomise();
customPaint.Cus tomPainter = new MyMarginPainter ();
customPaint.Att ach(textBox4);
Implementation Details
There are two parts to the implementation of TextBoxMarginCu stomise. The
first is the functionality which allows the margins to be set, and the
second is functionality to draw onto the TextBox.
1. Setting Margins
The margin setting message in the Windows API allows the left or right
margin of the text box to be set. However, to be consistent with the rest
of the .NET Framework the class exposes these as Near and Far margin
properties, which means it needs to know whether the Window is right to
left or not. In this class this is done by interrogating the Windows Styles
but it could equally be done using the standard Framework properties.
The margin message itself takes a single parameter with both left and right
margin values combined into a single long value. The LoWord contains the
left margin and the HiWord contains the right margin. This code
demonstrates setting the Far margin for a TextBox with a given handle
(setting the Near margin is a matter of reversing the margin flag and
shift):
[DllImport("user 32", CharSet=CharSet .Auto)]
private extern static int SendMessage(
IntPtr hwnd,
int wMsg,
int wParam,
int lParam);
private const int EC_LEFTMARGIN = 0x1;
private const int EC_RIGHTMARGIN = 0x2;
private const int EC_USEFONTINFO = 0xFFFF;
private const int EM_SETMARGINS = 0xD3;
private const int EM_GETMARGINS = 0xD4;
[DllImport("user 32", CharSet=CharSet .Auto)]
private extern static int GetWindowLong(
IntPtr hWnd,
int dwStyle);
private const int GWL_EXSTYLE = (-20);
private const int WS_EX_RIGHT = 0x00001000;
private const int WS_EX_LEFT = 0x00000000;
private const int WS_EX_RTLREADIN G = 0x00002000;
private const int WS_EX_LTRREADIN G = 0x00000000;
private const int WS_EX_LEFTSCROL LBAR = 0x00004000;
private const int WS_EX_RIGHTSCRO LLBAR = 0x00000000;
private static bool IsRightToLeft(I ntPtr handle)
{
int style = GetWindowLong(h andle, GWL_EXSTYLE);
return (
((style & WS_EX_RIGHT) == WS_EX_RIGHT) ||
((style & WS_EX_RTLREADIN G) == WS_EX_RTLREADIN G) ||
((style & WS_EX_LEFTSCROL LBAR) == WS_EX_LEFTSCROL LBAR));
}
private static void FarMargin(IntPt r handle, int margin)
{
int message = IsRightToLeft(h andle) ? EC_LEFTMARGIN :
EC_RIGHTMARGIN;
if (message == EC_LEFTMARGIN)
{
margin = margin & 0xFFFF;
}
else
{
margin = margin * 0x10000;
}
SendMessage(han dle, EM_SETMARGINS, message, margin);
}
To make the class work with ComboBoxes, we need to be able to find the
handle to the TextBox within the ComboBox. This is done using the Windows
FindWindowsEx API call, using the class name of the TextBox (which is
"EDIT"):
[DllImport("user 32", CharSet=CharSet .Auto)]
private extern static IntPtr FindWindowEx(
IntPtr hwndParent,
IntPtr hwndChildAfter,
[MarshalAs(Unman agedType.LPTStr )]
string lpszClass,
[MarshalAs(Unman agedType.LPTStr )]
string lpszWindow);
///
/// Gets the handle of the TextBox contained within a
/// ComboBox control.
///
/// The ComboBox window handle.
/// The handle of the contained text box.
public static IntPtr ComboEdithWnd(I ntPtr handle)
{
handle = FindWindowEx(ha ndle, IntPtr.Zero, "EDIT", "\0");
return handle;
}
///
/// Sets the far margin of a TextBox control or the
/// TextBox contained within a ComboBox.
///
/// The Control to set the far margin
/// for
/// New far margin in pixels, or -1
/// to use the default far margin.
public static void FarMargin(Contr ol ctl, int margin)
{
IntPtr handle = ctl.Handle;
if
(typeof(System. Windows.Forms.C omboBox).IsAssi gnableFrom(ctl. GetType()))
{
handle = ComboEdithWnd(h andle);
}
FarMargin(handl e, margin);
}
2. Drawing Onto the TextBox
This functionality is achieved by derived from NativeWindow, which allows
access to the message stream for the TextBox window the class is attached
to. The TextBox is a slightly tricky control because it repaints itself in
response to many messages and not just the standard WM_PAINT message.
Here's the WndProc override:
private const int WM_PAINT = 0xF;
private const int WM_SETFOCUS = 0x7;
private const int WM_KILLFOCUS = 0x8;
private const int WM_SETFONT = 0x30;
private const int WM_MOUSEMOVE = 0x200;
private const int WM_LBUTTONDOWN = 0x201;
private const int WM_RBUTTONDOWN = 0x204;
private const int WM_MBUTTONDOWN = 0x207;
private const int WM_LBUTTONUP = 0x202;
private const int WM_RBUTTONUP = 0x205;
private const int WM_MBUTTONUP = 0x208;
private const int WM_LBUTTONDBLCL K = 0x203;
private const int WM_RBUTTONDBLCL K = 0x206;
private const int WM_MBUTTONDBLCL K = 0x209;
private const int WM_KEYDOWN = 0x0100;
private const int WM_KEYUP = 0x0101;
private const int WM_CHAR = 0x0102;
///
/// Calls the base WndProc and performs WM_PAINT
/// processing to draw the icon if necessary.
///
/// Windows Message
protected override void WndProc(ref Message m)
{
base.WndProc(re f m);
if (this.control == null)
{
switch (m.Msg)
{
case WM_SETFONT:
setMargin();
break;
case WM_PAINT:
RePaint();
break;
case WM_SETFOCUS:
case WM_KILLFOCUS:
RePaint();
break;
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
RePaint();
break;
case WM_LBUTTONUP:
case WM_RBUTTONUP:
case WM_MBUTTONUP:
RePaint();
break;
case WM_LBUTTONDBLCL K:
case WM_RBUTTONDBLCL K:
case WM_MBUTTONDBLCL K:
RePaint();
break;
case WM_KEYDOWN:
case WM_CHAR:
case WM_KEYUP:
RePaint();
break;
case WM_MOUSEMOVE:
// Only need to process if a mouse button is down:
if (!m.WParam.Equa ls(IntPtr.Zero) )
{
RePaint();
}
break;
}
}
else
{
switch (m.Msg)
{
case WM_PAINT:
moveControl();
break;
}
}
}
This loop instructs the control to repaint for most messages; it also
ensures that the margin is correctly set when the edit control's font
changes (the WM_SETFONT message) and, if a control is being placed in the
margin, calls a positioning routine whenever the item is painted. This
positioning routine checks whether the control is in the right place and
moves it if not.
To actually perform the painting, the code needs to get a Graphics object
to draw on. This is performed using the API to obtain a hDC for the control
and then converting that to a managed Graphics object:
[StructLayout(La youtKind.Sequen tial)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user 32")]
private extern static IntPtr GetDC(
IntPtr hwnd);
[DllImport("user 32")]
private extern static int ReleaseDC (
IntPtr hwnd,
IntPtr hdc);
[DllImport("user 32")]
private extern static int GetClientRect (
IntPtr hwnd,
ref RECT rc);
...
GetClientRect(t his.Handle, ref rcClient);
bool rightToLeft = IsRightToLeft(t his.Handle);
IntPtr handle = this.Handle;
IntPtr hdc = GetDC(handle);
Graphics gfx = Graphics.FromHd c(hdc);
// ... Drawing here ...
gfx.Dispose();
ReleaseDC(handl e, hdc);
it is the article on the link
http://www.vbaccelerator.com/home/NE...d_ComboBox/Tex
tBox_Icon/article.asp
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC