Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
Hi all,
I am trying to create a customize standard Winform Button for my application in which I can put an image with some text(like Label Control)and also to disable the default behavior of the Standard button.
Any help will be appreciated.


Thanks in Advance
Posted

 
Share this answer
 
Comments
Henry Minute 1-Mar-11 10:12am    
Looks good to me.
Wild-Programmer 2-Mar-11 7:23am    
Thanks Henry
There are some nice CodeProject on custom picture buttons. Read the following articles:
WinForm ImageButton

Oriented Button in Windows Forms

CImageTextButton
 
Share this answer
 
Copy this text, then add a class to your project and replace text in class with this method. Once you have done this rebuild your application and open toolbox and you will have a custom button control called myButton that should show towards the top of your tool box drag and drop the control onto your form and you should see the control on your form.

C#
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

    class myButton : Panel
    {
        public myButton()
        {
            this.Size = new Size(75, 23); // this is just to set size to default button size change as needed
            this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; // makes a border around button
        }
        private Image buttonImage
        {
            get
            {
                string base64string = "iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA1RJREFUeNq0VetLVFEQn3P35aobahpEUtkXQSw2A+1BEPTVPgRiCqsSQn71TwmCyCAMM5IQIZLwSxBIpNGGhRBEYmUPRFvfu3v3nkcz99y9u3dd0Q0aGC7nnDm/md/MnLlMKQWMMSDp+6iCYMAtXHbg8gIeBWEfca5m0PYNKBiXAu4/irIMbdrYWQexuDoGDCZxP1oeBgiX4WU4gDhGqTTATpJQYU4JaH/cyn66DnrmIMjTKo7WzbWH0UaQd60HZADMB2CgrqziQqp5X4idG22BjJ8O0+tygDGjua4WgKcQWNqRgKa5P7jNAtUwAGoi6GSFNfO0HMCdOwadWztmTyiAX8yc4BgAMhCOSrm3ujakeI+TIgZhZRCTsG0GCvzRoF8bQZHUBDGMwUaAxkN63T+7BxuhmRAWMH+U9m0GkosAgUsneo9ipD6WAyd50LbbLss8iyM4D7gOOPIkqtyhKfJSRBc3sS69096Ihy8W2BXe5wJyDITcHXkRjb3yOhm5XNxOs5Hg1kDggjazPX29AaDzFBxIRq8AdL/07lENhePAYSA8ERwUPCtPrurW9jLJS5HgmgEpC8A/iVEGLobWXQyoDgoMnD4Ty6WB98azDJSjwmWga2AJ7RGbmZs+GF8EeLasnz+1nLWND2cT7UwFT68xCPpy4LF3+jy9JvJqIF0GtgMpla46Okgl8JsoHunzDm/+umewLXcI3PJUmBxIje+8ZMwLxwbGeZQ3YLzy4kbIs+58TSMGwFwzCzpIaidGIFcDI1jB8eVha3FMCXdzmFV/ZQgmlnIgHfjozA0c0atJj53gln2fWxY2S3kqlyJIf05nWFPIr5y54mVCdRj5hD2/gMHRa02Sg+2ikZOYmH9lmFjJCu0g+evLFDvR2hRSKZBW2naZP49TifW9W6hgMhqBMsiEwpD89nYKoFWnaOFe/11ubi5uGGEIVFUTPbfw+yrZodIduksYhEWYdozOL9OoOd/Vfvzm0G1f+FBDZSVAJFLaW9jaAtjGrInU5uL34YHBxMzYJGJLlvfTDwWqjradHBjtCtefvuSP1J0pxQHf/jOfWvow/XUoNmat/55teajMeF+OgZtC1HrUI6jVJU4LKhTNgB9nh7HiCPk+6+B/yl8BBgDnEVPWrXYOEgAAAABJRU5ErkJggg==";
                byte[] base64byte = Convert.FromBase64String(base64string);
                MemoryStream base64stream = new MemoryStream(base64byte);
                return Image.FromStream(base64stream);
            }
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            // do mousedown events
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            // do mouseup events
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            DrawButtonImage(e);
            DrawButtonText(e);            
        }
        private void DrawButtonText(PaintEventArgs e)
        {
            e.Graphics.DrawString("button1",this.Font,Brushes.Black,new PointF(25,3));
        }
        private void DrawButtonImage(PaintEventArgs e)
        {
            e.Graphics.DrawImage(buttonImage, 0, 0, 20, 20);
        }
    }
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900