简体   繁体   English

Windows窗体:如何扩展按钮?

[英]Windows Forms: How to extend a button?

How can I extend a Button? 如何扩展按钮?

I want a Button that has an additional property IsSwitchOffable. 我想要一个具有附加属性IsSwitchOffable的Button。

Do I have to write an extra custom control? 我是否必须编写额外的自定义控件?

EDIT: I want that the button is usable as a standard WindowsFormsButton. 编辑:我希望该按钮可用作标准的WindowsFormsButton。

This includes that I can add the button at design time! 这包括我可以在设计时添加按钮!

Extending a button control is no different then extending any class in C#. 扩展按钮控件与扩展C#中的任何类没有什么不同。 Simply do the following: 只需执行以下操作:

class ToggleButton : Button {
// Add your extra code
}

You need to create a class that inherits the System.Windows.Forms.Button class and adds your property and associated behavior. 您需要创建一个继承System.Windows.Forms.Button类的类,并添加您的属性和相关行为。

After compiling your project, your new class will appear in the toolbox. 编译项目后,新类将显示在工具箱中。

I know this is old and has been answered - however, Why make life difficult? 我知道这已经过时了,已经得到了解答 - 然而,为什么要让生活变得困难?

Each control has a Tag property which you can easily set to IsSwitchedOffable - or better English CanBeDisabled 每个控件都有一个Tag属性,您可以轻松设置为IsSwitchedOffable - 或者更好的英文CanBeDisabled

Far easier. 更容易。

In my puzzle application 2d button's location is to be changed... So i need extra facilities... 在我的拼图应用程序2d按钮的位置将被更改...所以我需要额外的设施......

My button ButObj extends Button class 我的按钮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;
    }
         }

Following code is written in form 1_load () 以下代码以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); B [i] [j] .MouseClick + = new MouseEventHandler(MouseClick); } }

Coding in mouse click event 在鼠标单击事件中编码

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM