Question Obtain listbox items from another form.

dvdsnyd

New member
Joined
Sep 15, 2011
Messages
1
Programming Experience
1-3
Hi,
I apologize if I don't get enough supporting data or if this has been resolved already. However, I am using Visual Basic 2010 Express. I have 2 forms. A main form which does all the grunt work and then a small supporting form to get some additional user inputs.
Some program background...I am developing a program to characterize solid rocket motors based on test stand data for a hobby project. I load the time, thrust data into lists via a streamreader from a text box. However, since the data is raw I would like for the user to select the "start" and "end" of the burn. I am currently trying to do it right after I load the data. As I read the data in, I just put it in the listbox of the supporting form as well. My main issue is, I need two values from the user. A start and end time. the index would probably be easiest, as I will rewrite the data list based on the time increment and delete everything before the first listbox selection and everything after the second selection. I can't get the values from the main form. I have exhaustively searched the internet trying to find something that I can apply. If someone could help with a framework on how to do this.

I understand how to get text from this text box on a different form, but this deals in variables and maybe setting up a class? I am having trouble communicating between the two forms. Any help is much appreciated.

Thanks a lot,
David
 
Communicating between forms is very easy. People just try to complicate it by thinking of forms as somehow different to other objects. They are not. They are exactly the same. How do you usually get data into an object? You set a property or call a method and pass and argument, right? For instance, if you want to display text in a Label or TextBox, what do you do? You set the Text property, right? A TextBox or Label is just an object and you get data into it by setting a property. A form is just an object too, so you get data into it by setting a property. If this is your custom data then obviously it needs to be your custom property.

For instance, let's say that you have two forms, Form1 has a TextBox and a Button and Form2 has a Label. You want the user to enter text into the TextBox on Form1 and then click the Button to display Form2 with the same text displayed in the Label. In Form2, you need a property that will accept the data and display it in the Label.
Public Class Form2

    Public Property LabelText() As String
        Get
            Return Me.Label1.Text
        End Get
        Set(value As String)
            Me.Label1.Text = value
        End Set
    End Property

End Class
In Form1, you need to create an instance of Form2 and pass it the data.
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Using dialogue As New Form2
            dialogue.LabelText = Me.TextBox1.Text
            dialogue.ShowDialog()
        End Using
    End Sub

End Class
Now, let's change things up a bit. Let's say that Form1 now has a Label and a Button and Form2 has the TextBox. The user must click the Button and then Form2 will be displayed. The user enters text in the TextBox and closes the form. The same text then appears in the Label. This is almost identical except that Form1 has to get the property value instead of setting it.
Public Class Form2

    Public Property TextBoxText() As String
        Get
            Return Me.TextBox1.Text
        End Get
        Set(value As String)
            Me.TextBox1.Text = value
        End Set
    End Property

End Class
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Using dialogue As New Form2
            dialogue.ShowDialog()
            Me.Label1.Text = dialogue.TextBoxText
        End Using
    End Sub

End Class
That's all there is to it. It doesn't matter how much data there is or how complex the data is. You simply define as many properties as you need of whatever types you need to be able to pass the required data into and out of the Form(s). Those properties can be read/write, as mine were above, or they could be read-only or write-only if the data only needs to go one way. You might also find that methods are appropriate to be used as well as or instead of properties in some cases.
 

Latest posts

Back
Top