Link to home
Start Free TrialLog in
Avatar of ANINDYA
ANINDYAFlag for India

asked on

Image in Textbox and corner Icon

Experts
How to set an image at the textbox in Winform application (c#)
and also how to set custom image at the left corner of the winform.
For more clarification please see the attached image.
Thanking you
Anindya.bmp
Avatar of Dennis Aries
Dennis Aries
Flag of Netherlands image

Ok, not that difficult ;)

To set the image/icon in the topleft corner of the application, set the 'icon'-property of the desired winform.


As for the image in the textbox, You will have to create a custom control to create that. Are you up for that?
Avatar of ANINDYA

ASKER

Expert djjaries
you want to say I have to create a custom textbox for image in the textbox ?
if it is only solution then I do not know how to do it.
Thanking you,
I think for the textbox you can write a class with background image and set the class and see it must work  
For question part how to set custom image at the left corner of the winform.:

http://niitdeveloper.blogspot.com/2010/08/how-to-set-icons-for-window-forms.html
Avatar of ANINDYA

ASKER

expert Vs00saini
your code for the form icon is perfect .
Can you please provide me an option for the image for a textbox.
Thanking you,
apply this class to the textbox,
in the class give your search image path
.textboximg{
width:215px;
height:21px;
border:0px solid ;
background:url(../images/search.gif) no-repeat;
font:12px Arial, Helvetica, sans-serif;
color:#000;
font-weight:normal;
}

Open in new window

Found an link:

http://www.codeproject.com/KB/edit/csexrichtextbox.aspx

that enables you to insert image in richtextbox with some hack. Try if it helps you.

I am seeking some simple solution or alternative. I'll let you know if succeeded.
Following is the code for the new custom textbox:

Add PaintedTextBox Class in your solution.
Add new PaintedTextBox on your Form and set BitmapImage property of PaintedTextBox.


public class PaintedTextBox : System.Windows.Forms.TextBox
    {
        public PaintedTextBox() : 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));
           
        }

    }

Open in new window

And setting the Icon property for form is straight forward

Just set the Icon property of Form.
SOLUTION
Avatar of Vikram Singh Saini
Vikram Singh Saini
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ANINDYA

ASKER

Expert vs00saini
please see the attached image

Anindya.bmp
Avatar of ANINDYA

ASKER

Expert Naman goel
Please see the attached image.
There are some errors are coming.
Thanking you
Anindya.bmp
hey, anindya,

class name and constructor name is different in your code
class name is Class_PaintedTextBox
and constructor name is PaintedTextBox.

for ease of use just copy paste the code I had posted.

or just send me your code.
Avatar of ANINDYA

ASKER

Master Naman Goel
I have not been able to do it .
Please help me.
I am sending you the code and image of the architecture also.
Thanking you,
Anindya

//The code of the form as shown in the image is below


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Presentation.Items;

namespace Presentation
{
    public partial class Form_Items : Form
    {
        public Form_Items()
        {
            InitializeComponent();
            
            this.textBox_subCategory.Click += new EventHandler(textBox_subCategory_Click);
            this.Load+=new EventHandler(Form_Items_Load);
        }
        TextBox tbx = new TextBox();
        void textBox_subCategory_Click(object sender, EventArgs e)
        {
            show_subCategoryForm();
        }

        private void show_subCategoryForm()
        {
            Form_SubCategoryDetails sc = new Form_SubCategoryDetails();
            sc.ShowDialog();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form_Items_Load(object sender, EventArgs e)
        {
            
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void button_longDescription_Click(object sender, EventArgs e)
        {
            get_longDescription();
        }

        private void get_longDescription()
        {
            string desc = string.Empty;
            desc=this.textBox_description.Text.Trim();
            Form_LongDescription ld = new Form_LongDescription(desc);
            ld.ShowDialog();
            textBox_description.Text = ld.Longdescription;
        }

    }
}


//


//Here is the code of the class_paintedtextbox

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Presentation.Common
{
    public class Class_PaintedTextBox:System.Windows.Forms.TextBox
    {
        public Class_PaintedTextBox() : 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));
           
        }

    }
}

Open in new window

Anindya.bmp
Hello Anindya,

(1) Application.StartupPath is folder from where .exe file of application executes i.e. YourAppFolder/bin/Debug.

And in this folder you can create folder for storing images. And you can refer that folder as:

Application.StartupPath + @"\Images\pbar.gif";

Open in new window


OR you can access your own image file as :

 private Bitmap Img = (Bitmap)Image.FromFile(@"E:\Images\myimage.gif");

Open in new window


(2) I saw your screenshot. There you have commented line:

pictureBox1.Size = new System.Drawing.Size(23, 18); // Do not comment it.

However I have uploaded demo application in above link ID:34423357. You can download it too.
Hey,

I am not able to understand the problem in your code as it worked fine for me:

for your ease I am customizing my code according to your naming convention :

Steps you can use for adding the Class_PaintedTextBox on your Form_Items :
1. Open Form_Items in designer mode
2. Open Toolbox and drag and drop Class_PaintedTextBox on your form
3. set the properties for Class_PaintedTextBox
       i. )  set the name property of Class_PaintedTextBox to textBox_subCategory
       ii. ) set BitmageImage property (Select search icon from your local resource)

also I have modified Class_PaintedTextBox a little bit to handle one more scenario



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Presentation
{
    public partial class Form_Items : Form
    {
        private Presentation.Common.Class_PaintedTextBox textBox_subCategory;
    
        public Form_Items()
        {
            InitializeComponent();
        }



        private void InitializeComponent()
        {
            this.textBox_subCategory = new Presentation.Common.Class_PaintedTextBox();
            this.SuspendLayout();
            // 
            // textBox_subCategory
            // 
            this.textBox_subCategory.BitmapImage = global::WindowsFormsApplication1.Properties.Resources.Search;
            this.textBox_subCategory.Location = new System.Drawing.Point(13, 13);
            this.textBox_subCategory.Multiline = true;
            this.textBox_subCategory.Name = "textBox_subCategory";
            this.textBox_subCategory.Size = new System.Drawing.Size(253, 229);
            this.textBox_subCategory.TabIndex = 0;
            // 
            // Form_Items
            // 
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Controls.Add(this.textBox_subCategory);
            this.Name = "Form_Items";
            this.ResumeLayout(false);
            this.PerformLayout();

        }
    }
}


//Here is the code of the class_paintedtextbox

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Presentation.Common
{
    public class Class_PaintedTextBox:System.Windows.Forms.TextBox
    {
        public Class_PaintedTextBox() : 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);

            if (BitmapImage is Bitmap)
            {
                System.Drawing.Image img = BitmapImage as System.Drawing.Image;
                e.Graphics.DrawImage(img, new System.Drawing.Point(this.Width - (img.Width), 0));
            }
           
        }

    }
}

Open in new window

Avatar of ANINDYA

ASKER

Expert Naman Goel
instead of your code
 this.textBox_subCategory.BitmapImage = global::WindowsFormsApplication1.Properties.Resources.Search;
I have used the
this.textBox_new.BitmapImage = (Bitmap)Image.FromFile(Application.StartupPath + @"\Images\search.gif");
 to access the image but I am getting error.
Thanking you
Anindya.bmp
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ANINDYA

ASKER

Expert Naman Goel and vs00saini
thanks a lot for helping me with  the code .
Both of your code is working fine.
Hope to get your cooperation in future too.
Thanking you,
Anindya
Bangalore