Results 1 to 3 of 3

Thread: Please help - passing string from text box to another form

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    4

    Question Please help - passing string from text box to another form

    Hi There,

    I would like for someone to be able to type an IP address on one form in Visual Basic Express 2008, and when a button pressed open another form and print the value of the text box on this form.

    First form:
    Code:
    
    Public LDServerIP As String
    
    Private Sub ButtonConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonConnect.Click
         ServerIP = TextBox.IpAddress
    End Sub
    The second form where i want it to echo:

    Code:
    Private Sub ServerIPLabelSet()
            Label.ServerIPLabel.Caption = CStr(FirstForm.ServerIP)
    End Sub
    I am probably way off, but if anyone could help me, that would be greatly appreciated.

    Thank you,
    Jordan (Nzol)

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Please help - passing string from text box to another form

    Define a public sub in the second form and pass the IP as a parameter
    Code:
    Public Sub ServerIPLabelSet(ByVal strIP As String)
            Label.ServerIPLabel.Caption = strIP
    End Sub
    then call that sub in the Button click event
    Code:
    Private Sub ButtonConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonConnect.Click
         ServerIP = TextBox.IpAddress
         Form2.ServerIPLabelSet(ServerIP) ' Replace Form2 with the actual name.
    End Sub



  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Please help - passing string from text box to another form

    Either way is entirely viable, but ONLY if the scope is correct. In your initial example, that code would work as long as FirstForm is in scope for the second form. If you are using default instances, then it will be in scope, but default instances are rather evil because they tend to cause confusion in the long run. If you are not using the default instance, then it seems kind of unlikely that FirstForm will be visible to the other form (though there are certainly ways that it could be).

    In the example posted by 4x2y, the scoping is reversed. Form2 has to be visible to FirstForm, but FirstForm doesn't have to be visible to Form2. Once again, if you are using the default instances of the forms, they will all be in scope, and the same caveats apply.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width