C# 使用SQL在两个表单之间传递数据

C# 使用SQL在两个表单之间传递数据,c#,mysql,C#,Mysql,我正在尝试使用reader传递另一个表单。 此代码来自Form1 if (count == 1) // IF Ok. { userLabel.Text = myReader[0].ToString(); // SHOW THE USERNAME loginSuccessTimer1.Enabled = true; // FOR TIMER LoginFormSuccessBG

我正在尝试使用reader传递另一个表单。 此代码来自Form1

if (count == 1) // IF Ok.
            {
                userLabel.Text = myReader[0].ToString(); // SHOW THE USERNAME
                loginSuccessTimer1.Enabled = true; // FOR TIMER
                LoginFormSuccessBG loginSuccess = new LoginFormSuccessBG();
                loginSuccess.Show(); //LoginSuccess SHOW FORM
            }
 private void button2_Click(object sender, EventArgs e)
    {
        userLabel2.Text = loginForm.userLabel.Text;
    }
此代码来自Form2。我想显示表单1中的文本

if (count == 1) // IF Ok.
            {
                userLabel.Text = myReader[0].ToString(); // SHOW THE USERNAME
                loginSuccessTimer1.Enabled = true; // FOR TIMER
                LoginFormSuccessBG loginSuccess = new LoginFormSuccessBG();
                loginSuccess.Show(); //LoginSuccess SHOW FORM
            }
 private void button2_Click(object sender, EventArgs e)
    {
        userLabel2.Text = loginForm.userLabel.Text;
    }
但是如果我点击表单2上的按钮2;我在Visual Studio上遇到以下错误:

An unhandled exception of type 'System.NullReferenceException' occurred in Launcher.exe Additional information: Object reference not set to an instance of an object.
我已经将userLabel设置为public,并在Form2上进行了尝试

userLabel2.Text = loginForm.userLabel.ToString();

但它不起作用。始终给出此错误。

您可以将Form1作为参数传递给Form2的构造函数。然后,如果您使用了userLabel public,那么您可以从Form2访问它。以下是一个例子:

表格1代码:

public Form1()
{
    InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
    Form2 frm = new Form2(this);
    frm.Show();
}
表格2代码:

Form1 form1;
public Form2(Form1 sender)
{
    InitializeComponent();
    form1 = sender;
}

private void button1_Click(object sender, EventArgs e)
{
    string text = form1.label1.Text;
}

这应该行得通,我只是在一个测试应用程序中做的

userLabel2.Text =
    (Application.OpenForms["yourForm1"] as yourForm1).userLabel.Text;

很遗憾,没有,LoginForm=sender;部分原因是问题。Visual studio不接受它,变量olarak algılıyor:p您应该更改变量名称。可能您是这样定义的:“LoginForm LoginForm;”如果您将第二个单词的第一个字符更改为小写,它应该可以工作。(非:德伊肯·伊斯梅尔·蒂普·伊斯米·艾因·奥尔马斯)