Solved

data transfer from sub form to main form

Posted on 2011-09-27
6
353 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
  • 3
  • 2
6 Comments
 
LVL 85

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 85

Expert Comment

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

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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 85

Accepted Solution

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

        public Form2()
        {
            InitializeComponent();
        }

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

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 250 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
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.
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

759 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

20 Experts available now in Live!

Get 1:1 Help Now