C# 在两个窗体之间传递字符串时出错

C# 在两个窗体之间传递字符串时出错,c#,forms,winforms,C#,Forms,Winforms,我正在使用C#开发一个windows窗体应用程序,并尝试将字符串从一个窗体传递到另一个窗体。我的字符串似乎传递到第二个表单中,但是,当我尝试在第二个表单上将该字符串显示为标签时,它不会显示该字符串。但是,当我尝试在第二个表单的消息框中显示它时,它会在消息框中显示传递的字符串。如何更改代码,以便使用传递的字符串在第二个表单中显示为标签 这是我的代码: 我的表格1包含: private void Report_Bug_Click(object sender, EventArgs e) {

我正在使用C#开发一个windows窗体应用程序,并尝试将字符串从一个窗体传递到另一个窗体。我的字符串似乎传递到第二个表单中,但是,当我尝试在第二个表单上将该字符串显示为标签时,它不会显示该字符串。但是,当我尝试在第二个表单的消息框中显示它时,它会在消息框中显示传递的字符串。如何更改代码,以便使用传递的字符串在第二个表单中显示为标签

这是我的代码:

我的表格1包含:

 private void Report_Bug_Click(object sender, EventArgs e)
    {
        ReportForm newForm = new ReportForm();
        string myString = "Hello World!";// string to be passed
        newForm.AuthUser = myString; //sending the string to the second form
        newForm.Show();
    }
public partial class ReportForm : Form
{
    public string AuthUser { get ; set; } //retrieving passed data

    public ReportForm()
    {
        InitializeComponent();
        populateListBox();
        userlabel.Text = AuthUser; //setting the label value to "Hello World!" - This doesn't work
    }

    private void Report_Submit(object sender, EventArgs e)
    {
        MessageBox.Show(AuthUser);// This displays a message box which says "Hello World!" so the string is passed
    }
}
我的表格2(报告表格)包含:

 private void Report_Bug_Click(object sender, EventArgs e)
    {
        ReportForm newForm = new ReportForm();
        string myString = "Hello World!";// string to be passed
        newForm.AuthUser = myString; //sending the string to the second form
        newForm.Show();
    }
public partial class ReportForm : Form
{
    public string AuthUser { get ; set; } //retrieving passed data

    public ReportForm()
    {
        InitializeComponent();
        populateListBox();
        userlabel.Text = AuthUser; //setting the label value to "Hello World!" - This doesn't work
    }

    private void Report_Submit(object sender, EventArgs e)
    {
        MessageBox.Show(AuthUser);// This displays a message box which says "Hello World!" so the string is passed
    }
}

如何更改代码,使标签“userlabel”显示我从第一个表单传递的字符串?

在设置AuthUser属性之前,先在表单中设置标签文本。您可以让ReportForm构造函数接受该字符串

public ReportForm(string labelText)
    {
        InitializeComponent();
        populateListBox();
        userlabel.Text = labelText;
    }

在这里,我假设您不再需要AuthUser了。

在设置AuthUser属性之前,先在表单中设置标签文本。您可以让ReportForm构造函数接受该字符串

public ReportForm(string labelText)
    {
        InitializeComponent();
        populateListBox();
        userlabel.Text = labelText;
    }

在这里,我假设您不再需要AuthUser了。

userlabel.Text=AuthUser
不应位于第二个表单的
ReportForm()
方法中。它是类的构造函数,在将
myString
分配给
newForm.AuthUser
之前执行。最简单的方法是放置
userlabel.Text=AuthUser。您还可以更改构造函数以接收该字符串作为参数,并在标签中显示它。

userlabel.Text=AuthUser
不应位于第二个表单的
ReportForm()
方法中。它是类的构造函数,在将
myString
分配给
newForm.AuthUser
之前执行。最简单的方法是放置
userlabel.Text=AuthUser。您还可以更改构造函数以接收该字符串作为参数,并将其显示在标签中。

在您在报告中设置AuthUser属性之前,将执行ReportForm上的构造函数。可以通过将字符串直接传递给重载构造函数来解决此问题:

public ReportForm() {}

public ReportForm(string authUser)
{
    this.AuthUser = authUser
    InitializeComponent();
    populateListBox();
    userlabel.Text = this.AuthUser;
}
private void Report_Bug_Click(object sender, EventArgs e)
{
    ReportForm newForm = new ReportForm("Hello World!");
    newForm.Show();
}
    string myString = "Hello World!";// string to be passed
    ReportForm newForm = new ReportForm(myString);


public ReportForm(string text)
{
    InitializeComponent();
    populateListBox();
    userlabel.Text = text; 
}
在Form1中,在构造函数中传递字符串:

public ReportForm() {}

public ReportForm(string authUser)
{
    this.AuthUser = authUser
    InitializeComponent();
    populateListBox();
    userlabel.Text = this.AuthUser;
}
private void Report_Bug_Click(object sender, EventArgs e)
{
    ReportForm newForm = new ReportForm("Hello World!");
    newForm.Show();
}
    string myString = "Hello World!";// string to be passed
    ReportForm newForm = new ReportForm(myString);


public ReportForm(string text)
{
    InitializeComponent();
    populateListBox();
    userlabel.Text = text; 
}

ReportForm上的构造函数在您在Report\u Bug\u单击中设置AuthUser属性之前执行。可以通过将字符串直接传递给重载构造函数来解决此问题:

public ReportForm() {}

public ReportForm(string authUser)
{
    this.AuthUser = authUser
    InitializeComponent();
    populateListBox();
    userlabel.Text = this.AuthUser;
}
private void Report_Bug_Click(object sender, EventArgs e)
{
    ReportForm newForm = new ReportForm("Hello World!");
    newForm.Show();
}
    string myString = "Hello World!";// string to be passed
    ReportForm newForm = new ReportForm(myString);


public ReportForm(string text)
{
    InitializeComponent();
    populateListBox();
    userlabel.Text = text; 
}
在Form1中,在构造函数中传递字符串:

public ReportForm() {}

public ReportForm(string authUser)
{
    this.AuthUser = authUser
    InitializeComponent();
    populateListBox();
    userlabel.Text = this.AuthUser;
}
private void Report_Bug_Click(object sender, EventArgs e)
{
    ReportForm newForm = new ReportForm("Hello World!");
    newForm.Show();
}
    string myString = "Hello World!";// string to be passed
    ReportForm newForm = new ReportForm(myString);


public ReportForm(string text)
{
    InitializeComponent();
    populateListBox();
    userlabel.Text = text; 
}

或者,根据Andy的建议,将字符串作为参数传递给构造函数:

public ReportForm() {}

public ReportForm(string authUser)
{
    this.AuthUser = authUser
    InitializeComponent();
    populateListBox();
    userlabel.Text = this.AuthUser;
}
private void Report_Bug_Click(object sender, EventArgs e)
{
    ReportForm newForm = new ReportForm("Hello World!");
    newForm.Show();
}
    string myString = "Hello World!";// string to be passed
    ReportForm newForm = new ReportForm(myString);


public ReportForm(string text)
{
    InitializeComponent();
    populateListBox();
    userlabel.Text = text; 
}

或者,根据Andy的建议,将字符串作为参数传递给构造函数:

public ReportForm() {}

public ReportForm(string authUser)
{
    this.AuthUser = authUser
    InitializeComponent();
    populateListBox();
    userlabel.Text = this.AuthUser;
}
private void Report_Bug_Click(object sender, EventArgs e)
{
    ReportForm newForm = new ReportForm("Hello World!");
    newForm.Show();
}
    string myString = "Hello World!";// string to be passed
    ReportForm newForm = new ReportForm(myString);


public ReportForm(string text)
{
    InitializeComponent();
    populateListBox();
    userlabel.Text = text; 
}

你能再解释一下你的答案吗ReportForm()是你的构造函数,你在这里设置了标签。当您调用新的ReportForm时,将调用它并设置标签。然后,设置AuthUser属性。如何更改代码,使其不存在冲突?有很多选项。我建议的一个方法是让构造函数(ReportForm)接受一个字符串参数,然后将其设置为标签。在这种情况下,您可能不再需要AuthUser属性。请您再解释一下您的答案。ReportForm()是您设置标签的构造函数。当您调用新的ReportForm时,将调用它并设置标签。然后,设置AuthUser属性。如何更改代码,使其不存在冲突?有很多选项。我建议的一个方法是让构造函数(ReportForm)接受一个字符串参数,然后将其设置为标签。在这种情况下,您可能不再需要AuthUser属性。非常感谢。这很好用。它甚至允许我在Reportform()之外使用字符串。再次,非常感谢:)非常感谢。这很好用。它甚至允许我在Reportform()之外使用字符串。再次,非常感谢:)