Rama Tito
asked on
Send data from form to Sub form
I have Form1, form2 and Subform2. The code is working but i need to transfer more data from line 82 to Form1.
I got data type in date, int and text. I have to send this parameters from SubForm2 to Form1,
I got data type in date, int and text. I have to send this parameters from SubForm2 to Form1,
namespace Main_Forms
{
public partial class Form1 : Form
{
Form2 frm2;
Sub_From.SubForm2 Sfrom;
public string answer;
public Form1()
{
InitializeComponent();
button2.Enabled = false;
textBox1.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
button2.Enabled = true;
textBox1.Enabled = true;
frm2 = new Form2();
if(frm2.ShowDialog()==DialogResult.Yes)
{
Sfrom = new Sub_From.SubForm2();
Sfrom.FormClosed += new FormClosedEventHandler(SubForm_FormClosed);
Sfrom.Show(this);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (Sfrom != null)
{
Sfrom.Close();
//Sfrom.Dispose();
Sfrom = null;
}
}
private void SubForm_FormClosed(object sender, FormClosedEventArgs e)
{
textBox1.Text = Sfrom.valueans;
Sfrom.Dispose();
Sfrom = null;
}
}
}
============================================================================
namespace Main_Forms
{
public partial class Form2 : Form
{
//Sub_From.SubForm2 Sform;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Yes;
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
===========================================================================
namespace Sub_From
{
public partial class SubForm2 : Form
{
public string valueans
{
get { return textBox1.Text; }
}
public SubForm2()
{
InitializeComponent();
}
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you create public property you get access to data from subform. This data belongs to instance of the subform (not static/global variable), you can get access to this data if you have access to instance of subform.
You can also create internal properties if you will be using that subform inside project (dll).
You can also create internal properties if you will be using that subform inside project (dll).
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try this code I have modified your original code and replace it with this one.Please check this :
namespace Main_Forms
{
public partial class Form1 : Form
{
Form2 frm2;
Sub_From.SubForm2 Sfrom;
public string answer;
public Form1()
{
InitializeComponent();
button2.Enabled = false;
textBox1.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
button2.Enabled = true;
textBox1.Enabled = true;
frm2 = new Form2();
if(frm2.ShowDialog()==DialogResult.Yes)
{
Sfrom = new Sub_From.SubForm2(this); //**Changes**
Sfrom.FormClosing += new FormClosingEventHandler(SubForm_FormClosing);
Sfrom.Show();
}
}
private void button2_Click(object sender, EventArgs e)
{
if (Sfrom != null)
{
Sfrom.Close();
//Sfrom.Dispose();
Sfrom = null;
}
}
private void SubForm_FormClosing(object sender, FormClosingEventArgs e)
{
SubForm2 sub2 = (SubForm2)sender;//**Changes**
sub2.Hide();//**Changes**
this.Show();//**Changes**
this.textBox1.Text = sub2.valueans; //**Changes**
}
}
}
============================================================================
namespace Main_Forms
{
public partial class Form2 : Form
{
//Sub_From.SubForm2 Sform;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Yes;
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
===========================================================================
namespace Sub_From
{
public partial class SubForm2 : Form
{
public string valueans
{
get { return textBox1.Text; }
}
public SubForm2()
{
InitializeComponent();
}
Form parentFrm;//**Changes**
//**Changes**
public SubForm2(Form parent)
{
InitializeComponent();
parentFrm = parent;
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER