?
Solved

data transfer from sub form to main form

Posted on 2011-09-27
6
Medium Priority
?
371 Views
Last Modified: 2012-05-12
Hi, I am trying to transfer variable data from sub form to main form. What i meant is, I did call Form2.show() from Form1. In form2 i have button, by clicking that data has to update autoamtically in textbox in Form1.
0
Comment
Question by:TITO2009
[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
6 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36714194
Use ShowDialog() so that Form1 code execution stops until Form2 is closed.

When code execution returns to Form1 you simply use your reference to your instance of Form2 to grab the data.

For instance, create Form1 with a Label and a Button.
Now add a TextBox and a Button to Form2.
On Form2, select the TextBox and set the Modifiers() Property to Public.

In this example, the data entered into the Form2 TextBox will be entered into the Form1 Label when Form2 is closed:
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            if (f2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.label1.Text = f2.textBox1.Text;
            }
        }
    }

Open in new window

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

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

    }

Open in new window


*A more formal approach would wrap the TextBox in Form2 in a ReadOnly property instead of exposing it via the Modifiers() Property...but this method is easier to understand for beginners.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36714197
Here's a screencast of it in action:
Idle-Mind-505116.flv
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 36714234
If you want another approach, without  dialogs, try the code below. You just supply a reference to a Form 1 for a Form 2 (using a constructor).
public partial class Form1 : Form
    {
        public Form1(string p1, string p2)
        {
            InitializeComponent();
        }


        Form2 frm = null;
        private void button2_Click(object sender, EventArgs e)
        {
            if (frm == null)
            {
                frm = new Form2(this);
                frm.Show();
            }
            frm.Focus();
        }
        public void UpdateTexBoxContent(string vardata)
        {
             this.textBox1.Text = vardata;
        }
}
=========================
    public partial class Form2 : Form
    {
        Form1 parent = null;
        
        public Form2(Form1 _parent)
        {
            InitializeComponent();
            this.parent = _parent;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            parent.UpdateTexBoxContent("Hello from form2");
        }
    }

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:TITO2009
ID: 36715147
Hi, I am looking for without closing any forms. While trying anarki solution i did got error.

WindowsFormsApplication2.Form1' does not contain a constructor that takes '0' arguments      
namespace WindowsFormsApplication2
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Open in new window

0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 36716727
Leave the default instructor in Form2:

        public Form2()
        {
            InitializeComponent();
        }

        public Form2(Form1 _parent)
        {
            InitializeComponent();
            this.parent = _parent;
        }
0
 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 1000 total points
ID: 36720099
Forgive me - just change the constructor for the form 1 - it just came from another task...
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        Form2 frm = null;
        private void button2_Click(object sender, EventArgs e)
        {
            if (frm == null)
            {
                frm = new Form2(this);
                frm.Show();
            }
            frm.Focus();
        }
        public void UpdateTexBoxContent(string vardata)
        {
             this.textBox1.Text = vardata;
        }
}

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

762 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