Solved

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

Posted on 2016-10-17
5
40 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 32

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now