473,842 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 19879
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

Nov 21 '05 #2

Hi Mohamoss,

Thank you for gave reply.
I tested ur code but it is asking about reference for
TextBoxMarginCu stomise.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
5909
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 necessary to have two differing icons. I could place two ErrorProviders on my form, but what I would really like to do is switch the Icon on the ErrorProvider control at run-time. But I am having difficulty in understanding the syntax required...
5
1995
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 information around it. If you know a way, or a third party control, please let me know. tia Martin
5
2552
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 proper file icon before I display the file name. E.g. if it is a .doc file, I would like to display the Microsoft Word icon. The icon should depend on which program is associated with the file type.
2
5795
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 advance. Mamatha
2
2111
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
1909
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? Thanks.
0
1083
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 a grid but the output shows System.Drawing.Bitmap instead of the icon. I cannot configure the grid to have an imageList because the grid does not have such property. Please help!
3
5947
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 control, is the name of the text field that holds the path to the jpg. What I want is each record in the sub form to show the associated
2
4392
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 form, the icon was displayed in the task bar. But when I set the border to None, the icon no longer displays. The taskbar button only shows the text name of my program. How can I get the icon to display in the taskbar for a borderless form? ...
0
9871
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9715
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10944
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7855
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7035
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4089
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3144
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.