0

你们如何以正确的方式将表格 2(双精度)文本框中的数值实际传输到另一种表格(表格 1)中的标签,这就是我所做的:

//Form 2
private void btnok_Click(object sender, EventArgs e)
    {
        double exchange;
        exchange = Double.Parse(txtcurrent.Text);
        this.ownerForm.PassValue(txtcurrent.Text);
        this.Close();
    }
//Form 1
public void PassValue(string strValue)
    {
        lblexchange.Text = strValue;
    }
private void update_Click(object sender, EventArgs e)
    {
        if (fromcountry.Text == tocountry.Text)
        {
            MessageBox.Show(" Please Choose Two Different Currencies ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        else
        {
            btnconvert.Enabled = true;
            Exchange_Rate frm = new Exchange_Rate();
            frm.Show();

        }

最后我得到了 NullReferenceException 未处理。我不知道如何进一步编码。我需要帮助

4

2 回答 2

0

您必须传递Form1对方法的引用Show。然后使用`thi

btnconvert.Enabled = true;
Exchange_Rate frm = new Exchange_Rate();
frm.Show(this);

接着:

private void btnok_Click(object sender, EventArgs e)
{
    double exchange;
    exchange = Double.Parse(txtcurrent.Text);

    var frm = (Form1)this.Owner;
    frm.PassValue(txtcurrent.Text);

    this.Close();
}
于 2013-07-23T13:13:45.090 回答
0

请按照以下步骤操作: (1) 对于第一个打开的 form2 设计文件,其中放置了标签,并将其声明从私有更改为公开。

Than from form1, call directly to form2.label --> 

    form2.lblexchange.text = "PASS VALUE"

In this case, need to open form2 without creating object of form2.

If you want to create object of form2 for open form than at that time do code as below:



        Form2 obj = new Form2();
        obj.lblexchange.text = "PASS VALUE";
        obj.show();

Enter code here:

(2) method to solve using module. (VB project)
This method is very simple to use. Below:
--> Create module file enter code here.
--> Declare public variable (var1) in module file.
--> Set public variable (var1) from form1.
--> Form2_load event - set label (lblexchange) to var1.

I think above code will help you.
于 2013-07-23T13:29:09.030 回答