我需要在表单2中获取form1值? [英] i need to get form1 values in form 2?

查看:77
本文介绍了我需要在表单2中获取form1值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我的系统有登录系统..

所以如果密码正确,它将进入新窗口,并在角落顶部显示记录的用户名。



i是这样的吗?



Basically my system has login system..
so if password is correct it will proceed to new window and appear logged user name on the top of the corner.

i did like this?

public partial class Form1 : Form
{
    // This is the text that will be entered in form2
    public String form2text;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // Show form2
        Form2 objForm2 = new Form2(this);
        objForm2.ShowDialog();
        // When form2 is closed, update the label text on form1
        label1.Text = form2text;
    }
}

public partial class Form2 : Form
{
    // This is the instance of Form1 that called form2
    private Form1 form1caller;

    public Form2(Form1 form1caller)
    {
        InitializeComponent();
        this.form1caller = form1caller;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // Pass the textBox value to form1 before closing form2
        form1caller.form2text = textBox1.Text;
        this.Close();
    }
}

推荐答案

你知道你的代码......因为它......我会工作?从技术上讲,您已经将对Type1的对象实例的引用注入到Type2的对象的实例中。因此,注入的Type(Form1)有一个公共字段'form2Text,然后可以在'Form2的实例中设置。



注意这样做这已经暴露了Form1实例中的所有,其访问修饰符(声明)是'public或'internal Form2的实例。



正如其他人在这个帖子中所做的链接所示,还有其他方法可以达到这个目的。



如果你想留下来例如,注入模型可以将Form1上的Label对象传递给第二个Form:这样可以减少第二个Form中发生的事情可能对第一个Form产生异常的可能性。



但是,我想建议你有一个更好的方法来实现这个,基于你的设计的意图有一个在我们之前首先显示并需要验证的所需UserName /密码输入对话框呃见过(有权访问)主表格。



如果你想看一个这个模型的代码示例...主表单将除非登录成功,否则甚至不会被创建 ...只需询问。
You do know that your code ... as it is ... will work ? Technically, you have injected a reference to an instance of an object of Type 'Form1 into an instance of an object of Type 'Form2. So, the injected Type (Form1) has a public field, 'form2Text, that can, then, be set within the instance of 'Form2.

Note that by doing this you have exposed everything in the Form1 instance whose access modifier (declaration) is 'public or 'internal to the instance of Form2.

As the links suggested by other people responding on this thread show, there are other ways to achieve this.

If you wanted to stay with the model of "injection," you could, for example, pass the Label object on Form1 to the second Form: that reduces the possibility that what happens in the second Form could do something strange to the first Form.

However, I'd like to suggest to you there is a much better way to implement this based on the assumption that the intent of your design is to have a required UserName/Password entry dialog that is presented first, and requires validation, before the user ever sees (has access to) the Main Form.

If you want to see a code example of this model ... where the Main Form will not even be created unless the log-in is successful ... just ask.


这篇关于我需要在表单2中获取form1值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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