Member Avatar for Rahul47

Hello people,

I need to do this:
I have Form1 having textbox1. I want to display text entered into textbox1 into Label1 of Form2.

Suggestions Welcomed.

Thanks

Recommended Answers

All 8 Replies

Define a property in Form2 that will hold that string, then assign to it and refresh()/invalidate() the control in Form2 whenever you want (ex. in the validated event of the textbox in Form1). For example, in Form2:

public string MyText {
    get { return textBox1.Text; }
    set {
        textBox1.Text = value;
        textBox1.Invalidate();
    }
}

And in Form1 (assuming a Form2 object has been created):

private void textBox1_Validated(object sender, EventArgs e)
{
    _form2.MyText = textBox1.Text;
}

Edit: My bad, I thought this was the C# forum. But the solution is identical, just use VB.NET instead of C#. ;)

how are you displaying form2? I do it with a button click in form1. So after creating an instance of form2 you just assign the text from form1.textox1.text to form2.label1.text

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim fm2 As Form2 = New Form2
        fm2.label1.Text = Me.textbox1.Text
        fm2.ShowDialog()
    End Sub

So after creating an instance of form2 you just assign the text from form1.textox1.text to form2.label1.text

Only if the control is explicitly made public in Form2. By default new controls added through the designer are private (as they should be!), and in a code review I'd question making them public.

I didn't change anything like that. In fact I made no code changes at all to Form2. Here's the application declarations, all code generated by vb.net

    Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(112, 94)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(39, 13)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Label1"
        '
        'Form2
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(284, 262)
        Me.Controls.Add(Me.Label1)
        Me.Name = "Form2"
        Me.Text = "Form2"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

Only if the control is explicitly made public in Form2. By default new controls added through the designer are private (as they should be!), and in a code review I'd question making them public.

I'm not looking to jump into the fray; just providing some information.

For C# the default control access modifier is "private", for VB the default access modifier is "Friend" (internal).

In your Form2

label1.Text = Form1.textbox1.Text

create a variable that will hold the text in texbox1 in Form1
Dim x as String = TextBox1.Text

then on Form2 you can import
Imports ProjectName.Form1

with that you can
Label1.Text = Form2.x

or simply
Label1.Text = x
though i always include the form name as to not get confused ..

well i'm not particularly sure with the Import thing but that's what i learned from a certain site, or perhaps its not actually needed, anyways its not that hard so you can do it :) plus you can always use modules when you have functions that are used repetitively or to maintain the orderliness of your program.good luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.