Solved

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

Posted on 2012-03-12
7
240 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 74

Expert Comment

by:käµfm³d 👽
ID: 37712979
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 74

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now