在 Windows 窗体之间传递值 c# [英] Passing Values Between Windows Forms c#

查看:23
本文介绍了在 Windows 窗体之间传递值 c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力研究如何在表单之间传递值.我有四个表单,我想将 Login 检索到的信息传递给第四个也是最后一个表单.

I am struggling to work out how to pass values between forms. I have four forms and I want to pass the information retrieved by the Login to the fourth and final form.

这是我目前所拥有的.

在这个函数中:

private void btnLogin_Click(object sender, EventArgs e)

我已经反序列化了我想要的数据:

I have deserialized the data I want like this:

NewDataSet resultingMessage = (NewDataSet)serializer.Deserialize(rdr);

然后,当我调用下一个表单时,我已经这样做了:

Then, when I call the next form I have done this:

Form myFrm = new frmVoiceOver(resultingMessage);
myFrm.Show();

然后,我的 VoiceOver 表单如下所示:

Then, my VoiceOver form looks like this:

public frmVoiceOver(NewDataSet loginData)
{
    InitializeComponent();
}

private void btnVoiceOverNo_Click(object sender, EventArgs e)
{
    this.Close();
    Form myFrm = new frmClipInformation();
    myFrm.Show();
}

当我调试时,我可以看到数据在第二种形式的loginData 中,但我似乎无法在btnVoiceOverNo_Click 事件中访问它.我如何访问它以便我可以将它传递给下一个表单?

When I debug, I can see the data is in loginData in the second form, but I cannot seem to access it in the btnVoiceOverNo_Click event. How do I access it so I can pass it to the next form?

推荐答案

您需要将 loginData 放入 frmVoiceOver 类中的局部变量中才能访问它从其他方法.目前它的作用域是构造函数:

You need to put loginData into a local variable inside the frmVoiceOver class to be able to access it from other methods. Currently it is scoped to the constructor:

class frmVoiceOver : Form
{
    private NewDataSet _loginData;

    public frmVoiceOver(NewDataSet loginData)
    {
        _loginData = loginData;

        InitializeComponent();
    }

    private void btnVoiceOverNo_Click(object sender, EventArgs e)
    {
        // Use _loginData here.
        this.Close();
        Form myFrm = new frmClipInformation();
        myFrm.Show();
    }
}

此外,如果两个表单在同一个进程中,您可能不需要序列化数据,只需将其作为标准引用传递给表单的构造函数即可.

Also, if the two forms are in the same process you likely don't need to serialize the data and can simply pass it as a standard reference to the form's constructor.

Google 类似C# 变量范围"之类的内容,以了解更多这方面的内容你会一直遇到这个概念.我很感激你是自学成才的,所以我只是想加强这一点:-)

Google something like "C# variable scope" to understand more in this area as you will encounter the concept all the time. I appreciate you are self-taught so I'm just trying to bolster that :-)

这篇关于在 Windows 窗体之间传递值 c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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