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

How to display icon in textbox in VB.NET

Hi

I want to add an icon to the textbox's text.
I don't know how to display icon in textbox in VB.NET.
If any one knows please let me know.
Thanks in advance.
Mamatha
Nov 21 '05 #1
2 19765
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 TextBoxMarginCustomise 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:
TextBoxMarginCustomise.NearMargin(comboBox1, 16);
// Set the far margin of textBox1
TextBoxMarginCustomise.FarMargin(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.Components.Controls;

...

private TextBoxMarginCustomise textBox1Icon;

...

// Assign a text box customiser to show an
// icon in the near margin of textBox1:
textBox1Icon = new TextBoxMarginCustomise();
textBox1Icon.ImageList = ilsIcons;
textBox1Icon.Icon = 1;
textBox1Icon.Attach(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 TextBoxMarginCustomise comboBox3Icon;

// Set to display a CheckBox control in the near
// margin of a combo box:
comboBox3Icon = new TextBoxMarginCustomise();
comboBox3Icon.Control = checkBox2;
comboBox3Icon.Attach(comboBox3);

Drawing a Customised Image
To customise painting you need to create a class which implements the
ITextBoxMarginCustomisePainter interface. Then you can connect the class
using the CustomPainter property.

Implementing the ITextBoxMarginCustomisePainter interface is a matter of
providing a concrete implementation of the GetMarginWidth and Draw methods:

public class MyMarginPainter : ITextBoxMarginCustomisePainter
{
///
/// 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(SystemBrushes.Highlight, rcDraw);
}
}

Once done you attach a new instance of the class:

private TextBoxMarginCustomise customPaint;

customPaint = new TextBoxMarginCustomise();
customPaint.CustomPainter = new MyMarginPainter();
customPaint.Attach(textBox4);

Implementation Details
There are two parts to the implementation of TextBoxMarginCustomise. 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("user32", 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("user32", 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_RTLREADING = 0x00002000;
private const int WS_EX_LTRREADING = 0x00000000;
private const int WS_EX_LEFTSCROLLBAR = 0x00004000;
private const int WS_EX_RIGHTSCROLLBAR = 0x00000000;

private static bool IsRightToLeft(IntPtr handle)
{
int style = GetWindowLong(handle, GWL_EXSTYLE);
return (
((style & WS_EX_RIGHT) == WS_EX_RIGHT) ||
((style & WS_EX_RTLREADING) == WS_EX_RTLREADING) ||
((style & WS_EX_LEFTSCROLLBAR) == WS_EX_LEFTSCROLLBAR));
}

private static void FarMargin(IntPtr handle, int margin)
{
int message = IsRightToLeft(handle) ? EC_LEFTMARGIN :
EC_RIGHTMARGIN;
if (message == EC_LEFTMARGIN)
{
margin = margin & 0xFFFF;
}
else
{
margin = margin * 0x10000;
}
SendMessage(handle, 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("user32", CharSet=CharSet.Auto)]
private extern static IntPtr FindWindowEx(
IntPtr hwndParent,
IntPtr hwndChildAfter,
[MarshalAs(UnmanagedType.LPTStr)]
string lpszClass,
[MarshalAs(UnmanagedType.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(IntPtr handle)
{
handle = FindWindowEx(handle, 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(Control ctl, int margin)
{
IntPtr handle = ctl.Handle;
if
(typeof(System.Windows.Forms.ComboBox).IsAssignabl eFrom(ctl.GetType()))
{
handle = ComboEdithWnd(handle);
}
FarMargin(handle, 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_LBUTTONDBLCLK = 0x203;
private const int WM_RBUTTONDBLCLK = 0x206;
private const int WM_MBUTTONDBLCLK = 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(ref 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_LBUTTONDBLCLK:
case WM_RBUTTONDBLCLK:
case WM_MBUTTONDBLCLK:
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.Equals(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(LayoutKind.Sequential)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}

[DllImport("user32")]
private extern static IntPtr GetDC(
IntPtr hwnd);
[DllImport("user32")]
private extern static int ReleaseDC (
IntPtr hwnd,
IntPtr hdc);
[DllImport("user32")]
private extern static int GetClientRect (
IntPtr hwnd,
ref RECT rc);

...

GetClientRect(this.Handle, ref rcClient);
bool rightToLeft = IsRightToLeft(this.Handle);

IntPtr handle = this.Handle;
IntPtr hdc = GetDC(handle);
Graphics gfx = Graphics.FromHdc(hdc);

// ... Drawing here ...

gfx.Dispose();
ReleaseDC(handle, 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

Nov 21 '05 #2

Hi Mohamoss,

Thank you for gave reply.
I tested ur code but it is asking about reference for
TextBoxMarginCustomise.How can i give reference and which reference is
supported this one.
No problem to display an image in either textbox or in combobox in
VB.NET.
I will wait for ur reply.
Thank you

Mamatha
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #3

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

Similar topics

2
by: Steve Le Monnier | last post by:
My user interface design requires that ErrorProviders are used for errors but also to indicate warnings to the users as well. In order to provide a clear visual clue between errors and warnings its...
5
by: Martin Moser | last post by:
Does somebody knows, if theres a way to display any file (tiff, doc, pdf, .....) "inline" on an asp.net site? What I want to do is, to display a file, which is stored in the DB, and some...
5
by: Anders K. Olsen | last post by:
Hello group I'm developing a file application, but I have run into a problem that I hope you can help with. My application lists a number of files (names). Now I would like to display the...
2
by: Mamatha | last post by:
Hi I want to display an icon in combobox or in textbox in VB.NET whatever it may be.I want to display like URL bar in Microsoft Internet Explorer.If any one knows please let me know thanks in...
2
by: Liu Feng | last post by:
How to display a 32bits icon in pictureBox or button? It can be displayed correct in toolbar, but not work in pictureBox and button. Who can help me? -- Thanks, LiuFeng
6
by: Franz | last post by:
hello, I currently develop an application which runs in background. I would like to put at the place of the notifyicon, a label, which displays information about this application. Any Ideas?...
0
by: missshhhh | last post by:
Hi i have tried to display an icon from the imageList to a button and it worked. I first configured the button by: openButton.imageList = myImageList But now I need to display an icon to a cell in...
3
by: Ian | last post by:
I have a form with a sub form, on the continues sub for I want to display some data along with a picture, on the On Current event I have the code: .Picture = Nz() is the name of the picture...
2
by: plevintampabay | last post by:
I have a borderless form in C#. The form has an icon specified in its properties and I also have the same icon specified for the project in the project properties. When I had a border on the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.