paries
asked on
Rookie question from java programmer
so i am trying to do a basic C# windows application.
I have a form that calls another form (Modal)
The settings form needs to access some vars from the parent form
So i am not sure the best way , but i thought i would try this::
SettingsForm settingsForm = new SettingsForm();
settingsForm.setParent(thi s);
settingsForm.ShowDialog();
then in the settings form i tried::
public void setParent(Form1 form1)
{
this.parent = form1;
}
public SettingsForm()
{
InitializeComponent();
tbServerURL.Text = parent.ServerURL;
tbDatFileLocation.Text = parent.DataSourceLocation;
}
When i call the method that opens the modal form i get the error:::
System.NullReferenceExcept ion was unhandled
Message=Object reference not set to an instance of an object.
I am sure this is some very rookie misunderstanding of the language.
Any help would be appreciated
Thanks
I have a form that calls another form (Modal)
The settings form needs to access some vars from the parent form
So i am not sure the best way , but i thought i would try this::
SettingsForm settingsForm = new SettingsForm();
settingsForm.setParent(thi
settingsForm.ShowDialog();
then in the settings form i tried::
public void setParent(Form1 form1)
{
this.parent = form1;
}
public SettingsForm()
{
InitializeComponent();
tbServerURL.Text = parent.ServerURL;
tbDatFileLocation.Text = parent.DataSourceLocation;
}
When i call the method that opens the modal form i get the error:::
System.NullReferenceExcept
Message=Object reference not set to an instance of an object.
I am sure this is some very rookie misunderstanding of the language.
Any help would be appreciated
Thanks
The reason you received that error is because you're setting the parent after the new Settings form has already been instantiated. Try this instead:
However, passing the entire form object may not be the most practical solution to retrieve the values. A common method for using "global" variables is to create a static class, and use it for your variables that will be used across different forms.
http://msdn.microsoft.com/en-us/library/79b3xss3(v=VS.100).aspx
//In Form1
SettingsForm settingsForm = new SettingsForm();
settingsForm.ShowDialog();
//In SettingsForm
public SettingsForm(Form1 form1)
{
InitializeComponent();
this.parent = form1;
tbServerURL.Text = parent.ServerURL;
}
However, passing the entire form object may not be the most practical solution to retrieve the values. A common method for using "global" variables is to create a static class, and use it for your variables that will be used across different forms.
http://msdn.microsoft.com/en-us/library/79b3xss3(v=VS.100).aspx
Ah, I forgot the form in the SettingsForm(this) call, but tgerbert's comment has it in there.
ASKER
thats for the response
that is what i did when i originally called settingsForm.Show(this);
but when i changed to settingsForm.ShowDialog(th is); (cause i want it to be modal)
it still only calls public SettingsForm()
I set my breakpoint in both
that is what i did when i originally called settingsForm.Show(this);
but when i changed to settingsForm.ShowDialog(th
it still only calls public SettingsForm()
I set my breakpoint in both
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks
@tgerbert
Sorry, people keep coming by my office with questions!
Sorry, people keep coming by my office with questions!
Open in new window
Is executed when it's instantiated with the SettingsForm settingsForm = new SettingsForm() line, and is making references to parent before your call to setParent.
Try passing the reference to the parent in the SettingsForm's constructor.
Open in new window
Open in new window