Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > How to use Strings, from form to form?


Reply
 
Thread Tools Display Modes
  #1  
Old 11-11-2005, 10:35 AM
dimirpaw's Avatar
dimirpaw dimirpaw is offline
Regular
 
Join Date: Nov 2005
Location: Canada, Alberta, Edmonton
Posts: 60
Default 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.
Reply With Quote
  #2  
Old 11-11-2005, 10:39 AM
Volte's Avatar
Volte Volte is offline
Ultimate Contributor

Retired Leader
* Guru *
 
Join Date: Aug 2001
Posts: 5,343
Default

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.
Reply With Quote
  #3  
Old 11-11-2005, 10:45 AM
dimirpaw's Avatar
dimirpaw dimirpaw is offline
Regular
 
Join Date: Nov 2005
Location: Canada, Alberta, Edmonton
Posts: 60
Default

Oh, alright! I'll be sure to try that now and let you know how it works!
Reply With Quote
  #4  
Old 11-11-2005, 11:41 AM
dimirpaw's Avatar
dimirpaw dimirpaw is offline
Regular
 
Join Date: Nov 2005
Location: Canada, Alberta, Edmonton
Posts: 60
Default

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.
Reply With Quote
  #5  
Old 11-11-2005, 11:43 AM
Volte's Avatar
Volte Volte is offline
Ultimate Contributor

Retired Leader
* Guru *
 
Join Date: Aug 2001
Posts: 5,343
Default

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.
Reply With Quote
  #6  
Old 11-11-2005, 11:58 AM
dimirpaw's Avatar
dimirpaw dimirpaw is offline
Regular
 
Join Date: Nov 2005
Location: Canada, Alberta, Edmonton
Posts: 60
Default

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!
Reply With Quote
  #7  
Old 11-11-2005, 12:30 PM
dimirpaw's Avatar
dimirpaw dimirpaw is offline
Regular
 
Join Date: Nov 2005
Location: Canada, Alberta, Edmonton
Posts: 60
Default

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?
Attached Files
File Type: zip example.zip (1.3 KB, 2 views)
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->