C# 如何从我的用户控件访问主窗体上的 setter 方法

本文关键字:窗体 方法 setter 访问 控件 我的 用户 | 更新日期: 2023-09-27 18:31:33

我有一个用户控件,它包含一个面板,面板包含一个图片框。当我将鼠标移到图片框上时,我想更新主窗体上的标签。我在主窗体上有一个 get/set 方法,但我如何使用它?谢谢

  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?
        }
    }

C# 如何从我的用户控件访问主窗体上的 setter 方法

实际上,

在您的情况下可以执行以下操作:

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

但正确的方式是像菲利斯·波拉诺这样的事件:

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");
    }
}

理想情况下,您应该为此引发一个事件。

创建委托

public delegate void  Update();

在用户控件中

public class MyUserControl : UserControl
{
public event Update OnUpdate;
}

在主窗体上注册用户控件事件的处理程序。

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();

这可能会给你一个想法...

    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";
    }
}

您有以下几种选择:

  1. 在用户控件上创建一个事件,并且必须形成侦听它(我认为这是大多数 C# 程序员推荐的方法)。
  2. 将对主窗体的引用传递给用户控件(在构造函数中)。这样,用户控件就知道其 MainForm。
  3. this.ParentForm强制转换为 MainForm 类,然后您就有了引用。

选项 2 和 3 稍微舒适和懒惰,但代价是用户控件必须知道特定的类 MainForm。第一个选项的优点是可以在另一个项目中重用用户控件,因为它不知道 MainForm 类。

应从用户控件发布事件,并从主窗体订阅该事件。至少这是为winform建议的模式。在任何情况下,这个想法都是使控件从需要查看坐标的代理中"可观察",而不是将其用作更新感兴趣的代理的驱动程序。