Solved

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

Posted on 2012-03-12
7
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 30

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 30

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 30

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where is this file? 3 32
How can I do Unit Testing with Session ? 1 23
orderby list (from Json) 1 28
ASP.NET data base connection 35 29
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

735 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