2012-04-18 53 views
2

我有一個UserControl,它包含一個面板,面板包含一個圖片框。 當我將鼠標移動到圖片框上時,我想更新MainForm上的標籤。 我在主窗體上有get/set方法,但是如何使用它?謝謝C#如何訪問主窗體上的setter方法,從我的用戶控件?

public partial class MainForm : Form 
    { 
     public MainForm() 
     { 
      InitializeComponent(); 
     } 
     public String MouseCords 
     { 
      get { return this.MouseCordsDisplayLabel.Text; } 
      set { this.MouseCordsDisplayLabel.Text = value; } 
     } 
    } 


    public partial class ScoreUserControl : UserControl 
    { 
     public ScoreUserControl() 
     { 
      InitializeComponent(); 
     } 

     private void ScorePictureBox_MouseMove(object sender, MouseEventArgs e) 
     { 
      // MainForm.MouseCords("Hello"); //What goes here? 
     } 
    } 

回答

2

事實上它可能在你的情況下,像這樣做:

((MainForm)this.ParentForm).MouseCords = "Some Value Here"; 

但是,正確的方法是與像菲菲Pollano事件mentinoed:

public partial class MainForm : Form 
{ 
    public MainForm() 
    { 
     InitializeComponent(); 

     this.myCustomControlInstanse.PicureBoxMouseMove += new EventHandler<StringEventArgs>(myCustomControlInstanse_PicureBoxMouseMove); 
    } 

    private void myCustomControlInstanse_PicureBoxMouseMove(object sender, StringEventArgs e) 
    { 
     this.MouseCordsDisplayLabel = e.Value // here is your value 
    } 
} 


public class StringEventArgs : EventArgs 
{ 
    public string Value { get; set; } 
} 

public partial class ScoreUserControl : UserControl 
{ 
    public event EventHandler<StringEventArgs> PicureBoxMouseMove; 

    public void OnPicureBoxMouseMove(String value) 
    { 
     if (this.PicureBoxMouseMove != null) 
      this.PicureBoxMouseMove(this, new StringEventArgs { Value = value }); 
    } 

    public ScoreUserControl() 
    { 
     InitializeComponent(); 
    } 

    private void ScorePictureBox_MouseMove(object sender, MouseEventArgs e) 
    { 
     this.OnPicureBoxMouseMove("Some Text Here"); 
    } 
} 
+0

如果他使用這種控制一種不同類型的表單? 你說得對 - 事件/觀察者方法是正確的。我們都走了「快而骯髒」... – 2012-04-18 09:36:34

+0

感謝您的快速回復,非常感謝 – theIrishUser 2012-04-18 09:43:13

0

您應該從用戶控件發佈event並從主窗體訂閱它。 至少這是winform建議的模式。在任何情況下,這個想法都是讓需要查看座標的代理人控制「可觀察到的」,而不是將其用作更新感興趣代理的驅動程序。

+0

能否請你告訴我如何做到這一點。 我嘗試之前我嘗試get/set方法。 – theIrishUser 2012-04-18 09:29:45

+0

答案中有一個鏈接是一個完整的事件教程,看一看 – 2012-04-18 09:32:42

+0

謝謝felice,我是一個大規模的noob在這裏和C#,感謝您如此快速的回覆和幫助。 – theIrishUser 2012-04-18 09:37:17

1

這可能給你一個想法...

public partial class ScoreUserControl : UserControl 
{ 
    public ScoreUserControl() 
    { 
     InitializeComponent(); 
    } 

    private void ScorePictureBox_MouseMove(object sender, MouseEventArgs e) 
    { 
     // MainForm.MouseCords("Hello"); //What goes here? 
     MainForm parent = this.ParentForm as MainForm; 
     if (parent != null) parent.MouseCordsDisplayLabel.Text = "Hello"; 
    } 
} 
+0

美麗,謝謝 – theIrishUser 2012-04-18 09:37:51

1

您有幾種選擇:

  1. 在用戶控件上創建一個事件並且必須形成監聽(我認爲這是大多數C#程序員推薦的方式)。
  2. 將對主窗體的引用傳遞給用戶控件(在構造函數中)。這樣,用戶控件就知道它的MainForm。
  3. this.ParentForm轉換爲MainForm類,然後您可以參考。

選項2和3更舒適和懶惰,但代價是用戶控件必須知道特定的類MainForm。第一個選項的優點是可以在另一個項目中重用用戶控件,因爲它不知道MainForm類。

2

理想情況下,您應該提出相同的事件。

在用戶控制

public class MyUserControl : UserControl 
{ 
public event Update OnUpdate; 

} 

在主表格創建委託

public delegate void Update(); 

註冊用戶控制事件的處理程序。

public class Main 
{ 
public Main() 
{ 
myUserControl.OnUpdate += new Update(this.UpdateHandler); 
} 

void UpdateHandler() 
{ 
//you can set the delegate with sm arguments 
//set a property here 

} 
} 

在用戶控制,

要提高對按鈕的事件點擊

做到這一點

OnUpdate(); 
+0

美麗,非常容易理解。謝謝 – theIrishUser 2012-04-18 09:42:19

相關問題