passing variables between windows forms
-
How can I do this? For example: let's say on form1.vb, i have the following code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click product_code = TextBox1.Text.Substring(0, 4) 'switch over to next window Dim form2 As New form2 form2.Show() Endsub Now, how can I get the value in product_code in Form2.vb ?
-
How can I do this? For example: let's say on form1.vb, i have the following code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click product_code = TextBox1.Text.Substring(0, 4) 'switch over to next window Dim form2 As New form2 form2.Show() Endsub Now, how can I get the value in product_code in Form2.vb ?
I was told by the mods that this forum is primarly .Net intended, and so I feel compelled to think that you are trying to do this with it, but just in case this hasn't changed since VB6 or you are indeed doing it in VB6, this is (or should I say was) the way to do it: Declare the variable product_code on a separate .bas 'Module' object, with the Public word, insted of the good ol' 'Dim': Public product_code As String in this way you can access product_code from whereever in your project (different Subs, on different Forms, Modules, Class Modules, whatever!) Hope that helps (and that you ARE doing it in VB6!) Cheers, Pablo.ar
-
I was told by the mods that this forum is primarly .Net intended, and so I feel compelled to think that you are trying to do this with it, but just in case this hasn't changed since VB6 or you are indeed doing it in VB6, this is (or should I say was) the way to do it: Declare the variable product_code on a separate .bas 'Module' object, with the Public word, insted of the good ol' 'Dim': Public product_code As String in this way you can access product_code from whereever in your project (different Subs, on different Forms, Modules, Class Modules, whatever!) Hope that helps (and that you ARE doing it in VB6!) Cheers, Pablo.ar
Pablo, Sorry, I should have clarified. Yes, I am attempting this in vb.net. I am just starting in programming in .net, with no experience in vb6. I did try declaring as Public, but with no luck. I'm sure there's a way of declaring variables globally so that this is possible. By the way, I'm not sure what a .bas module object is. Is this vb6 terminology. Thanks again for your help, Mike
-
How can I do this? For example: let's say on form1.vb, i have the following code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click product_code = TextBox1.Text.Substring(0, 4) 'switch over to next window Dim form2 As New form2 form2.Show() Endsub Now, how can I get the value in product_code in Form2.vb ?
hi, looks to me like you're using .net based on the "handles" keyword in your method signature as well as a few other things, so i'll suggest another way for you. (no offense intended pablo! :) ) the best way to do this would be to set up a friend property on form1 one that gives access to a module level variable in form1. you'll also need a reference to the actual form1 object in form2. some thing like this...
public class form1
inherits system.windows.forms.form'initialization code
private m_productCode as String
friend property ProductCode() as String
Get
return me.m_productCode
End Get
Set(byval value as string)
me.m_productcode = value
End Set
end propertyPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
me.ProductCode = textbox1.text.substring(0, 4)
dim frmTwo as new form2
frmTwo.FormOne = me
frmTwo.Showend sub
end class
*'--- in a separate code file ---'*
class form2
Inherits System.Windows.Forms.Formprivate m_formOne as Windows.Forms.Form
Friend Property Form1 as Windows.Forms.Form
Get
return me.m_formOne
End Get
Set(byval value as Windows.Forms.Form)
me.m_formOne = value
End Set
End PropertyPrivate Sub form2_Load(byval sender as object, byval e as system.eventargs) handles form2.Load
'--- this line gives you access to the product code from form1 ---'
dim productCode as string = me.FormOne.ProductCodeEnd Sub
end class
the freind property in form2 gives form1 access to safely set a field in form2, and vice versa for the friend property in form1. this code isn't tested and wasn't typed into my editor, so beware of syntaxtual errors. just a warning... hope this helps
-jim
-
Pablo, Sorry, I should have clarified. Yes, I am attempting this in vb.net. I am just starting in programming in .net, with no experience in vb6. I did try declaring as Public, but with no luck. I'm sure there's a way of declaring variables globally so that this is possible. By the way, I'm not sure what a .bas module object is. Is this vb6 terminology. Thanks again for your help, Mike
There's no such thing as a "global" variable in the .NET Framework. This goes for every language that targets .NET, not just VB. The best way to do it is not to share a variable in the form, but to emulate the method Pablo mentioned. For an example of how to do this, check out Working with Multiple Forms in Visual Basic .NET: Upgrading to .NET[^] on MSDN. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
hi, looks to me like you're using .net based on the "handles" keyword in your method signature as well as a few other things, so i'll suggest another way for you. (no offense intended pablo! :) ) the best way to do this would be to set up a friend property on form1 one that gives access to a module level variable in form1. you'll also need a reference to the actual form1 object in form2. some thing like this...
public class form1
inherits system.windows.forms.form'initialization code
private m_productCode as String
friend property ProductCode() as String
Get
return me.m_productCode
End Get
Set(byval value as string)
me.m_productcode = value
End Set
end propertyPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
me.ProductCode = textbox1.text.substring(0, 4)
dim frmTwo as new form2
frmTwo.FormOne = me
frmTwo.Showend sub
end class
*'--- in a separate code file ---'*
class form2
Inherits System.Windows.Forms.Formprivate m_formOne as Windows.Forms.Form
Friend Property Form1 as Windows.Forms.Form
Get
return me.m_formOne
End Get
Set(byval value as Windows.Forms.Form)
me.m_formOne = value
End Set
End PropertyPrivate Sub form2_Load(byval sender as object, byval e as system.eventargs) handles form2.Load
'--- this line gives you access to the product code from form1 ---'
dim productCode as string = me.FormOne.ProductCodeEnd Sub
end class
the freind property in form2 gives form1 access to safely set a field in form2, and vice versa for the friend property in form1. this code isn't tested and wasn't typed into my editor, so beware of syntaxtual errors. just a warning... hope this helps
-jim
yeah, I guess I should've noticed that .Substring thingy and all the args on the event... it passed me by, sorry. One more idea... do forms still have that .tag property? That was an amazingly useful property, which I worked with many many times! Pablo.ar PS: I feel old, hehe.
-
yeah, I guess I should've noticed that .Substring thingy and all the args on the event... it passed me by, sorry. One more idea... do forms still have that .tag property? That was an amazingly useful property, which I worked with many many times! Pablo.ar PS: I feel old, hehe.
sure do. :)
-jim