2012-04-18 32 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

相关问题