communication between two child forms in C#
-
I want to perform some communication between two child forms that are each opened from a parent form (separate button to show each form). I want to change the visibility and value of a numericalUpDown and also the visibility and text of a textbox. I have used properties, the get part is working perfectly, but the set part works only on the child.show() event, e.g. when I click on Child1Form.button to show the Child2Form the "set" part works, but if I do not show Child2Form, it does not work. and i also do not want to use from setting files. I used form event handling. The method has gotten fired and the message has shown correctly, but the changes in text and visibility have not. On the parents side where I call the publisher form, means Child3 form:
Child3 child3 = new Child3();
Child1 child1 = new Child1();
child3.Child1Button2Clicked += child1.child3_Child1Button2Clicked;
child3.Show();On the subscriber(listener) side: Event Handling between two child forms that each open from the same parent (Two child - one parent).
public void child3_Child1Button2Clicked(object sender, FormsCommunication e)
{
NumericalUD1.Visible = e.NumericalUDPass;
textBox1.Text = e.textBoxPass;
MessageBox.Show("it got fire" + ": " + e.NumericalUDPass.ToString() + ": " + e.textBoxPass.ToString());
}On the publisher side:
public event EventHandler Child1Button2Clicked;
//button click event to set the values and fire the method.
private void Child3Btn1_Click(object sender, EventArgs e)
{
FormsCommunication formsCommunication = new
FormsCommunication("2782",Convert.ToBoolean(false));OnChild1Button2Clicked(formsCommunication);
}
protected virtual void OnChild1Button2Clicked(FormsCommunication e)
{
Child1Button2Clicked?.Invoke(this, e);
}//the constructor in the EventArgs Class:
public FormsCommunication(string textBox, bool NumericalUD)
{
textBoxPass = textBox;
NumericalUDPass = NumericalUD;
}public string textBoxPass { get; private set; }
public bool NumericalUDPass { get; private set; } -
I want to perform some communication between two child forms that are each opened from a parent form (separate button to show each form). I want to change the visibility and value of a numericalUpDown and also the visibility and text of a textbox. I have used properties, the get part is working perfectly, but the set part works only on the child.show() event, e.g. when I click on Child1Form.button to show the Child2Form the "set" part works, but if I do not show Child2Form, it does not work. and i also do not want to use from setting files. I used form event handling. The method has gotten fired and the message has shown correctly, but the changes in text and visibility have not. On the parents side where I call the publisher form, means Child3 form:
Child3 child3 = new Child3();
Child1 child1 = new Child1();
child3.Child1Button2Clicked += child1.child3_Child1Button2Clicked;
child3.Show();On the subscriber(listener) side: Event Handling between two child forms that each open from the same parent (Two child - one parent).
public void child3_Child1Button2Clicked(object sender, FormsCommunication e)
{
NumericalUD1.Visible = e.NumericalUDPass;
textBox1.Text = e.textBoxPass;
MessageBox.Show("it got fire" + ": " + e.NumericalUDPass.ToString() + ": " + e.textBoxPass.ToString());
}On the publisher side:
public event EventHandler Child1Button2Clicked;
//button click event to set the values and fire the method.
private void Child3Btn1_Click(object sender, EventArgs e)
{
FormsCommunication formsCommunication = new
FormsCommunication("2782",Convert.ToBoolean(false));OnChild1Button2Clicked(formsCommunication);
}
protected virtual void OnChild1Button2Clicked(FormsCommunication e)
{
Child1Button2Clicked?.Invoke(this, e);
}//the constructor in the EventArgs Class:
public FormsCommunication(string textBox, bool NumericalUD)
{
textBoxPass = textBox;
NumericalUDPass = NumericalUD;
}public string textBoxPass { get; private set; }
public bool NumericalUDPass { get; private set; }Do yourself a favour and scrap that - form2 shouldn't even know form 3 exists, much less try to modify it's behaviour. See here: Transferring information between two forms, Part 3: Child to Child[^] Form1 needs to know they both exist because it opens them - so all communication goes via the parent.
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Do yourself a favour and scrap that - form2 shouldn't even know form 3 exists, much less try to modify it's behaviour. See here: Transferring information between two forms, Part 3: Child to Child[^] Form1 needs to know they both exist because it opens them - so all communication goes via the parent.
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
Thank you so much for your help. Is it safe to use from setting files in the user scope and share it between two text boxes for example?
-
Thank you so much for your help. Is it safe to use from setting files in the user scope and share it between two text boxes for example?
Why would you want to do something that cumbersome? Perhap you need to explain in more detail exactly what you are trying to do?
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Why would you want to do something that cumbersome? Perhap you need to explain in more detail exactly what you are trying to do?
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
Hi, thanks a million for your reply. Sometimes there are many data that i want to share between two forms. So it is easier to use from properties.settings.default (for me). that works very good and i can save and retrieve my data in the most simple way. So sometimes i prefer to use from them. setting files with user scope, are accessible all over the program directly. So i am doubtful about their security hazards. That is why i have asked the question.
-
Hi, thanks a million for your reply. Sometimes there are many data that i want to share between two forms. So it is easier to use from properties.settings.default (for me). that works very good and i can save and retrieve my data in the most simple way. So sometimes i prefer to use from them. setting files with user scope, are accessible all over the program directly. So i am doubtful about their security hazards. That is why i have asked the question.
Do you have any idea how inefficient that is? Every time you want to access items in memory, you will be going via a hard drive! You do realise that Properties.Settings is backed by an XML file, so every time you write to them there is a heck of a lot of "read the file, copy it up to the parameter we want, write the new value instead, copy the rest of the file" going on behind the scenes? And reading it means "read the file, scan it for the parameter we want, copy the value" each time? And that if you want to use the same class in two places, they will clash on the settings file because it only knows about one parameter? Start doing it a lot - and you will, because "it works" - and you will slug the heck out of your computer. Learn to do it properly, and it not only works better, but is considerably more maintainable.
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Do you have any idea how inefficient that is? Every time you want to access items in memory, you will be going via a hard drive! You do realise that Properties.Settings is backed by an XML file, so every time you write to them there is a heck of a lot of "read the file, copy it up to the parameter we want, write the new value instead, copy the rest of the file" going on behind the scenes? And reading it means "read the file, scan it for the parameter we want, copy the value" each time? And that if you want to use the same class in two places, they will clash on the settings file because it only knows about one parameter? Start doing it a lot - and you will, because "it works" - and you will slug the heck out of your computer. Learn to do it properly, and it not only works better, but is considerably more maintainable.
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
Hi, I do not know how should i thanks from you for your help and guide. Can you give me a link of an article that how should i save and retrive my data in the minimum time and what the best way is? .thanks a million.
-
Hi, I do not know how should i thanks from you for your help and guide. Can you give me a link of an article that how should i save and retrive my data in the minimum time and what the best way is? .thanks a million.
Well, the way I'd do it is to create a separate class to act as a "data transfer" class and fill an instance of that in the property getter. That way, all the related data is together, and in its correct data format ready to be used. You presumably know how to create a class?
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Well, the way I'd do it is to create a separate class to act as a "data transfer" class and fill an instance of that in the property getter. That way, all the related data is together, and in its correct data format ready to be used. You presumably know how to create a class?
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
Hi, As i have gotten your point, Yes i know how to create a class, and how to use from get and set. But my means is about save and retrieve data and not about sharing them. But if you means is a different thing, no unfortunately i have no idea about that and if so, do me a favour and guide me through it, please. Thaaaaaaaank you again .