I have just made a new button for my C# uni project. For some reason it doesn't seem to work any ideas?
Here is my code for the button:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSD_A2_WS2435593_WS243380.Classes.FormsaAndControls
{
class clsFlatButton : Button //control object declaration.
{
public clsFlatButton()
{
BackColor = Color.FromArgb(225, 236, 244);
ForeColor = Color.FromArgb(57, 115, 157);
}
protected override void OnPaint (PaintEventArgs e) //draws the rectangle and text
{
e.Graphics.FillRectangle(new SolidBrush(BackColor), 0, 0, this.Width, this.Height);
TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter;
TextRenderer.DrawText(e.Graphics, Text, Font, new Point(Width + 3, Height / 2), ForeColor, flags);
}
protected override void OnMouseEnter(EventArgs e) //changes on mouse hover it enters the box
{
base.OnMouseEnter(e);
BackColor = onMouseHoverBackColour;
ForeColor = onMouseHoverForeColour;
}
protected override void OnMouseLeave(EventArgs e) //changes on mouse hover when it leaves the box
{
base.OnMouseEnter(e);
BackColor = Color.FromArgb(225, 236, 244);
ForeColor = Color.FromArgb(57, 115, 157);
}
protected override void OnMouseDown(MouseEventArgs e) //changes on mouse click
{
base.OnMouseDown(e);
BackColor = Color.FromArgb(57, 115, 157);
ForeColor = Color.FromArgb(225, 236, 244);
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseDown(e);
BackColor = Color.FromArgb(225, 236, 244);
ForeColor = Color.FromArgb(57, 115, 157);
}
//Available settings:
//Static values for initialisation.
private Color onMouseHoverBackColour = Color.FromArgb(251, 255, 255);
private Color onMouseHoverForeColour = Color.FromArgb(6, 64, 106);
//These will appear on the clsFlatButton control so they can be changed.
public Color onMouseHoverBackColourChanger
{
get { return onMouseHoverBackColour;}
set { onMouseHoverBackColour = value; }
}
public Color onMouseHoverForeColourChanger
{
get { return onMouseHoverForeColour; }
set { onMouseHoverForeColour = value; }
}
}
}
So when I go to click it it does change colour even when hover, though it does not actually execute the code inside of it.
So I have nothing else to say but stackoverflow won't let me post this question without more normal text. So here we go I guess, I could just paste lorem ipsum but I don't want to be shouted at.
EDIT: #1 Image as requested: Screenshot of button properties
This happens because you are invoking
base.OnMouseDown
and not base.OnMouseUp
in
protected override void OnMouseUp(MouseEventArgs e)
I've reproduced the problem, and fixed it by:
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
BackColor = Color.FromArgb(225, 236, 244);
ForeColor = Color.FromArgb(57, 115, 157);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.