Click to See Complete Forum and Search --> : Problem accessing parentform's controls?


DFX1212
January 27th, 2003, 04:15 AM
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 :D

Thanks in advance!

pareshgh
January 27th, 2003, 10:10 AM
pass the search items text in
newform's constructor instead.
for ex.



NewTitleWindow myForm = new NewTitleWindow(search.text);


Paresh

pareshgh
January 27th, 2003, 10:13 AM
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

DFX1212
January 27th, 2003, 02:09 PM
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?

pareshgh
January 27th, 2003, 02:29 PM
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