Solved

data transfer from sub form to main form

Posted on 2011-09-27
6
359 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 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 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
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!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

735 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