Solved

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

Posted on 2012-03-12
7
241 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 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

11 Experts available now in Live!

Get 1:1 Help Now