 |
 |

11-11-2005, 10:35 AM
|
 |
Regular
|
|
Join Date: Nov 2005
Location: Canada, Alberta, Edmonton
Posts: 60
|
|
How to use Strings, from form to form?
|
I was working on a chat client and wanted to make it so when you click connect another form loads to enter a username, ect... And once you enter the username is will then save it as the String usernames and pass it on to form1.
A better example of what I mean is, (This is not my real code but a mear example.)
Code:
Code:
Option Explicit
Dim usernames As String
Dim usernamein As Boolean
Private Sub Command1_Click()
If usersnamein = True then
txtmain.text = txtmain.text & vbcrlf & "Welcome " & usernames & Vbnewline
else
Form2.Visible = True
end if
End Sub
//// Form2 ////
(Login form)
Option Explicit
Private Sub Command1_Click()
txtusername.text = usernames
usernamein = True
End Sub
Now the code above may be wrong, but take it as an example, since thats not the code I really have.
What I need to do is make the String usernames work with Form2 and any events that effect it such as the txtusername.text = usernames which will bring that name typed into the text box and store it in the string usernames then allow it to work with form1. I'm not sure if I was too clear, but in short, I want usernames to work as the same String in both Forms. 
|
|

11-11-2005, 10:39 AM
|
 |
Ultimate Contributor
Retired Leader * Guru *
|
|
Join Date: Aug 2001
Posts: 5,343
|
|
You can either set up a public property or variable in one of the Forms to allow you to pass the string to and from it, or put the public declaration of the variable in a module, which will allow it to be accessed from all the forms in the project.
To do the former, just change the declaration to
Code:
Public usernames As String
and then you can access it with 'txtUserName.Text = Form1.usernames' for example.
|
|

11-11-2005, 10:45 AM
|
 |
Regular
|
|
Join Date: Nov 2005
Location: Canada, Alberta, Edmonton
Posts: 60
|
|
Oh, alright! I'll be sure to try that now and let you know how it works! 
|
|

11-11-2005, 11:41 AM
|
 |
Regular
|
|
Join Date: Nov 2005
Location: Canada, Alberta, Edmonton
Posts: 60
|
|
I understand how all this kinda works, but It just won't work for me.
My frmMain
Code:
Code:
Option Explicit
Private Sub mnuConnect_Click()
frmConnect.Visible = True
End Sub
Private Sub txtMainChat_Change()
If usernamein = True Then
txtMainChat.Text = txtMainChat.Text & vbCrLf & "Welcome " & frmConnect.username
End If
End Sub
My frmConnect
Code:
Code:
Option Explicit
Private Sub cmdLogin_Click()
If txtName.Text = "" Then
MsgBox "Please Enter Your Username", , "Login Failed"
Else
usernamein = True
Unload Me
End If
End Sub
Private Sub Form_Load()
End Sub
Private Sub txtName_Change()
username = txtName.Text
End Sub
My Module
mdl
Code:
Code:
Public username As String
Public usernamein As Boolean
This is my first time learning about how to do this, so I guess it's expected to for me not to get it on my first try. 
|
|

11-11-2005, 11:43 AM
|
 |
Ultimate Contributor
Retired Leader * Guru *
|
|
Join Date: Aug 2001
Posts: 5,343
|
|
|
You're mixing the two methods I gave you. If you are using a module, you don't need "frmConnect.username". Just use "username" as you were doing before.
|
|

11-11-2005, 11:58 AM
|
 |
Regular
|
|
Join Date: Nov 2005
Location: Canada, Alberta, Edmonton
Posts: 60
|
|
Oh ok, thanks!  When I get time I gota find out why it isn't displaying the Welcome message. I guess it's gona take me a bit more time to fully understand how all this works.  Thanks for the help though! 
|
|

11-11-2005, 12:30 PM
|
 |
Regular
|
|
Join Date: Nov 2005
Location: Canada, Alberta, Edmonton
Posts: 60
|
|
|
Well what I did here was using the same way I alway's used my String for names in a single form, but it won't work again, even aftering looking at how I did my other projects. I zipped up a small example of what I was doing, could anyone give some input on whats wrong here?
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|