点击按钮时传递形式之间的变量 [英] Pass variable between forms when clicking button

查看:121
本文介绍了点击按钮时传递形式之间的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式。有一个在正在执行的所有主要code。并点击通过使用这种方法的菜单项时,其他形式被显示:

I have two Forms. One with where all the main code is being executed. And the other form is displayed when clicking a menu item by using this method:

Form2 videoSettings = new Form2();       

private void videoToolStripMenuItem_Click(object sender, EventArgs e)
{
    videoSettings.Show();
}

,然后打开containsfields当用户到达设置一些应用程序的设置形式。 然后,当点击保存按钮,我想这个变量:公众诠释deviceIndex; 从原来的形式获取。

The form which is then opened containsfields where the user gets to set some settings for the application. Then when clicking the "save" button I want this variable: public int deviceIndex; to be fetched from the original Form.

所以我想知道如果我可以添加任何事件或某事在Form1中,其检测时,保存被点击videoSettings(窗体2)?按钮

So I'm wondering if I can add any event or something in Form1 which detects when the save button is clicked in videoSettings (Form2)?

推荐答案

我会做不同的方式。我想分开操作的用户界面和业务逻辑层之间的code。因此,您的方案会以这样的方式运行:

I would do it a different way. I'd separate the code between the UI handling and the business logic layers. So your scenario would run in such a way:

  1. 第一种形式发出事件通知与某些语义按钮被激活。处理所需的数据被包括在该事件的数据。
  2. 业务逻辑监听该事件,并决定为第二形态上发出一个命令。它呼吁窗体类适当的方法,将所需的信息作为参数(也许如果需要preprocessing参数)。
  3. 的第二种形式从业务逻辑接收指令并更新视图。

这个方法不会出现问题的。

This way the problem doesn't arise at all.

例:(我不是专家的WinForms,当心它可以从最佳的WinForms做法POV完全错误的)

Example: (I'm not the winforms expert, beware it can be totally wrong from the POV of best winforms practices.)

第1部分(第一种形式):

Part 1 (first form):

class ProcessingActivatedEventArgs : EventArgs
{
    public ProcessingActivatedEventArgs(int data) { MoreData = data; }
    public int MoreData { get; protected set; }
}

class Form1 : Form
{
    private int currentData;
    public event EventHandler<ProcessingActivatedEventArgs> ProcessingActivated;
    void OnButtonClick(object sender, EventArgs args)
    {
        // ...
        if (ProcessingActivated != null)
            ProcessingActivated(new ProcessingActivatedEventArgs(currentData));
    }
}

第2部分:(业务逻辑)

Part 2: (business logic)

class Controller
{
    Form1 f1;
    Form2 f2;

    void StartFirstForm()
    {
        f1 = new Form1();
        f1.ProcessingActivated += OnProcessingActivated;
        f1.Show();
    }

    void OnProcessingActivated(object sender, ProcessingActivatedEventArgs args)
    {
        int data = args.MoreData;
        f1.DisableProcessingRequests();
        model.ProcessingFinished += OnProcessingFinished;
        model.StartProcessing(data);
        if (data > 0)
            f2.DisplayDataProcessing(0, data);
        else if (data < 0)
            f2.DisplayDataProcessing(data, 0);
        else
            throw new SomeCoolException("impossible data");
    }
}

第3部分:(第二种形式)

Part 3: (second form)

class Form2 : Form
{
    public void DisplayDataProcessing(int lower, int upper)
    {
        // ... update the UI
    }
}

请注意,此实现纽带控制器并形成严格的比它可以做。在WPF中,去耦通过使用合适的的DataContext 实现(但我不知道如何正确地做它的WinForms)。

Note that this implementation ties the Controller and forms tighter than it could be done. In WPF, the decoupling is achieved by using the appropriate DataContext (but I don't know how to do it properly in WinForms).

这篇关于点击按钮时传递形式之间的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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