Solved

c# I need one form to populate a text in another

Posted on 2012-03-12
7
242 Views
Last Modified: 2012-03-30
When I click on a button on Form1 I need it to populate a textbox in Form2.  How can I do this?
0
Comment
Question by:pdering
  • 3
  • 2
  • 2
7 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 37712738
For that you need to have a reference from one form to another. Are these form parent - child forms? Please explain the scenario.
0
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 37712796
OK, I have put a simple project for you:

Two forms.
Form1 has
 - a label, (label1)
 - a button to display a child Form2, (btnShowForm2)
 - a button to display a message on a child form (btnShowMsgOnChild)

Form 2 has:
 - a button to show messages on a parent (ShowMsgOnParent)
 - a label (label1)

See the code below. It has comments, hope this will help:

    public partial class Form1 : Form
    {
        // reference to a child form
        private Form2 childForm = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnShowForm2_Click(object sender, EventArgs e)
        {
            // create child and show
            childForm = new Form2(this);
            btnShowForm2.Enabled = false;
            childForm.Show();
        }

        private void btnShowMsgOnChild_Click(object sender, EventArgs e)
        {
            // we send this message from parent; you may read it from textbox, e.g.
            childForm.ShowMessageFromParent("Hello from parent form");
        }

        // this method is invoked by a child form
        public void ShowMessageFromChild(string s)
        {
            this.label1.Text = s;
        }
    }

Open in new window


    public partial class Form2 : Form
    {
        Form1 parentForm = null;// reference to parent form


        // See, this is NOT a standard default constructur!
        public Form2(Form1 parentForm)
        {
            InitializeComponent();
            this.parentForm = parentForm;// set reference to parent form
        }

        private void ShowMsgOnParent_Click(object sender, EventArgs e)
        {
            // show message on parent; instead of hardcoding you may read it from textbox
            parentForm.ShowMessageFromChild("this is a message from child form");
        }

        // this method is invoked by a parent form
        public void ShowMessageFromParent(string msg)
        {
            this.label1.Text = msg;
        }
    }

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37712979
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 37715190
The following will do that for you, it passes the parameter to the form and adds it to the text box.

This would be your code in your first form:


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
      
        private void btnButton_Click(object sender, EventArgs e)
        {
           Form2 mySecondForm = new Form2("Whatever your data is");
         mySecondForm.ShowDialog();
        }
   }


---

And this is the code for the second form:


    public partial class Form2 : Form
    {
        public Form2(string myData)
        {
            InitializeComponent();
          
        }

      private void frmMain_Load(object sender, EventArgs e)
        {
            txtMyTextBox.Text = myData;
      }
   }
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 37716757
Wow!

Passing text information through constructor... I suspect this approach is not flexible...
0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 37738713
Just a different way of going about the OPs question.  And it's flexability is based on what exactly the OP wants to actually achieve.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37788473
Should I take that as my article was too long?   : \
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question