CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2003
    Posts
    2

    Problem accessing parentform's controls?

    I have a main form called MainWindow and a second form called NewTitleWindow. On the MainWindow form I have a textbox control called SearchTerm. When a user clicks a button on this form it opens the NewTitleWindow and I want to be able to access the value inside the SearchTerm textbox but I keep getting the error: "Object reference not set to an instance of an object." I've read multiple tutorials online and this is the code I've put together...

    On the MainWindow form I call the NewTitleWindow form with:

    NewTitleWindow myForm = new NewTitleWindow();
    myForm.ShowDialog(this);

    On the NewTitleWindow form I have:

    String search = ((MainWindow)this.ParentForm).SearchTerm.Text;

    and that is the line at which I get the error.

    If anyone could tell me how to fix this I'd love them forever

    Thanks in advance!

  2. #2
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    pass the search items text in
    newform's constructor instead.
    for ex.



    NewTitleWindow myForm = new NewTitleWindow(search.text);


    Paresh

  3. #3
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    forgot to tell you,

    you can do the same thing with your current code also by setting mainwindow as the current't windows parent

    NewTitleWindow myForm = new NewTitleWindow();
    after this line set the parent
    myForm.parent = mainwindow...

    Paresh

  4. #4
    Join Date
    Jan 2003
    Posts
    2
    Thanks pareshgh but now I'm getting the error:

    Cannot add a top level control to a control.

    When I added myForm.Parent = this; to the MainWindow.

    Argh, what am I doing wrong?

  5. #5
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    no you have reversed what i said.

    your new (child form) should have a reference to a parent form somehow and that's how u access it. either by using a TAG property or parent property.

    *the casting should be always correct *

    Paresh

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