Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in page 3 46
Vb.net dynamic formulas in runtime 11 63
Javascript and Jquery not firing 9 42
Counting connections to SQL Server through C# 3 29
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 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

12 Experts available now in Live!

Get 1:1 Help Now