Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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?

Thanks,

Tehon

Guest mutant
Posted

In the constructor for the second form put something like this:

 

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.

Guest mutant
Posted
Yes, that is the constructor, it will be called before anything else.
Guest mutant
Posted

Sure.

 

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

 

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:

 

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!

Posted
Can you see where the error is in this?
    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.

Thanks,

Tehon

Guest mutant
Posted
So the list is not on the form from this code?
Posted
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...

Thanks,

Tehon

Guest mutant
Posted
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.
Posted
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?

Thanks,

Tehon

Guest mutant
Posted
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.
Guest mutant
Posted
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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...