Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

windows form how to add an icon to the right or left of a text box

I am building a Windows form application and I have a text box for searching purposes.

I would like to put a search icon inside the text box, at the right or left

like this:

enter image description here

I would prefer at the right

Update 1

I am asking about Windows forms not ASP.net or MVC

like image 977
Agnieszka Polec Avatar asked Aug 08 '14 10:08

Agnieszka Polec


People also ask

How do I add an icon to Windows form?

On the menu bar, choose Project > Properties. When the Project Designer appears, choose the Application tab. (Visual Basic)—In the Icon list, choose an icon (. ico) file.

How do I make my windows form more attractive in C#?

Try using Application. EnableVisualStyles() . Make sure to call it before Application. Run() .

How do I add a TextBox in Visual Studio 2022?

To add a button and a text box Verify that the document is open in the Visual Studio designer. From the Common Controls tab of the Toolbox, drag a TextBox control to the document.

How do I add text to Windows form?

Step 1: Create a windows form. Step 2: Drag the TextBox control from the ToolBox and Drop it on the windows form. You can place TextBox anywhere on the windows form according to your need. Step 3: After drag and drop you will go to the properties of the TextBox control to set the Text property of the TextBox.


2 Answers

You can use a Panel, a TextBox and a PictureBox.

enter image description here

The TextBox must be placed in a Panel so you can't write over your search picture.

like image 159
tezzo Avatar answered Oct 13 '22 00:10

tezzo


You can create a new UserControl which will do the required job. You have to extend the TextBox class for that. Look at the code below:

    public class IconTextBox : System.Windows.Forms.TextBox
    {
        public IconTextBox() : base() { SetStyle(System.Windows.Forms.ControlStyles.UserPaint, true); this.Multiline = true; }

        public System.Drawing.Bitmap BitmapImage
        {
            set;
            get;
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            base.OnPaint(e);
            System.Drawing.Image img = BitmapImage as System.Drawing.Image;
            e.Graphics.DrawImage(img, new System.Drawing.Point(this.Width - (img.Width), 0));

        }

    }

And in the OnPaint method you can specify the image. Also you can extend this to have a custom property which can be the image path. Your choice.

like image 25
Atanas Desev Avatar answered Oct 12 '22 23:10

Atanas Desev