表单之间发送的参数不变 [英] sent parameter from form to form does not change

查看:77
本文介绍了表单之间发送的参数不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我正尝试将参数从一种形式发送到另一种形式,如下所示

Hello
I''m trying to send a parameter from one form to another as follows

int number=0;
Form2 f2 = new Form2(ref number);
f2.ShowDialog();


int nb;
       public Form2(ref int number)
       {
           nb=number;
           InitializeComponent();
       }




问题是,当用户关闭Form2时,整数数字"不会更改
在Form2中,我进行了一些计算并将结果保存在"nb"中
那是什么问题?
当我使用参考标签并将结果保存在标签文本中时,结果就是我想要的
但是为什么它不能与整数或字符串一起使用?
提前thx




the problem is that when the user closes Form2 the integer "number" does not change
in Form2 i did some calculation and saved the result in "nb"
so what''s the problem?
when i used a ref Label and saved the result in the label''s text the result was what i wanted
but why is it not working with integers or strings?
thx in advance

推荐答案

考虑使用例如属性而不是ref参数(尤其是构造函数).

例如,在form2中定义数字如下:
Consider using for example properties instead of ref parameters (especially to constructors).

For example define the number as follows in form2:
public number { get; set; }
public Form2()
{
    InitializeComponent();
}


现在,您可以从调用方引用该属性:


Now you can refer the property from the calling side:

int number=0;
Form2 f2 = new Form2();
f2.number = number;
if (f2.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
   number = f2.number;
}


上面的代码还检查对话框是否以正确的结果关闭,并且只有在此情况下,调用方的号码才会更改.

为了使此功能有效,还应该在form2中的某个位置更改数字.


The code above also checks that the dialog is closed with proper result and only if it is, the number at calling side is changed.

In order for this to work the number should also be changed somewhere in form2


您回答了自己的问题.您更改了nb,而不是数字.关闭Form2之前,请确保也已将新值分配给number.
You answered your own question. You changed nb, not number. Before you close Form2, ensure that you have assigned the new value to number as well.


这篇关于表单之间发送的参数不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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