Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-03-12
7
Medium Priority
?
247 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 2000 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
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

715 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