2014-02-10 56 views
0

我有一個User Control。現在我想限制用戶,他只能使用一個 User Control每個窗體。限制用戶對用戶只有一個控制每個表格

假設我的自定義控件是文本框。在這種情況下,用戶只能爲每個表單使用一個文本框。

+1

沒有什麼是用戶可以如何將控件添加到表單。只有程序員才能做到這一點。當然,沒有什麼可以或者應該做些什麼來阻止程序員以他想要的方式創建程序。如果你嘗試,那麼你的UC會很快結束。 –

回答

0

嘗試Enterevent

  this.TxtBox.Enter += new System.EventHandler(this.TxtBox_Enter); 
     private void TxtBox_Enter(object sender, EventArgs e) 
     { 
       //disable all other textboxes 
       foreach (Control c in this.Controls) 
       { 
        if (c is TextBox) 
        { 
         c.Enabled = false; 
        } 
       } 
     } 
0

,如果你想限制你的用戶不必在單個窗體使用下面的技巧你無法控制的多個實例,以限制

public partial class UserControl1 : UserControl 
    { 
     public UserControl1() 
     { 
      InitializeComponent(); 
      this.Load += new System.EventHandler(this.UserControl1_Load); 
     } 
     private void UserControl1_Load(object sender, EventArgs e) 
     { 
      if (this.ParentForm.Controls.OfType<UserControl1>().Count() > 1) 
      { 
       //form already has a controler throw error 
       throw new System.Exception("you can have only one instance of me on a single form"); 
       this.Enabled = false; 
       this.Dispose(); 
      } 
     } 
    } 
+0

這將是我使用這種控制的唯一時間。下一站,通知文件。 – DonBoitnott

+0

你在做什麼? –