multiple forms and global variables

  • Thread starter Thread starter jez
  • Start date Start date
J

jez

Hi guys,

I read a couple of old posts about multiple forms and am still puzzled about
2 issues :
1) does opening one extra form really slow down the entire app ?
2) Ginny strongly adviced against the use of global variables

More details :
1) So far I have one form with 4 tabs. In one of these tabs I'd like to open
an extra floating form with four controls on it. It would be used to enter a
number in a textBox then click on a button that would close the form. That
number would then be used with some more information from the first form to
add a row to a DataTable. Would I complicate my life if I were to create
such a form and make sure that I get the text from the textBox on that
second form ?

2) If I do decide to do so could someone provide me with a little snippet of
code to get data from another Form ? I tried to create a public method that
returns data from the first form but it doesn't seem to work..

Something like :
public string getCustomerId () {
return customerId;
}

When I'm in my second form I type Form1. but the intelliSense doesn't find
that method - what's wrong ?

thanks all for the help !
 
You'll see a perf hit when the Form loads, like with loading any resource.

The reason you don't see the method in Intellisense is it must be called
from an instance of the form, not the form's class name (unless you make it
a static method).
 
To get the value back from your second form, create a public property as
below:

public string getCustomerID()
{
get{return customerID;}
}

, and then in your first form, work it like below:


Form2 f = new Form2.Create()
f.ShowDialog();

string form2Value = f.getCustomerID;

f.Dispose();
f = null;


Chris.
 
Chris,

thanks for your quick reply! I assume the loading time will be long enough
to become "annoying" for the user.. I thought about another option - perhaps
better ?

What if I simply create a panel on top of the tab the user is currently on
and when the user presses the add button that panel comes up ?

So the default property for panel.visible is false, and when the user clicks
add the panel's visible property turns to true and comes on top of the rest.
Is that a feasible option ?
 
Chris,

thanks for your reply. I tried the Get Method although I keep getting an
error. I can do the following :

public string getCustomerID() {
return customerID;
}

would that not be the same ?
 
yep.
I'm strongly considering using a visibile/invisible panel. Just tested it
for five minutes, it looks pretty interesting/easy to implement.. what do
you think ?
 
The time may not be an annoyance - it really depends on what has to happen
on Form creation. The best way to know is to test.
 
I'm trying out a panel.visible/invisible type of solution. Basically there
are only 2 buttons, a lable and a textBox on that panel. I think it's
probably easier that way; performance-wise / code-wise..

do you have any experience that would suggest against such an implementation
?
 
The Panel idea should work fine, though if you have many it gets really
confusing. It sounds simple enough that I doubt a separate for would have
any perf hit.
 
I will stick with the panel for the moment and will let you know how it
goes..
perhaps a little questions attached to that.. Is it possible to disable my
other controls while that panel is visible ?

for example, i'd like to all controls but the ones that are on the panel
until the OK button is clicked. Is there a way to do that ?
 
Set their .Visible property to False.

jez said:
I will stick with the panel for the moment and will let you know how it
goes..
perhaps a little questions attached to that.. Is it possible to disable my
other controls while that panel is visible ?

for example, i'd like to all controls but the ones that are on the panel
until the OK button is clicked. Is there a way to do that ?
 
Back
Top