HI,

How to use multiple forms in c# windows application?
Where i need to fetch the information form "Form2" to "Form1". Here Form2 is used to get information from user and again those informations will be used in Form1.

Please suggest me to do this.

Thanks a lot,
Anjali :)

Dani AI

Generated

A clean way to pass data from Form2 back to Form1 is to keep Form2’s controls private and expose typed, read-only properties. Show Form2 as a modal dialog, set a DialogResult on OK/Cancel, then read the properties from the same instance you showed. This follows what and suggested, but avoids making controls public and keeps Form1 decoupled from Form2’s UI.

// Form2.cs
public partial class Form2 : Form
{
    public string UserValue => txtValue.Text.Trim();

    public Form2()
    {
        InitializeComponent();
        btnOk.Click += (s, e) => this.DialogResult = DialogResult.OK;
        btnCancel.Click += (s, e) => this.DialogResult = DialogResult.Cancel;
    }
}
// Form1.cs
using (var dlg = new Form2())
{
    if (dlg.ShowDialog(this) == DialogResult.OK)
    {
        string value = dlg.UserValue; // use it in Form1
    }
}

Common pitfalls that match issues in this thread:

  • Blank values usually mean you either called Show (modeless) and read immediately, or you read from a different/new instance. Always read after ShowDialog returns and from the same variable you showed, .
  • Do not turn controls public just to read them, . Prefer properties like above.
  • Access members on the instance, not the class type, which is why ’s note to fixed the protection/static confusion.

Bonus for : to show all ListBox items in a MessageBox:

var lines = listBox1.Items.Cast<object>().Select(x => x.ToString());
MessageBox.Show(string.Join(Environment.NewLine, lines), "List contents");

Recommended Answers

All 9 Replies

You mean MDI? or just you need to open Form2 from Form1 and use some values in Form1 user entered in Form2??

For second case, just create another Form let say it called Form2, some public members (TextBox, ListBox, etc...)

Add this code in Form1 to open Form2

Form2 form2 = new Form2();
fom2.ShowDialog();//enforces user to not back to form1 unless finishing work on form2

Get the value of form2's textbox

string str = form2.textbox2.Text;

Set properties in the form2, and then retrieve them when form1 becomes active again.

Its displaying blank values for entered textboxes in Form2.
When Form2 will disappear after entering the values, then how it will load the textbox values in Form1?
What we need to add in Form2?

Please suggest me for this.

Thanks a lot,
Anjali.....

Hey its working fine now...
Thanks a lot for your suggestions.

Thanks,
Anjali ..... :) :)

"Set properties in the form2, and then retrieve them when form1 becomes active again"

Which proerty i have to set, can u please guide me.

Thanks
Chirag

I get this:

private void button2_Click(object sender, EventArgs e)
        {
            Form3 form3 = new Form3(); form3.ShowDialog();
            string str = [U]Form3.textBox1[/U].Text;
        }

It tells me that "Mail.Form3.textBox1 Is inaccesable due to its pro tsction level."

PLEASE HELP!!!

Because you said "Form3.textBox1.Text" you're not accessing the variable of the form you just created... you're trying to access the class direct, change "Form3.textBox1" to "form3.textBox1"

I get this:

private void button2_Click(object sender, EventArgs e)
        {
            Form3 form3 = new Form3(); form3.ShowDialog();
            string str = [U]Form3.textBox1[/U].Text;
        }

It tells me that "Mail.Form3.textBox1 Is inaccesable due to its pro tsction level."

PLEASE HELP!!!

c.v.burgess, you may need to set protection level of textbox to private or declare properties/functions to get its text. To change protection level, select the control and from properties window change the value of "Modifiers" from private to public.

consider my data are in listbox. when i press to button1, i want all that data are in messagebox. what should i do. plz help me

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.