如何在Form1上创建一个标签,从form2中说出“Hello”?

时间:2021-01-09 00:58:17

That's it. It's a dumb dumb (embarrassing!) question, but I've never used C# before, only C++ and I can't seem to figure out how to access a Label on my main form from a secondary form and change the text. If anybody can let me know real quick what to do I'd be so grateful!

而已。这是一个愚蠢的(令人尴尬的!)问题,但我以前从未使用过C#,只有C ++,我似乎无法弄清楚如何从辅助表单访问主表单上的Label并更改文本。如果有人能让我快速了解该怎么做我会非常感激!

BTW, I should really clarify. Sorry: I've got two separate .cs files that each look about like below. I was using the [Designer] in VS2008 to add in the label in Form1. When I type something like Form1.label1 it doesn't understand. The dropdown shows a list of methods and properties for Form1, but there's only about 7, like ControlCollection, Equals, MouseButtons, and a couple others... I can publicly define a variable in Form1 and that shows, but I don't know how to access the label...

顺便说一下,我应该澄清一下。对不起:我有两个单独的.cs文件,每个文件看起来如下所示。我在VS2008中使用[Designer]在Form1中添加标签。当我输入类似Form1.label1的东西时,它不明白。下拉列表显示了Form1的方法和属性列表,但是只有大约7个,比如ControlCollection,Equals,MouseButtons和其他几个......我可以在Form1中公开定义一个变量并显示,但我不知道如何访问标签......

namespace AnotherProgram
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

4 个解决方案

#1


4  

You'll need a reference to the instance of Form1 - for example, if it's Form1 which is constructing Form2, you might pass this in as a constructor parameter.

您需要对Form1实例的引用 - 例如,如果它是构造Form2的Form1,您可以将其作为构造函数参数传递。

Then you'll need to either expose the label of Form1 via a property (or - ick! - a non-private field) or write a method/property which will set the text of the label. For example:

然后,您需要通过属性(或 - ick! - 非私有字段)公开Form1的标签,或者编写将设置标签文本的方法/属性。例如:

public class Form1 : Form 
{
    private Label label;

    // Construction etc as normal

    public string LabelText
    {
         get { return label.Text; }
         set { label.Text = value; }
    }

    public Form2 CreateForm2()
    {
        return new Form2(this);
    }
}

public class Form2 : Form
{
    private Form1 form1;

    public Form2(Form1 form1)
    {
        this.form1 = form1;
        // Normal construction
    }

    public void SayHello()
    {
        form1.LabelText = "Hello";
    }
}

#2


0  

You have to have a reference to Form1 from Form2. Then you need either a method or a property on Form 1 visible to Form2 that changes the label text to "Hello".

您必须从Form2引用Form1。然后,您需要Form2可见的Form 1上的方法或属性,将标签文本更改为“Hello”。

#3


0  

Public Class Form1
    Inherits Form


    Friend label1 As New Label


    Public Sub openForm2()
        Dim f As New Form2(Me)
        f.Show()
    End Sub


End Class


Public Class Form2
    Inherits Form


    Private _ref As Form1


    Public Sub New()
        _ref = Nothing
    End Sub


    Public Sub New(ByVal formRef As Form1)
        _ref = formRef
    End Sub


    Public Sub accessLabel(ByVal setText As String)
        If (_ref IsNot Nothing) Then
            _ref.label1.Text = setText
        Else
            Throw New NullReferenceException("_ref is NULL")
        End If
    End Sub


End Class

#4


0  

I think that delegates is the most powerful option, besides having properties in a Form: http://www.c-sharpcorner.com/UploadFile/mosessaur/winformsdelegates09042006094826AM/winformsdelegates.aspx

我认为代表是最强大的选择,除了在表单中具有属性:http://www.c-sharpcorner.com/UploadFile/mosessaur/winformsdelegates09042006094826AM/winformsdelegates.aspx

In the second form I define:

在第二种形式中我定义:

public delegate void AddItemDelegate(string item);
public AddItemDelegate AddItemCallback;

And from the form that opened it I write:

从打开它的表格我写道:

private void btnScenario2_Click(object sender, EventArgs e)
{

    FrmDialog dlg = new FrmDialog();
    //Subscribe this form for callback
    dlg.AddItemCallback = new AddItemDelegate(this.AddItemCallbackFn);
    dlg.ShowDialog();

}
private void AddItemCallbackFn(string item)
{

    lstBx.Items.Add(item);

}

#1


4  

You'll need a reference to the instance of Form1 - for example, if it's Form1 which is constructing Form2, you might pass this in as a constructor parameter.

您需要对Form1实例的引用 - 例如,如果它是构造Form2的Form1,您可以将其作为构造函数参数传递。

Then you'll need to either expose the label of Form1 via a property (or - ick! - a non-private field) or write a method/property which will set the text of the label. For example:

然后,您需要通过属性(或 - ick! - 非私有字段)公开Form1的标签,或者编写将设置标签文本的方法/属性。例如:

public class Form1 : Form 
{
    private Label label;

    // Construction etc as normal

    public string LabelText
    {
         get { return label.Text; }
         set { label.Text = value; }
    }

    public Form2 CreateForm2()
    {
        return new Form2(this);
    }
}

public class Form2 : Form
{
    private Form1 form1;

    public Form2(Form1 form1)
    {
        this.form1 = form1;
        // Normal construction
    }

    public void SayHello()
    {
        form1.LabelText = "Hello";
    }
}

#2


0  

You have to have a reference to Form1 from Form2. Then you need either a method or a property on Form 1 visible to Form2 that changes the label text to "Hello".

您必须从Form2引用Form1。然后,您需要Form2可见的Form 1上的方法或属性,将标签文本更改为“Hello”。

#3


0  

Public Class Form1
    Inherits Form


    Friend label1 As New Label


    Public Sub openForm2()
        Dim f As New Form2(Me)
        f.Show()
    End Sub


End Class


Public Class Form2
    Inherits Form


    Private _ref As Form1


    Public Sub New()
        _ref = Nothing
    End Sub


    Public Sub New(ByVal formRef As Form1)
        _ref = formRef
    End Sub


    Public Sub accessLabel(ByVal setText As String)
        If (_ref IsNot Nothing) Then
            _ref.label1.Text = setText
        Else
            Throw New NullReferenceException("_ref is NULL")
        End If
    End Sub


End Class

#4


0  

I think that delegates is the most powerful option, besides having properties in a Form: http://www.c-sharpcorner.com/UploadFile/mosessaur/winformsdelegates09042006094826AM/winformsdelegates.aspx

我认为代表是最强大的选择,除了在表单中具有属性:http://www.c-sharpcorner.com/UploadFile/mosessaur/winformsdelegates09042006094826AM/winformsdelegates.aspx

In the second form I define:

在第二种形式中我定义:

public delegate void AddItemDelegate(string item);
public AddItemDelegate AddItemCallback;

And from the form that opened it I write:

从打开它的表格我写道:

private void btnScenario2_Click(object sender, EventArgs e)
{

    FrmDialog dlg = new FrmDialog();
    //Subscribe this form for callback
    dlg.AddItemCallback = new AddItemDelegate(this.AddItemCallbackFn);
    dlg.ShowDialog();

}
private void AddItemCallbackFn(string item)
{

    lstBx.Items.Add(item);

}