使用按钮在表单之间传递变量 [英] Passing variables between forms using a button

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

问题描述

我想知道如何传递,比如说一个整数,从form1到form2.

I'd like to know how to pass, let's say an integer, from form1 to form2.

我试图通过一个可以打开 form2 的按钮来做到这一点,但事件按钮单击无法识别整数...我该怎么办?

I tried to do that through a button that would open form2, but the event button click didn't recognize the integer... What should I do?

在 form1 中我有整数 x,我希望当我点击 button1 时,form2 会在标签中打开 x 值.

In form1 I have integer x, and I want that when I click on button1, form2 would open up with x value in a label.

如果有一种方法可以在没有按钮的情况下传递信息(然后我可以使用按钮来打开 form2),那也很棒.

If there's a way to pass the info without the button (then I could use the button just to open form2) that would be great as well.

推荐答案

您可以使用第二个表单构造函数.

you can use second form constructor.

    private int input;
    public Form2(int input)
    {
        this.input = input;
        InitializeComponent();
    }

当你创建一个对象时,你可以传递你的 var(int in here):

when you create an object , you can pass your var(int in here):

        int myvar=911;
        Form2 tmp = new Form2(myvar);
        tmp.Show();

现在您可以在 form2 中使用该私有变量:

now you can use that private variable in form2:

lbl.Text=input.toString();

<小时>

在 Form1 中:


in Form1:

private void button1_Click(object sender, EventArgs e)
    {
        Form2 tmp = new Form2(911);
        tmp.Show();
    }

在 Form2 中:

    public Form2(int input)
    {
        InitializeComponent();
        label1.Text = input.ToString();
    }

发送你的代码来解决这个问题.我不知道为什么没有你的代码它不能识别你的变量!

send your code for solve this problem.i can't find out why it doesn't recognize your vars without your codes!

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

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