CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2001
    Posts
    104

    Accessing parent members?

    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 ...
    Code:
    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.

  2. #2
    Join Date
    Nov 2002
    Location
    Tatooine
    Posts
    155
    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.
    That which does not kill us, only makes us stronger.

    MCSD .NET

  3. #3
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    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
    - Software Architect

  4. #4
    Join Date
    Jul 2002
    Location
    EU
    Posts
    68

    using textboxes in another form

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured