Question about use of Hide method

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is this a proper technique or just a newbie hack? I was looking for alternatives to globals

I have a project with a main form
I add a form X.
I add a property Y and a button to X.
I call X.ShowDialog() from the main form
In form X, I press the button which sets the value of the property Y and then calls Me.Hide()
I then retrieve the value of the property Y from X and then call X.Dispose in the main form

(I came up with this myself. I was not aware that Hide could return control to the calling form and I haven't found anything explicitly relating to this in the documentation yet.
 
Hiding the form is essentially what is done when the window is "closed"
after being shown using ShowDialog. Have a look at the "remarks" section of
the Form.ShowDialog method.
http://msdn.microsoft.com/library/d...stemwindowsformsformclassshowdialogtopic1.asp

Another way that you do this is to pass the result back to the main form by
creating an overload to the ShowDialog method in X that returns the
appropriate value. Alternatively, depending on exactly what you're trying to
accomplish, you could create a custom event in X and have the main form hook
into this event before calling ShowDialog. You could then send the
information to the main form through this event.

--
Tim Wilson
..Net Compact Framework MVP
{cf147fdf-893d-4a88-b258-22f68a3dbc6a}
B. Chernick said:
Is this a proper technique or just a newbie hack? I was looking for alternatives to globals.

I have a project with a main form.
I add a form X.
I add a property Y and a button to X.
I call X.ShowDialog() from the main form.
In form X, I press the button which sets the value of the property Y and then calls Me.Hide().
I then retrieve the value of the property Y from X and then call X.Dispose in the main form.

(I came up with this myself. I was not aware that Hide could return
control to the calling form and I haven't found anything explicitly relating
to this in the documentation yet.)
 
Correction. Actually you wouldn't want to overload the ShowDialog method
unless you are passing in a value to X as well, and I'm not sure if you need
to do this. But you can always create a method in X that "internally" calls
ShowDialog and returns the appropriate value to the main form if the user
chooses to do so.

<pseudocode>

public string GetValue()
{
if (ShowDialog() = OK)
{
return someValue
}
return null
}

</pseudocode>
 
Back
Top