[英]Custom C# Button Does Nothing When Clicked Any Ideas?
I have just made a new button for my C# uni project.我刚刚为我的 C# uni 项目创建了一个新按钮。 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.所以我无话可说,但是 stackoverflow 不会让我在没有更多正常文本的情况下发布这个问题。 So here we go I guess, I could just paste lorem ipsum but I don't want to be shouted at.所以我们开始吧,我想,我可以粘贴 lorem ipsum 但我不想被大喊大叫。
EDIT: #1 Image as requested: Screenshot of button properties编辑:#1 要求的图像:按钮属性的屏幕截图
This happens because you are invoking发生这种情况是因为您正在调用
base.OnMouseDown
and not base.OnMouseUp
in base.OnMouseDown
而不是base.OnMouseUp
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.