Share via


How can I use image for the background of textbox instead of a white blank format of it.

Question

Friday, February 3, 2012 6:29 AM

Dear Buddies:

In order to make my textbox beautiful, I really enthusiastic about locating an image for the backgound of my texbox and writing text on that image. I would really be happy if some one could help me to tackle this challenge.

Yours

-- Mohammad Multi-Millionare

All replies (9)

Friday, February 3, 2012 7:30 AM âś…Answered | 2 votes

Hi MohammadMultiMillionare,

 

 You can use native code to use background picture for TextBox control. I will list these code snippet as follows:

 

    class MyTextBox : TextBox
    {
        const int WM_ERASEBKGND = 0x0014;

        private Image backImage;

        [DisplayName("picture")]
        public Image BackImage
        {
            get { return backImage; }
            set { backImage = value; }
        }

        protected void OnEraseBkgnd(Graphics gs)
        {
            gs.FillRectangle(Brushes.White, 0, 0, this.Width, this.Height); //fill in white color in order to be void painting double image
            if (backImage != null) gs.DrawImage(backImage, 0, 0); //draw background
            gs.Dispose();
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_ERASEBKGND) //draw background
            {
                OnEraseBkgnd(Graphics.FromHdc(m.WParam));
                m.Result = (IntPtr)1;
            }
            base.WndProc(ref m);
        }
    }

=========================================

        const int WM_CTLCOLOREDIT = 0x0133;
        const int TRANSPARENT = 0x1;
        const int NULL_BRUSH = 0x5;

        [DllImport("gdi32")]
        static extern int SetBkMode(IntPtr hdc, int bkMode);
        [DllImport("gdi32")]
        static extern int SetTextColor(IntPtr hdc, int color);
        [DllImport("gdi32")]
        static extern IntPtr GetStockObject(int fnobject);

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_CTLCOLOREDIT && m.LParam == myTextBox1.Handle)//Type is EDIT(TextBox)
            {
                SetBkMode(m.WParam, TRANSPARENT);//Set background transparent
                SetTextColor(m.WParam,0xFF);     //font color is red
                m.Result = GetStockObject(NULL_BRUSH);
                return;
            }
            else base.WndProc(ref m);
        }

 I hope it can resolve your problem.

 

Sincerely,

Jason Wang

orichisonic http://blog.csdn.net/orichisonic


Friday, February 3, 2012 6:43 AM

Hello Mohammad  look at the following link

http://www.codeproject.com/Articles/4390/AlphaBlendTextBox-A-transparent-translucent-textbo

Want to add MVP with my name.


Friday, February 3, 2012 7:22 AM

check this solved thread

http://social.msdn.microsoft.com/Forums/ar-SA/winforms/thread/c0b7033b-4925-48b6-9009-d9e8561a789e

http://www.java-forums.org/forum.php


Friday, February 3, 2012 9:17 AM

Dear jason:

 

Thanks for your nice clue. The code is working properly, However, I want the textbox to show my picturebox instead of my desktop backgound. What should I do with that.

 

Yours

-- Mohammad Multi-Millionare


Friday, February 3, 2012 9:40 AM

hi mohammad,

refer the following links, which might help.

http://www.daniweb.com/software-development/csharp/threads/25861

http://stackoverflow.com/questions/1893201/choosing-backgroung-image-for-a-textbox-during-runtime-using-c-asp-net

http://stackoverflow.com/questions/5557365/textbox-with-a-transparent-background

 

Don't forget to mark the post as answer or vote as helpful if it does, Regards - Rajasekhar.R


Friday, February 10, 2012 5:23 AM

Hi MonhammadMultiMillionare,

   You can drag-drop Picturebox control from Toolbox into Windows Form.Consequently,Using the following code snippet to assign the BackgroundImage of this instance of Picturebox control.

pictureBox1.BackgroundImage = this.textBox1.BackImage;

   I hope it will help you resolve your problem.

Sincerely,

Jason Wang

orichisonic http://blog.csdn.net/orichisonic If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


Friday, February 10, 2012 6:55 AM

Are you trying in web/winforms?


Friday, February 10, 2012 7:48 AM

Hi MonhammadMultiMillionare,

  I am sorry I have pasted an error codes. I correct it as follows:

 this.myTextBox1.BackImage = pictureBox1.BackgroundImage;

Sincerely,

Jason Wang

orichisonic http://blog.csdn.net/orichisonic If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


Wednesday, November 27, 2019 9:05 AM

Using Visual Studio 15.9.7 and Windows 7 this solutions do not behave in the required way.

The first solution sets a background image but this is overdrawn with the text and background color. The Image can not be seen.

The second solution with the transparent colour is showing the background of the application, I. e. the desktop. It does not show something behind the textbox in the application like a picturebox that is placed behind the textbox.

So I can't see that these solutions do help in the goal to have a defined image in the background of the textbox.