将文本框中的数字转移到另一个窗体c#windows窗体中的标签

本文关键字:窗体 另一个 标签 c#windows 数字 文本 转移 | 更新日期: 2023-09-27 18:24:21

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

将文本框中的数字转移到另一个窗体c#windows窗体中的标签

您必须将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) 对于放置标签的form2的第一个打开设计文件,并将其声明从private更改为public。

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.