从另一种观点认为更新视图 [英] Update view from another view

查看:200
本文介绍了从另一种观点认为更新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新从通过事件另一个视图的视图。目前,我不知道该怎么做?

I'd like to update a view from another view via events. At the moment I have no idea how to do that?.

我的情况: 我有一个计算引擎,当我进行计算,我要通知用户有关执行的步骤。于是我打开了的RichTextBox 控件来显示所需信息的新窗口。当执行一个新的台阶,我想提出一个事件,以显示在另一个窗口中的文本(的RichTextBox )。

My Case: I have a calculation engine, and when I perform the calculation, I want to inform the user about the performed steps. So I open a new window with a RichTextBox control to display the desired information. When a new step is performed, I want to raise an event in order to display the text in the other window (RichTextBox).

有一个例子,它可以帮助我做到这一点?

Is there an example which can help me to do this?

推荐答案

只是把它添加没有编译检查,谨防错字的的。

Just added it without compile-check, beware of typo's.

public partial class Form1: Form {
    protected void btnCalculation_Click(object sender, EventArgs e) {
        var form2 = new Form2();
        form2.RegisterEvent(this);
        form2.Show();
        OnCalculationEventArgs("Start");
        // calculation step 1...
        // TODO
        OnCalculationEventArgs("Step 1 done");
        // calculation step 2...
        // TODO
        OnCalculationEventArgs("Step 2 done");
    }

    public event EventHandler<CalculationEventArgs> CalculationStep;
    private void OnCalculationStep(string text) {
        var calculationStep = CalculationStep;
        if (calculationStep != null)
            calculationStep(this, new CalculationEventArgs(text));
    }
}

public class CalculationEventArgs: EventArgs {
    public string Text {get; set;}
    public CalculationEventArgs(string text) {
        Text = text;
    }
}

public partial class Form2: Form {
    public void RegisterEvent(Form1 form) {
        form1.CalculationStep += form1_CalculationStep;
    }

    private void form1_CalculationStep(object sender, CalculationEventArgs e) {
        // Handle event.
        // Suppose there is a richTextBox1 control;
        richTextBox1.Text += e.Text;
    }
}

这篇关于从另一种观点认为更新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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