tehon3299 Posted March 29, 2003 Posted March 29, 2003 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? Quote Thanks, Tehon
Guest mutant Posted March 29, 2003 Posted March 29, 2003 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. Quote
tehon3299 Posted March 29, 2003 Author Posted March 29, 2003 Will the public sub new fire before the form load event is fired? Quote Thanks, Tehon
Guest mutant Posted March 29, 2003 Posted March 29, 2003 Yes, that is the constructor, it will be called before anything else. Quote
tehon3299 Posted March 29, 2003 Author Posted March 29, 2003 Think you could post your example for me? Quote Thanks, Tehon
Guest mutant Posted March 29, 2003 Posted March 29, 2003 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! Quote
tehon3299 Posted March 29, 2003 Author Posted March 29, 2003 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. Quote Thanks, Tehon
Guest mutant Posted March 29, 2003 Posted March 29, 2003 So the list is not on the form from this code? Quote
tehon3299 Posted March 29, 2003 Author Posted March 29, 2003 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... Quote Thanks, Tehon
Guest mutant Posted March 29, 2003 Posted March 29, 2003 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. Quote
tehon3299 Posted March 29, 2003 Author Posted March 29, 2003 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? Quote Thanks, Tehon
Guest mutant Posted March 29, 2003 Posted March 29, 2003 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. Quote
tehon3299 Posted March 29, 2003 Author Posted March 29, 2003 Thanks for the help! Quote Thanks, Tehon
tehon3299 Posted March 29, 2003 Author Posted March 29, 2003 Should I be creating that Public Sub New() or should I use the .NET built in one? Quote Thanks, Tehon
Guest mutant Posted March 29, 2003 Posted March 29, 2003 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.