Passing Values

tehon3299

Centurion
Joined
Jan 6, 2003
Messages
155
Location
Liverpool, NY
How would I pass a string from a form to another form? I want to pass the value of a selected item in a listview to another form. How would this be done?
 
In the constructor for the second form put something like this:

Visual Basic:
public sub New(ByVal thetext as string)

And then when creating the form pass the string into that variable. If you want an example just say, i got one.
 
Sure.

This will be the code in the form thats calling the second form and passing the value to it:

Visual Basic:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'of course you dont have to do it in formload
Dim thetext As String = "hi"
Dim form2 As New Form2(thetext)
form2.Show()
End Sub

And this will be the second form with the modified constructor:

Visual Basic:
Dim texts As String 'Dim some global variable so you can get the passed value
Public Sub New(ByVal thetext As String)
       texts = thetext 'get the value or you can use it for whatever you want
End Sub

Hope this helps!
 
Can you see where the error is in this?
Visual Basic:
    Dim team As String

    Public Sub New(ByVal teamName As String)
        team = teamName
    End Sub

    Private Sub frmRosterView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim listViewItems As New ListViewItem()

        lvwPlayers.View = View.Details
        lvwPlayers.GridLines = True

        lvwPlayers.Columns.Add("First", 100, HorizontalAlignment.Left)
        lvwPlayers.Columns.Add("Last", 100, HorizontalAlignment.Left)
        lvwPlayers.Columns.Add("Address", 100, HorizontalAlignment.Left)
        lvwPlayers.Columns.Add("City", 100, HorizontalAlignment.Left)
        lvwPlayers.Columns.Add("State", 100, HorizontalAlignment.Left)
        lvwPlayers.Columns.Add("ZIP", 50, HorizontalAlignment.Left)

        Dim mySelectQuery As String = "SELECT * FROM tmPlayerInfo WHERE Team = teamName"
        Dim myConnection As New SqlConnection("server=localhost;database=StatKeeper;Trusted_Connection=yes")
        Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
        myConnection.Open()
        Dim myReader As SqlDataReader
        myReader = myCommand.ExecuteReader()

        While myReader.Read()
            listViewItems.Text = myReader.GetValue(2)
            listViewItems.SubItems.Add(myReader.GetValue(3))
            listViewItems.SubItems.Add(myReader.GetValue(4))
            listViewItems.SubItems.Add(myReader.GetValue(5))
            listViewItems.SubItems.Add(myReader.GetValue(6))
            listViewItems.SubItems.Add(myReader.GetValue(7))
            lvwPlayers.Items.Add(listViewItems)
            listViewItems = New ListViewItem()
        End While

        myReader.Close()
        myConnection.Close()
    End Sub
It says that lvwPlayers.View = View.Details is a null exception. And I have a listview on my form named lvwPlayers.
 
What do you mean? My listview is on this form and it's named lvwPlayers. Also, if I comment out that line it just raises an error on the next line, and so on and so on...
 
What I meant is: is that listview and code on the form named lvwPlayers or are you trying to access that list from other form than the one on which it is.
 
It is on the same form. This code is referencing everything that is on this form. For some reason, when I have a New() procedure then the form load procedure does not seem to be firing. Shouldn't this still be getting called?
 
It is fired, you must have some error with your FormLoad, im sorry but I dont know what it is so I cant help you on that one.
 
It doesnt matter, by creating your own you're just overloading it so now you have to two ways of calling that form, the one you created with passing the text, or the one that was created for you without passing anything.
 
Back
Top