从Form2更新Form1对象 [英] Updating Form1 objects from Form2

查看:77
本文介绍了从Form2更新Form1对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从Form2更新Form1值,但是什么也没有发生.也没有错误,很奇怪...

I try to update Form1 values from Form2, but nothing happens. No errors also, strange...

Form1:

 public static Form2 f2{ get; set; } = new Form2();

 private void addButton1_Click(object sender, EventArgs e)
        {        
            f2.Show(); 
        }

Form2:

 public void button1_Click(object sender, EventArgs e)
    {
        Form f1 = new Form();
        f1.label2.ForeColor = Color.Red;
    }

什么都没发生.我可以从Form1获取数据到Form2,但是我不能发送,我可以,但是什么也没发生...为什么?谢谢

Nothing happens. I can get data to my form2 from form1, but I cannot send, I mean I can, but nothing happens... Why? Thank you

推荐答案

此处提到的其他解决方案适用于您的特定情况,但我鼓励您从全局出发,并设计一个适用于大多数情况的通用解决方案案例.

The other solutions mentioned here would work for your specific case, but I encourage you to look at the big picture, and design a generalized solution that will work for most cases.

您的问题从本质上可以归结为 根据另一种形式的事件在一种形式上做某事.

Your problem essentially boils down to doing something on one form based on the events of another form.

我认为最好的方法是:

  • Form1自行执行所有操作.
  • Form2自行执行所有操作.
  • 如果您需要基于Form2上发生的事件在Form1上做某事,请让Form2通知Form1那里发生了某件事,所以请自己做.
  • 如有必要,将数据从Form2传递到Form1.
  • Let Form1 do all its own actions.
  • Let Form2 do all its own actions.
  • If you need to do something on Form1 based on an event that occurred on Form2, let Form2 notify Form1 that something happened there, so go do your own thing.
  • If necessary, pass data from Form2 to Form1.

因此,我将使用代理人为此.

想象一下,您在Form1上有一个Button和一个Label.单击该按钮将打开Form2,在其上还有另一个Button.单击Form2上的此按钮应更改Form1上标签的背景颜色.因此,我们的设置将如下所示.您没有提到它是Winforms还是WPF,因此为了方便起见,我使用WPF,但是两者的想法都是相同的.

Imagine you have a Button and a Label on your Form1. Clicking the button opens up Form2, on which you have another Button. Clicking this button on Form2 should change the background color of the label on Form1. So our setup would look like this. You haven't mentioned if it's Winforms or WPF, so I'm using WPF for my convenience but the idea is the same in either.

在我的Form1中,我将声明一个具有以下签名的公共代表:

In my Form1 I'd declare a public delegate with a signature like this:

public delegate void NotifyEvent();

也就是说,此委托表示一种不带任何参数且返回类型为void的方法.想法是让Form2Form1中调用"一个方法,该方法实质上是 notify 通知Form2上单击了按钮.因此,如果有一种方法可以从Form2调用位于Form1中的方法,那么我们可以通知Form1到目前为止与我有关的Form2事件发生了吗?

That is, this delegate represents a method that takes in no parameters, and has void return type. The idea is to let Form2 'call' a method in Form1, and that method essentially notifies the button was clicked on Form2. So, if there was a way for us to call a method that resides in the Form1 from Form2, we can then notify Form1 of an event happening on Form2 With me so far?

现在,如果我在Form1中编写这样的方法,然后从Form2中调用它,那应该可以实现我们的目标.在这里,lblDispForm1中的Label.

Now, if I write a method like this in the Form1, and let it be called from Form2, that should accomplish our goal. Here, lblDisp is the Label in Form1.

public void ButtonClickedOnForm2()
{
    lblDisp.Background = new SolidColorBrush(Colors.LawnGreen);
}

要实现此目的,我将在Form1中定义一个类型为NotifyEvent的委托,如下所示,并向其中注册ButtonClickedOnForm2()方法.您后面的Form1代码应如下所示:

To accomplish this, I would define a delegate of type NotifyEvent in Form1 like below, and register the ButtonClickedOnForm2() method to it. Your Form1 code behind should look like this

public delegate void NotifyEvent();

public partial class Form1 : Window
{
    public NotifyEvent notifyDelegate;

    Form2 form2 = null;

    public Form1()
    {
        InitializeComponent();
        // This is 'registering' the ButtonClickedOnForm2 method to the delegate.
        // So, when the delegate is invoked (called), this method gets executed.
        notifyDelegate += new NotifyEvent(ButtonClickedOnForm2);
    }

    public void ButtonClickedOnForm2()
    {
        lblDisp.Background = new SolidColorBrush(Colors.LawnGreen);
    }

    private void BtnOpen_Click(object sender, RoutedEventArgs e)
    {
        // Passing the delegate to `Form2`
        form2 = new Form2(notifyDelegate);
        form2.Show();
    }
}

因此,现在我们需要修改我们的Form2.我们需要告诉我们在单击按钮时要调用哪个委托.为此,我将像这样在Form2的构造函数中传递委托:

Accordingly, now we need to modify our Form2. We need to tell us which delegate to invoke when the button click happens. So to do that, I'd pass the delegate in the constructor of Form2 like so:

public partial class Form2 : Window
{
    NotifyEvent notifyDel;

    public Form2(NotifyEvent notify)
    {
        InitializeComponent();
        notifyDel = notify;
    }

    private void BtnOK_Click(object sender, RoutedEventArgs e)
    {
        // This invokes the delegate, which in turn calls the ButtonClickedOnForm2 method in Form1.
        notifyDel.Invoke();
    }
}

现在,在Form2上单击按钮时,它将调用委托.在Form1上,我们已经告知过,如果调用了委托,则应该继续执行ButtonClickedOnForm2方法.在这种方法中,我们编写了代码来更改标签的背景色.那应该可以解决您的问题.

Now, when the button is clicked on Form2, it invokes the delegate. And on our Form1, we've told it that in case the delegate is invoked, it should go ahead and execute the ButtonClickedOnForm2 method. In that method, we've written code to change the background color of the label. And that should solve your problem.

此外,如果要将数据从Form2传递到Form1,则可以简单地将参数添加到委托定义中.假设您要将stringForm2传递到Form1.然后,您将您的委托更改为如下所示:

Additionally, if you want to pass data from Form2 to Form1, you can simply add parameters to the delegate definition. Say you want to pass a string from Form2 to Form1. Then, you'd change your delegate to look like this:

public delegate void NotifyEvent(string data);

方法ButtonClickedOnForm2如下:

public void ButtonClickedOnForm2(string data)
{
    lblDisp.Content = data;
    lblDisp.Background = new SolidColorBrush(Colors.LawnGreen);
}

然后在Form2上,通过传递如下字符串来调用委托:

Then on Form2, invoke the delegate by passing a string like so:

private void BtnOK_Click(object sender, RoutedEventArgs e)
{
    // This invokes the delegate, which in turn calls the ButtonClickedOnForm2 method in Form1.
    notifyDel.Invoke("I am from Form2");
}

现在单击Form2上的按钮应更改Form1上标签的文本和背景颜色,如下所示:

Now clicking the button on Form2 should change text and background color of label on Form1 like this:

这篇关于从Form2更新Form1对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆