Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-10-17
5
Medium Priority
?
257 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 2000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 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