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

我有一个UserControl,其中包含一个面板,该面板包含一个图片框。
当我将鼠标移到图片框上时,我想更新MainForm上的标签。
我在主窗体上有一个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?
        }
    }

参考方案

实际上,您可以按照以下方式进行操作:

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

但是正确的方法是处理像Felice Pollano这样的事件:

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

Forms.py文件应该放在哪里? - python

我现在开始写我的第一个Django项目,我需要为应用创建我的forms.py文件。我见过一些教程将文件存储在项目的主文件夹下,而另一些则存储在app目录中。如果我想制作仅适用于一个应用程序的表单,哪种布局最适合我?是否可以制作多个文件来保存表单代码?谢谢! python大神给出的解决方案 这是标准布局:├── apps/ | ├── [app]/ | | ├…

模型中双向加密属性的Getter / setter - c#

我需要对数据库中的某些字段进行加密,因此,我需要对进入数据库的数据进行加密,然后在显示时将其解密。我已经设置好了加密方法和解密方法,并在一个动作中使它像这样工作:model.EncryptedProperty = Encrypt(viewModel.Property); viewModel.Property = Decrypt(EncryptedProper…

如何在XAML [Xamarin.Forms]中使用String以外的Type设置自定义属性值 - c#

在XAML的Xamarin.Forms中,您可以编写如下内容:<Entry Keyboard="Plain" /> 我调查了Entry类,并且Keyboard属性的类型为Xamarin.Forms.Keyboard。但是,如果我创建自己的自定义ContentView并在其中写入如下内容: public static reado…

LeetCode题解计算机为什么是基于二进制的?

可以是三进制么?二进制有什么好处?题解:为什么叫电子计算机?算盘应该没有二进制

LeetCode题解统计城市的所有灯泡

这个是我刚毕业的时候,一个真实的面试题,这是一个开放题。题目描述:想办法,将一个城市的所有灯泡数量统计出来。题解:费米估算法1、如果某个城市常驻人口有1000万2、假设每5人居住在一套房里,每套房有灯泡5只,那么住宅灯泡共有1000万只3、假设公众场所每10人共享一只灯泡,那么共有100万只4、主要的这两者相加就得出了1100万只当然实际上这是估算的,具体应…