Solved

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

Posted on 2016-10-17
5
126 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
[X]
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
  • 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 34

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert VB web project to C# 3 67
Need to find a web event on the IIS server 4 54
.NET Redemption/Outlook Examples 3 35
Error in Visual Project 10 49
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

710 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