Reputation: 133
I am having a form (parent form) which is invoking another form (child form).
In child form I am doing some changes in the text box, and there is label in the the parent form, where I want to display those changes dynamically.
what I am doing right now is:
NewForm newForm = new NewForm(this);
newForm.Parent = this;
newForm.ShowDialog(this);
I am not sure this the right way to do...
But I am not able to get the control from this.Parent in the child form.
Please help me with the same.
Thanks in advance...
Upvotes: 1
Views: 6223
Reputation: 266
First in your parent form add a property that will update the label.
public string LabelonParent {
get { return label1.Text;}
set { label1.Text=value;}
}
now add the event method that will display the child form
private void button1_Click(object sender, EventArgs e)
{
ChildClass form = new ChildClass();
DialogResult result = form.ShowDialog(this);
}
in the child form add textchanged event method.
private void textBox1_TextChanged(object sender, EventArgs e)
{
((ParentClass)this.Owner).LabelonParent = textBox1.Text;
}
you may even want to add a formload event method to populate the textBox1 with the label1.Text
private void ChildClass_Load(object sender, EventArgs e)
{
textBox1.Text = ((ParentClass)this.Owner).LabelonParent;
}
Upvotes: 4
Reputation: 6849
There are lot off way to do this. for example assign the control modifier property to public that can make that control accessible in another class. another way is you can create a property or public variable in child form and assign the value on textchange. this value can be return after ShowDialog() function call. even you can make parent form label control modifier property to public so you can access that label directly in child form.
But, assigning a control modifier public is not the proper way.
Another logic is you can create a public event in child form and that will be handled by parent form. this event will be executed when the text has been changed.
frmChild child = new frmChild();
frmChild.Lable_TextChanged += new EventHandler(child_TextChanged);
frmChild.ShowDialog();
//lbl which will be placed in parent form
private void child_TextChanged(object sender, EventArgs e)
{
//sender will return textbox from child form
lbl.Text = ((TextBox)sender).Text;
}
code in child form:
public event EventHandler Lable_TextChanged;
//txtText which will be placed in child form
private void txtText_TextChanged(object sender, EventArgs e)
{
if (Lable_TextChanged != null)
Lable_TextChanged(sender, e);
}
Upvotes: 1
Reputation: 702
Below is the best way to achieve this functionality and for real time update text on Child form and reflected in Parent form
Child Form Code
public delegate void PassText(string textValue);
public event PassText RaisePassTextEvent;
private void textBox_TextChanged(object sender, EventArgs e)
{
if (RaisePassTextEvent != null)
{
RaisePassTextEvent(textBox.Text);
}
}
Main/Parent Form Code
Child oChild = new Child();
oChild.RaisePassTextEvent += oChild_RaisePassTextEvent;
oChild.MdiParent = this;
oChild.Show();
void oChild_RaisePassTextEvent(string textValue)
{
this.Invoke(new Action(() => lableControl.Text = textValue));
}
Upvotes: 1
Reputation: 1010
You can expose a public string LabelText { get; set; }
in your child class.
When you are closing the diaglog update the LabelText
from child to parent.
check the following code
class ChildClass
{
public string LabelText { get; set; }
//Update the above property in suitable method.
public string ChildMethod()
{
LabelText = "XXXX";
}
}
class ParentClass
{
public string ParentLabelText { get; set; }
public string ParentMethod()
{
var childObj = new ChildClass();
DialogResult result = ShowDialog();
if (result == DialogResult.Cancel)
{
//update your parent each time you are closing the showdialog window.
this.ParentLabelText = childObj.LabelText;
}
}
}
Thanks Srikanth
Upvotes: 0
Reputation: 188
This is right way to achieve this. But if you want to get to specific custom property use casting like ((ParentForm)newForm.Parent).CustomProp
, and remember to use Invoke()
, because every form has own thread.
Upvotes: 0