|
-
January 27th, 2003, 05:15 AM
#1
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!
-
January 27th, 2003, 11:10 AM
#2
pass the search items text in
newform's constructor instead.
for ex.
NewTitleWindow myForm = new NewTitleWindow(search.text);
Paresh
-
January 27th, 2003, 11:13 AM
#3
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
-
January 27th, 2003, 03:09 PM
#4
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?
-
January 27th, 2003, 03:29 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|