簡體   English   中英

Windows窗體:如何擴展按鈕?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM