Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C# Winform Button, make the border thicker and different color?

Posted on 2016-10-17
5
73 Views
Last Modified: 2016-10-17
I have a Button that when clicked I would like to change the color of the button as well as the thickness of the border.   Is that possible?

thanks!
0
Comment
Question by:roujesky
  • 2
  • 2
5 Comments
 
LVL 20

Expert Comment

by:Russ Suter
ID: 41846829
It is possible if you subclass the control and handle the paint event manually. Here is the crudest of examples to get you started.
    public class MyButton : Button
    {
        private bool _isClicked = false;

        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            if (_isClicked)
            {
                pevent.Graphics.FillRectangle(Brushes.Red, this.ClientRectangle);
            }
        }

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            this._isClicked = true;
        }
    }

Open in new window

You would still need to draw the border and text to make it look right.
0
 

Author Comment

by:roujesky
ID: 41846864
How do I do it only when it is clicked?
0
 
LVL 20

Expert Comment

by:Russ Suter
ID: 41846869
That's what overriding the OnClick method is for and the private member _isClicked.
0
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 41846873
We would simply subscribe to the click event.  But also realize that the button border color and size are only shown when the button style is flat; e.g. -

Form1.cs -
using System;
using System.Drawing;
using System.Windows.Forms;

namespace EE_Q28976870
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
			button1.FlatAppearance.BorderColor = Color.Black;
			button1.FlatAppearance.BorderSize = 1;
		}

		private void OnClick(object sender, EventArgs e)
		{
			if (sender is Button)
			{
				var btn = sender as Button;
				if (btn.Equals(button1))
				{
					if (btn.FlatAppearance.BorderColor.Equals(Color.Black))
					{
						btn.FlatAppearance.BorderColor = Color.Blue;
						btn.FlatAppearance.BorderSize = 12;
						btn.FlatStyle = FlatStyle.Flat;
					}
					else
					{
						btn.FlatAppearance.BorderColor = Color.Black;
						btn.FlatAppearance.BorderSize = 1;
						btn.FlatStyle = FlatStyle.Standard;
					}
				}
			}
		}
	}
}

Open in new window

Form1.Designer.cs -
namespace EE_Q28976870
{
	partial class Form1
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 22F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.button1.Location = new System.Drawing.Point(42, 81);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(200, 99);
			this.button1.TabIndex = 1;
			this.button1.Text = "Click\r\nMe!!!";
			this.button1.UseVisualStyleBackColor = true;
			this.button1.Click += new System.EventHandler(this.OnClick);
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(284, 261);
			this.Controls.Add(this.button1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}

		#endregion

		private System.Windows.Forms.Button button1;

	}
}

Open in new window

Produces the following output -
Initial load -Capture.JPGAfter clicking the button -Capture.JPGAnd again -Capture.JPG
-saige-
0
 

Author Closing Comment

by:roujesky
ID: 41847133
that did it!

thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question