将文本框中的数字转换为另一种形式的标签c#windows窗体

时间:2013-07-23 13:08:23

标签: c# forms textbox transfer labels

你们如何以正确的方式将表格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最终未得到处理。我不知道如何进一步编码。我需要帮助

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分: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.