Solved

data transfer from sub form to main form

Posted on 2011-09-27
6
361 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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 250 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 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

Technology Partners: 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!

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

729 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