I'm a VB2005 beginner (25yrs in COBOL, but a VB beginner) Trying to learn VB to keep myself employed, and I'm really struggling with some of the concepts.

I've tried several books, but I must be searching using incorrect terminology - one of my many struggles.

I have FormOne with a listbox and want to call FormTwo and pass it the lstSelectedIndex of an item from FormOne.

When I get to FormTwo, I need to use the SelectedIndex to access items in an array. Once I get the selected index value into FormTwo, I'll be good to go.

How do I pass it

I'm trying to call FormTwo using

Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click

selectedItem = lstItems.SelectedIndex

Dim SecondForm As New frmFormTwo


End Sub

What am I missing How do I fix it



Re: Visual Basic General passing variables between forms/classes


declare the variable to used different form in module

public a as string

now the a can used in any form

Re: Visual Basic General passing variables between forms/classes

Dick Donny


In object oriented programming, you work with types and instances. A type can have zero or more instances.

A form is simply a type. You can inherit from this type and create a new form type. In this case, you have created form types called frmFormOne and frmFormTwo and they inherit from Form. Each time you use the new keyword to assign a type to a variable, you create and assign a new instance.

There are a number of ways you can pass data between instances of types ... here's a simple example that hopefully suits your scenario.

dim frm2 as new frmformtwo


Note here that we declare a variable (frm2) and assign a new instance of the frmformtwo type to it. From hereon in we will use the variable to reference the new instance we just created, not the forms name.

We will now have to code our own method called showdialog in frmformtwo .. something along these lines

public overloads function showdialog(byval selectedIndex as integer) as windows.forms.dialogresult

'' this is where you will need to add your code to do whatever you want to do with the selectedindex value

return mybase.showdialog

end function

You have now passed the selectedindex value to frmformtwo and can work with it from within our new showdialog method.

Hope this helps get you started.


Re: Visual Basic General passing variables between forms/classes


To expand a little on what narasiman said:-

Suggest that you consider the advantages of including a Module in your win applications. (from your IDE menu select [Project] then choose [Add Module])

Modules are great because you can declare public variables which are read/writeable in any of your forms.

Example: here are some arrays of different variable types that I want to manipulate in my various forms:-

Module Module1

Public bnf(0 To 50) As Short

Public dsg(0 To 50) As Integer

Public vnc(0 To 50) As Byte

Public dpo(0 To 50) As String

say i want to record the 3rd item in a combo box that i have on form1

in form1 somewhere i would have this line of code:-

dpo(25) = Combo1.Items.Item(3)

then later say i wanted the 5th item in a list in form3 to be that previously recorded item from (combo1 - form1)

in form3 i would include:-

List1.Items.Item(5) = dpo(25)

see how the variable dpo(25) is used to transfer information between the forms

Modules are also great because you can use them to contain the shared subroutines/functions that can be called from any of your forms. There is a bit to learn about what you can code in modules and what you cannot code in modules. I'm a beginner myself.

Also I would like to share an example on how to save your variables to file (on close) and how to load them from file.

'variables / arrays declared in module1

Public xji() As Byte

Public hbs(0 To 300) As Short

Public I%

Public J%

'load up array hbs() (short type) with data from file

xji = My.Computer.FileSystem.ReadAllBytes("C:\somefolder\somefile. ")

For I% = 0 To 300

J% = xji(I% * 2) + xji(I% * 2 + 1) * 256

If J% > 32767 Then J% = J% - 65536

hbs(I%) = J%

Next I%

' to save the contents of an array (short type) to a file

ReDim xji(0 To 601)

For I% = 0 To 300

J% = hbs(I%)

If J% < 0 Then J% = J% + 65536

xji(I% * 2 + 1) = Int(J% / 256)

xji(I% * 2) = J% - xji(I% * 2 + 1) * 256

Next I%

My.Computer.FileSystem.WriteAllBytes("C:\somefolder\somefile. "", xji, False)

Re: Visual Basic General passing variables between forms/classes


Thanks for your reply. Unfortunately it did not contain enough infor to be helpful - remember, I'm a beginner. Appreciate your effor. Another responder elaborated on your statement with info that was helpful, so I guess you actually started the trail to the answer.


Re: Visual Basic General passing variables between forms/classes


Thanks for your response. I tried your suggestion on a sample project, and found it offered a possible solution. I decided to use the MODULE statement from leonCS, as it fits better with what i am trying to do.

Thanks Again!


Re: Visual Basic General passing variables between forms/classes



Thanks for your help. the MODULE idea was a big help. I modified your suggestions a little bit, and was able to get everything working. It still isn't very pretty, but it works!