Click to See Complete Forum and Search --> : Accessing parent members?
dannytan
March 17th, 2003, 06:45 AM
hi guys. i m working with multiple forms in my c# program. assuming i have 2 forms, form 1 and form 2, i have learnt that in order to access form2's textboxes in form1 while returning from the ShowDialog method, the easiest way is to set all the textboxes in form2 modifier to public.
I am now having a problem. In form2, i wished to access the members in form1. How do i achieve so? I tried the below code but it fails:
While I m in form 2 ...
MainFrm mainFrm = (MainFrm) this.ParentForm;
MessageBox.Show(mainFrm.myConn.ConnectionString);
When I run, I get a System.NullReferenceException. There's nothing wrong with the connection string. I tried it in form 1 and it works. Thanks for any help.
wolfofthenorth
March 17th, 2003, 11:43 AM
It sounds like Form1 is not the parent of Form2. You could setup a callback something like this:
Form2-------
FrmMain _myCallback;
public Form MyCallbackObject
{
get{return _myCallback;}
set{_myCallback = (FrmMain)value;}
}
Maybe something like this could work for you. :)
pareshgh
March 17th, 2003, 04:49 PM
hi,
if you have Vs.NET with samples installed then checkout MDI application sample it shows how to access parent windows child windows etc.
Paresh
petru66
March 19th, 2003, 09:43 AM
Dannytan,
I read your message, and :) I do not agree with your easy solution - that is, making some member controls public. I had to cope with such problem myself, and what I did was to use an event mechanism.
The form that has the TextBox will be "trained" to "listen" to whoever may want to write something; the classes which have something to say will "raise" events, and the listener will catch them and write their message.
Shortly:
- assume that Form1 has a TextBox on which I want to print text from everywhere; then I make a public function that does this:
public class Form1: Form{
private TextBox textBox1;
...
public void printText(string s){
textBox1.Append(s);
}
...
}
- I made a delegate to hold functions like printText:
public delegate void ReportDelegate(string s);
- there will be a class that makes an instance of Form1; this class collects the function printText of that instance in a member field:
public class FormsCreator : ...
{
private /* public */ event ReportDelegate eReport;
...
private /* public */ makeForms(){
Form1 form1 = new Form1();
eReport += new ReportDelegate(form1.printText);
}
...
private /* public */ just_another_function(ParamList params){
// there is a message to be printed
if (eReport != null)
{
eReport("Message");
}
}
}
- the delegate can be easily passed to other forms;
- if Form1 creates an instance of another class Form2, this latter will have a public member of type ReportDelegate, and after the creation, Form1 will add to that member its own printText function:
public class Form2: Form
{
public event ReportDelegate eReport;
...
private /* public */ just_another_function(ParamList params){
// there is a message to be printed
if (eReport != null)
{
eReport("Message");
}
}
}
// in Form1:
...
public void createForm2(){
Form2 form2 = new Form2();
Form2.eReport += new ReportDelegate(this.printText);
}
...
Another advantage of this technique is that you may want your message (that is, the one generated by Form2 or FormsCreator) to go to other places as well (files, Console.out) etc.; all you will have to do then is to add new delegates to the event eReport, accordingly.
Hope this helps.
Petru
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.