How do get the content(Text or Numbers) of a text box in a windows form and take it to the next windows form and use it there.

Dani AI

Generated

A few simple, practical patterns beyond the constructor trick already suggested by (and the parent-reference option from ). Each fits different needs: one-way handoff, synchronous return, or live two-way updates.

Property (simple one-way)
Set a public property on the child before showing it; the child reads that property on Load.

// Form2
public string IncomingText { get; set; }

public Form2()
{
    InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
    textBox1.Text = IncomingText ?? string.Empty;
}

// Caller
var f2 = new Form2 { IncomingText = textBox1.Text };
f2.Show();

Modal return (caller reads a result after the dialog closes)
Use ShowDialog and a public Result property on the child. This is good when the parent needs a value back immediately after the child closes.

// Form2
public string ResultText { get; private set; }

private void okButton_Click(object sender, EventArgs e)
{
    ResultText = textBox1.Text;
    DialogResult = DialogResult.OK;
    Close();
}

// Caller
using (var f2 = new Form2())
{
    if (f2.ShowDialog(this) == DialogResult.OK)
        textBox1.Text = f2.ResultText;
}

Event/callback (decoupled two-way communication)
Expose an event or use an interface so the child can notify the parent without holding a concrete reference.

// Form2
public event EventHandler<ValueEventArgs> ValueSubmitted;

private void sendButton_Click(object sender, EventArgs e)
{
    ValueSubmitted?.Invoke(this, new ValueEventArgs { Value = textBox1.Text });
    Close();
}

// Caller
var f2 = new Form2();
f2.ValueSubmitted += (s, ev) => textBox1.Text = ev.Value;
f2.Show();

Tips and cautions: prefer constructor/property for simple transfers, ShowDialog for synchronous returns, events/interfaces for decoupling. Avoid static globals for passing values. Always call InitializeComponent() before touching controls; unsubscribe events if lifetimes differ to prevent leaks.

Recommended Answers

All 5 Replies

pass the textbox as a parameter which will be used in form2

Form1

form2 form2 = new form2(textBox1.Text)
form2.Show();

Then make a new constructor for the other form, with string parameter and assign this parameter to your textbox (form2).

Form2

Form2(string parameter)
{
textBox1.Text = parameter;
}

Thanx a lot AngelicOne

Sorry but form2 is incorrect. Put the new constructor where initializecomponent is..

Form2(string parameter)
{
InitializeComponent();
textBox1.Text = parameter;
}

something similar how to send back information to the calling form

if you want to pass information back and forth the easiest way is to pass a reference to the parent form into the child form. See my tutorial.

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.