如何通过主表单从第二个表单获取数据? [英] How Do I Get Data From A Second Form Via A Main Form ?

查看:61
本文介绍了如何通过主表单从第二个表单获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用多种形式。第一种形式应作为主要形式,而其他形式则具有个别功能。一个简单的例子,我想计算BMI。因此在用户插入其高度和重量时的主要形式。数据应传递给第二种形式,并在那里进行bmi的计算。一旦计算完成,答案将以主要形式显示。到目前为止,我只能将数据从一个表单发送到另一个表单。在检索数据时,我没有得到计算出的数据。我总是拿回原来的标签文字值。

ps~我创建上面的例子只是为了让我的问题更加清晰。



在我的情况下,Label1.Text值总是取SecondForm.Label1.Text中的初始值,而不是在函数radio.button1.click完成后取值。



这是我正在使用的实际代码:

I would like to use multiple form. the first form shall serve as the main form while the other forms would carry individual functions. A simple example, I would like to calculate BMI. thus in the main form when the user inserts their height and weight. The data should be passed to second form and calculation of bmi shall take place there. once the calculation is done the answer would be shown in main form. so far i could only send the data from a to another form. when retrieving back the data I'm not getting the calculated data. I always get back my original label text value.
ps ~ I created the example above just to make my question clearer.

In my case Label1.Text value always takes the initial value in SecondForm.Label1.Text rather than taking the value after the function radio.button1.click is done.

Here is the actual code I'm using:

Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim SecondForm As New Form2
        SecondForm.Show()
        SecondForm.RadioButton1.PerformClick()

        If SecondForm.RadioButton1.Checked = True Then
        Label1.Text = SecondForm.Label1.Text

    End If
    End Sub

推荐答案

参考文章

将数据传回主叫表单 [ ^ ]


在您的第二个表单中创建一个委托和一个事件,然后在主表单中订阅该事件。



在第二个表格课程中:

Create a delegate and an event in your second form, then subscribe to the event in the main form.

In second form class:
public delegate void UpdateCalculation(object sender, double value);
public event UpdateCalculation OnUpdateCalculation;





实际上这是首选的方式



Actually this is the preferred way

public class CalculationEventArgs : EventArgs
{
  CalculationEventArgs(double _value)
  {
    CalculatedValue = _value;
  } 

  double CalculatedValue { get; set; }
}
public delegate void UpdateCalculation(object sender, CalculationEventArgs e);
public event UpdateCalculation OnUpdateCalculation;





在第二种形式的某种方法中



In some method in your second form

void SomeMethod()
{
  if (OnUpdateCalculation != null)
    OnUpdateCalculation(this, new CalculationEventArgs(25.0));
}





主要形式:



In main form:

Form2 frm2 = new Form2();
frm2.OnUpdateCalculation += OnUpdateCalculation_fired;


参考:表格之间传递数据 - 如何在vbnet之间传递数据 - 形式 [ ^ ]


这篇关于如何通过主表单从第二个表单获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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