Try this, add a new module to your project. In the module right under the first line enter Public <and you var>
Like this:
Module Module1
Public UserName, UserPassword As String
End Module
there are quite a few threads about the same thing. In the previous post I had explained and gave you some code about passing variables to another form/class - thats exactly how its done :-)
Form1:
Dim theSecondForm as new Form2("Bob", "123 some street")
theSecondForm.ShowDialog()
Form2:
Dim theName as String
Dim theAddress as String
Public Sub New (byval name as String, byval address as String)
Me.theName = name
Me.theAddress = address
end sub
that's all there is to it to pass variables from 1 form to another. You increase the parameters to the amount you want to pass into the other class, and increase its constructor (new in this case)
Your question is a bit confusing...
Which form is the initial form (the one displayed first)? Is it Form3? The fact that you are using "Dim theFirstForm As New Form1" would inidicate that Form1 is not the startup form... If you use "Dim theFirstForm As New Form1", then you can access all the TextBoxes on Form1 directly using your variable: theFirstForm.FirstNameTextBox.Text
ahmedilyas gave you an answer... if you're not getting it that would indicate that you're trying to do something that you're not explaining... If the answer you got doesn't help, try to give us a clearer indiciation of how your program is supposed to operate: as in, which form loads first, when/how are the other forms displayed, where are you delcaring the instances of Form1 and Form2, etc.
We're going to deal with static data so the allocated storage will
exist during the entire run-time.
Add a new class to yopur project. Name it what ever you want. Make it look something like this:
Imports Microsoft.VisualBasic
Public Class Common
Public Shared sDateTime As String
Public Shared sReportName As String
Private Sub New()
'Makes this class uninheritable
End Sub
End Class
...........................
Now, when somebody (in this case) fills in the textboxes from one of your forms classes you'd do something like this:
Common.sDateTime = Textbox1.Text
Common.sReportName = Textbox2.Text.
Now when you need to get the information from yet another forms class
you access the fields again. If your writing them out to a database you
can put them in your sql string as is:
Dim sSql as string = "Insert Into tblWhatever(Date, ReportName) Values('" + Common.sDateTime...
You get the picture. I don't want to give to much away...
This is entirely an issue of personal preference, but I would strongly reccomend against using a global variable to pass data between forms. This includes using static class members and fieds inside of modules.
The reason I reccomend against this is simple: as your application logic grows more complex, debugging the use of global variables can become extremly difficult. In particular, because a global variable may be set anywhere in code, you may find yourself in the future pulling your hair out trying to figure where exactly a particular value is getting set.
If, however, you pass data between your forms by setting instance variables on the form, it is much easier to track the flow of information through your application.
You can do this either by adding parameters to the constructor of your form, as was suggested above, or by creating a property on your form that you use to set the necessary state from your secondary form.
ahmedilyas wrote:
there are quite a few threads about the same thing. In the previous post I had explained and gave you some code about passing variables to another form/class - thats exactly how its done :-)
Form1:
Dim theSecondForm as new Form2("Bob", "123 some street")
theSecondForm.ShowDialog()
Form2:
Dim theName as String
Dim theAddress as String
Public Sub New (byval name as String, byval address as String)
Me.theName = name
Me.theAddress = address
end sub
that's all there is to it to pass variables from 1 form to another. You increase the parameters to the amount you want to pass into the other class, and increase its constructor (new in this case)
I tried passing parameters from one form to another using the above suggested method.
The problem I am running into is, on my form I have a Label field. I want to change the text property of this label field to contain
the text that is passed to the form as a prameter.
Here is the code that I am using.
Public Class LookupsInherits System.Windows.Forms.Form Public Sub New(ByVal TableType As String)Select Case TableType Case "Vehicle Type" Vehicle_Type(TableType) End SelectEnd SubPrivate Sub Vehicle_Type(ByVal TableType As String) lblSubHeading.Text = TableType End Class |
The following error I am getting is at the red line of code above....
Additional information: Object reference not set to an instance of an object.
Any ideas as to why I receive this error ?
thanks
tattoo
tried doing it the other way? Really you shouldnt call methods from the New constructor but this should not be the problem.
Check to see in the debugger what lblSubHeading value is - if its nothing then thats the problem, looks like the control has not been instantiated
I tried calling my method from the form load routine and the program didn't know anything about the variable TableType that I had passed in..
I also looked at the value of the lblsubheading.text in the debugger and it is set as Nothing.... Why when this label was added at design time.
I can see it is defiend in the Windows Form Designer Generated Code so I don't understand why the app thinks it isn't instantiated.
If, as you say I shouldn't be calling any methods from the New Constructor, where should they be called from ?
If I call them from somewhere else, how do I get those methods to recognize the variables that I pass in....
thanks
tattoo
the should be called seperately else where.
now if lblSubHeading is nothing, then create the Vehicle_type method as public, so the calling form can access this and set the text before showing this form and remove the New constructor on the look ups form.
//calling form:
Dim theLookUps as new LookUps()
theLooksUps.Vehicle_Type("boo")
//other form:
public sub Vehicle_Type(byval TableType as string)
'rest of code here
end sub
Have you checked the value of the TableType variable when it is first set? That Select statment will only set the variable on a very specific value. Are you sure that "Vehicle Type" is the value you passed to the form? Nothing else will work, the way you've written it.
And I'm not sure what could be wrong with calling initialization subroutines from the constructor... forms do it implicitly with the call to InitializeComponent() and structured code design almost dictates that additional initialization code should be wrapped into common routines and called in the same manner.
Is there a way, when calling Form2 from Form1, that I can get Form2 to return a string back to Form1 ?
In Form1 I want to allow the user select from a list of email addresses. Form2 is the form that maintains this list.
thanks
tattoo
there is. you could either:
//form2:
public function DoGetAddress() as String
return "some email address"
end function
//form1:
Dim theForm2 as new Form2()
MessageBox.Show(theForm2.DoGetAddress())
does this help?
Oops !!!! I spoke to soon.....
I tried your suggestion above and it did work when I hard coded an email value to be returned but what
I actually want to do is display the second form to allow the user to select a valid email address from the database.
below is the code I used, the problem I have run into is that the line I used to display the form to select the email address leaves the form on the screen and doesn't allow me to select a value and return.
any ideas ?
'Calling Form Private Sub btnemail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnemail.ClickDim MailAddress As String Dim Address As New NetAddresses MailAddress = Address.GetDat() System.Diagnostics.Process.Start("mailto:" & MailAddress) End Sub 'Called Form Public Class NetAddresses Dim Message As String Public Function GetDat()SqlDataAdapter1.Fill(Addresslist, "NetAddress") Me.Show() Return CboAddress.Text End Function |