[英]Windows Forms: How to extend a button?
如何扩展按钮?
我想要一个具有附加属性IsSwitchOffable的Button。
我是否必须编写额外的自定义控件?
编辑:我希望该按钮可用作标准的WindowsFormsButton。
这包括我可以在设计时添加按钮!
扩展按钮控件与扩展C#中的任何类没有什么不同。 只需执行以下操作:
class ToggleButton : Button {
// Add your extra code
}
您需要创建一个继承System.Windows.Forms.Button
类的类,并添加您的属性和相关行为。
编译项目后,新类将显示在工具箱中。
我知道这已经过时了,已经得到了解答 - 然而,为什么要让生活变得困难?
每个控件都有一个Tag属性,您可以轻松设置为IsSwitchedOffable - 或者更好的英文CanBeDisabled
更容易。
在我的拼图应用程序2d按钮的位置将被更改...所以我需要额外的设施......
我的按钮ButObj扩展了Button类
Public Class ButObj : Button
{
Point initloc;
Public ButObj(Point loc)
{ this.Location=initloc=loc ; }
Public bool isNearto(ButObj X)
{
if (this.Location.X==X.Location.X || this.Location.Y==X.Location.Y)
return true;
else return false;
}
Public bool isSettled()
{
if(this.Location==initloc)
return true ;
else return false;
}
Public void Replace (ButObj X)
{
Point temp ;
temp=this.Location;
this.Location=X.Location;
X.Location=temp;
}
}
以下代码以1_load()的形式编写
ButObj[ ][ ] B=new ButObj[4][4];
char c='A';
for (int i=0;i<4;i++)
for (int j=0;j<4;j++)
{ B[i][j]=new ButObj(new Point (i*100+10,j*100+10));
B[j][i].Text = ""+c++;
B[i][j].Font =new Font ("Arial", 24);
this.Controls.Add (B[i][j]);
B [i] [j] .MouseClick + = new MouseEventHandler(MouseClick); }
在鼠标单击事件中编码
private void MouseClick(Object sender, EventArgs e)
{
ButObj b=(ButObj)sender;
if (b.isNearto(B[3][3]))
b.Replace(B[3][3]);
\\ checking after replace
if(AllSolved());\\game over
}
bool AllSolved()
{
for (int i=0;i<4;i++)
for (int j=0;j<4;j++)
if (!B[i][j].isSettled)
return false ;
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.