Displaying Entered Name on other forms

Obie
08-24-2000, 11:38 PM
Hi,
I am completely new to VB. My lecturer however has thrown a game assignment at me where I have to make a Digimon game.

One of the requirements is getting the user to enter their name. I have done this via a txtbox. If user does not enter their name and press the cmdStart button, a msgbox opens asking them to do so.

If they have entered atleast 1 character and press the button then the first form dissapears appears and you move onto a second form where the actual game is played.

However I need to display the entered name on subsequent forms automatically as they open up. I thought of displaying the name in a label. I think the first event of the second form will be:

Private Sub Form_Load()........however where do i go from there?

Thanks
Obie

Mill
08-25-2000, 07:53 AM
The EASIEST way to do this is to save add a module into your project (it's in one of the menu items), then create a global string variable. In the button's click event, set the global variable equal to whatever is in the textbox. From then on, you can just access that variable whenever you need it.

SoldierBoy
08-25-2000, 07:54 AM
This should work if you already got the variable passed to the new form or it's a global variable. However, if it's for a class, i wouldn't recommend global variables. :)

label1.caption = sName

w/o using a public variable, you can always do something like

me.label1.caption = form1.txtNameBox.text

where form1 is the name of the form where you entered it and txtNameBox is the name of the textbox you used to input the name. Oh, and I don't think it'll work if you're unloading the first form, so maybe just hide it instead if you're gonna do it that way (form1.hide)

SeiferTim
08-25-2000, 11:25 AM
I agree with the Guru. I had to learn about Modules on my own, because my teachers never even mentioned them. They are fun, though.

-Seifer Tim

Visit my Web-Site: http://solenoid.50megs.com

Nevvvv
08-28-2000, 03:47 PM
U can do this without using global variables by setting a "public property let" function in the second form and then placing the text into that property before closing unloading the first form. Example:

Form2 code:

public property let Text (sText as string)
Text = sText
end property

Form1 code:

Form2.show
form2.Text = TextBox.text
unload me

SeiferTim
08-28-2000, 04:06 PM
Hmm... Or you could just do it the old fashioned way:

<code>
Variable1 = Info
Form2.Label1.Caption = Variable1
Form2.Visible = True
Me.Visible = False
</code>

Then, in Form2:
<code>
Variable2 = Label1.Caption
</code>

That's how I did it before discovering the miracle that is Modules...

-Seifer Tim

Visit my Web-Site: http://solenoid.50megs.com

BillSoo
08-28-2000, 07:59 PM
The problem with global variables is not that they are inherently evil, it's that they can be over used and abused.

A program with too many globals can be incredibly hard to debug as values get changed in obscure functions.

In this case however, a global is appropriate. The user name is only entered once and is read only after that. So there should be no problem about unexpected changes.

So as other posters have said, go with a module public variable.

"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum