Solved

data transfer from sub form to main form

Posted on 2011-09-27
6
358 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Question! 4 36
converto datetime always changes format 9 37
Cant save 3D 4 21
application restarter  - for an exe that hangs 9 27
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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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…

810 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