none
VB Transfer Value RRS feed

  • Question

  • Hello . i currently doing my project which is write using vb. My question is how to transfer only the needed value from the listbox in form 1 to the textbox in form 2. For example , the value in listbox in form 1 is " my ip is 192.168.1.1 " and i only want to transfer only "192.168.1.1" to my textbox in form 2. Please help.

    Thursday, September 21, 2017 3:03 PM

Answers

  • The problem can be solved using StartsWith and Substring functions of String. And also with Regular Expressions:

    Dim value_from_listbox As String = " my ip is 192.168.1.1 "

    Dim value_for_textbox As String = Regex.Replace(value_from_listbox, "^\s*my ip is\s+", "")

     

    Adjust this example for your needs.

    • Marked as answer by tommywick Saturday, September 23, 2017 8:49 AM
    Thursday, September 21, 2017 5:55 PM
  • Okay, I have a demonstration project which shows how to ensure the child form is running and permits pushing information to the non modal child form. 

    The following is not in that project but you can add it and play with it.

    I added a ListBox and a button (button code below)

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
        Dim ipAddress As String = System.Text.RegularExpressions.Regex.Replace(ListBox1.Text, "[^.0-9]", "")
        If Not My.Application.IsFormOpen("ChildForm") Then
            mChildForm = New ChildForm
            mChildForm.Owner = Me
            mFirstTime = True
        End If
        mChildForm.Show()
        If mFirstTime Then
            mChildForm.Location = New Point(Me.Left + Me.Width, Top)
            mFirstTime = False
        End If
        mChildForm.TextBox1.Text = ipAddress
    End Sub
    That copies the ip address to TextBox1 on the child form.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by tommywick Saturday, September 23, 2017 8:50 AM
    Thursday, September 21, 2017 11:27 PM
    Moderator

All replies

  • If you created an instance (not using the default instance) of the child form at form level of form1 then you should be able to do something like (where "f" is the variable name for the current instance of form2)

    f.SomeTextBox.Text = Me.ListBox1.Text (where Text is the currently selected item in the ListBox)

    To clarify creating an instance of a form

    Dim f As New Form2

    Then using it

    F.Show

    Rather than

    Form2.Show


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, September 21, 2017 3:35 PM
    Moderator
  • Hello . i currently doing my project which is write using vb. My question is how to transfer only the needed value from the listbox in form 1 to the textbox in form 2. For example , the value in listbox in form 1 is " my ip is 192.168.1.1 " and i only want to transfer only "192.168.1.1" to my textbox in form 2. Please help.

    I suggest that you pass it into the new instance of your other form using a constructor.

    For example, I put some strings into a ListBox and when I select one and click the button, I then pass that string to the other form:

    Form1:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Private Sub _
            Form1_Load(sender As System.Object, _
                       e As System.EventArgs) _
                       Handles MyBase.Load
    
            ListBox1.Items.AddRange(New String() _
                           {"This", "That", "The Other"})
    
            Button1.Enabled = False
    
        End Sub
    
        Private Sub _
            ListBox1_SelectedIndexChanged(sender As System.Object, _
                                          e As System.EventArgs) _
                                          Handles ListBox1.SelectedIndexChanged
    
            If ListBox1.SelectedIndex > -1 Then
                Button1.Enabled = True
            Else
                Button1.Enabled = False
            End If
    
        End Sub
    
        Private Sub _
            Button1_Click(sender As System.Object, _
                          e As System.EventArgs) _
                          Handles Button1.Click
    
            If ListBox1.SelectedIndex > -1 Then
                Dim selectedItem As String = _
                    ListBox1.Items(ListBox1.SelectedIndex).ToString
    
                Using f2 As New Form2(selectedItem)
                    f2.ShowDialog()
                End Using
            End If
    
        End Sub
    End Class

    Form2:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form2
        Public Sub New(ByVal textToShow As String)
    
            InitializeComponent()
    
            If String.IsNullOrWhiteSpace(textToShow) Then
                Close()
            Else
                TextBox1.Text = textToShow
            End If
    
        End Sub
    
        Private Sub _
            Form2_Load(sender As System.Object, _
                       e As System.EventArgs) _
                       Handles MyBase.Load
    
            ' Initialization here (if applicable)
    
        End Sub
    End Class


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Thursday, September 21, 2017 4:29 PM ...typo in the code
    Thursday, September 21, 2017 4:27 PM
  • Thanks karen for your reply, i had done what you told me but it showed " my ip is 192.168.1.1 " in textbox. How do i get rid that " my ip is " in the front of the ip address in textbox without change the value in the listbox ? 

    Thursday, September 21, 2017 5:02 PM
  • The problem can be solved using StartsWith and Substring functions of String. And also with Regular Expressions:

    Dim value_from_listbox As String = " my ip is 192.168.1.1 "

    Dim value_for_textbox As String = Regex.Replace(value_from_listbox, "^\s*my ip is\s+", "")

     

    Adjust this example for your needs.

    • Marked as answer by tommywick Saturday, September 23, 2017 8:49 AM
    Thursday, September 21, 2017 5:55 PM
  • Thanks for your reply Frank and well explained. That a nice idea . But it is possible to transfer the value from listbox to textbox without changing the value from the listbox. 

    Here my code for explaining more on my questions . 

    Form 1 : 

    Public Class Form1
        Dim streamer As IO.StreamReader


        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            OpenFileDialog1.ShowDialog()
            TextBox1.Text = OpenFileDialog1.FileName

        End Sub

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            streamer = IO.File.OpenText(TextBox1.Text)
            Dim mystring() As String = streamer.ReadToEnd.Split(vbNewLine)
            ListBox1.Items.AddRange(mystring)


        End Sub

        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

            Dim f As New Form2
            f.TextBox1.Text = Me.ListBox1.Text
            f.Show()
            Me.Hide()

        End Sub


    End Class

    Form 2 : 


    Public Class Form2


        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Process.Start("CMD.exe", " /k ping")

        End Sub


    End Class

    Thursday, September 21, 2017 6:24 PM
  • But it is possible to transfer the value from listbox to textbox without changing the value from the listbox.

    I'm confused on what you want to send to the other form? Explain that please? Do you want send everything in the ListBox over to it?



    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Thursday, September 21, 2017 6:34 PM ...reworded
    Thursday, September 21, 2017 6:33 PM
  • Tommy,

    As an addendum here, you don't need to use Process.Start (at least I don't think you will).

    You might make use of this:

    Imports System.Net
    Imports System.Net.NetworkInformation

    ...add a function:

        Private Function GetPing(ByVal ipString As String) As PingReply
    
            Dim retVal As PingReply = Nothing
    
            If Not String.IsNullOrWhiteSpace(ipString) Then
                Dim ip As IPAddress = Nothing
    
                If IPAddress.TryParse(ipString, ip) Then
                    retVal = New Ping().Send(ip)
                End If
            End If
    
            Return retVal
    
        End Function

    The return value will be an instance of the PingReply class:

    https://msdn.microsoft.com/en-us/library/fk38xy8f(v=vs.110).aspx

    Be sure to test the return value though because it defaults to null (Nothing).


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Thursday, September 21, 2017 6:45 PM ...typo
    Thursday, September 21, 2017 6:45 PM
  • Im so sorry Frank , i want to say that im newbie to this " programming world " and i have to learn a lot. So correct me if im wrong. For answering your question , i want to send a specific information or value from listbox to the textbox. Not everything in it. For example, the value in the listbox is " my ip address 192.1.1.1 " . so i want to send only " 192.1.1.1" to the textbox for ping test using cmd which is in another form.
    Thursday, September 21, 2017 7:14 PM
  • Im so sorry Frank , i want to say that im newbie to this " programming world " and i have to learn a lot. So correct me if im wrong. For answering your question , i want to send a specific information or value from listbox to the textbox. Not everything in it. For example, the value in the listbox is " my ip address 192.1.1.1 " . so i want to send only " 192.1.1.1" to the textbox for ping test using cmd which is in another form.

    Oh I see.

    Look at what Viorel posted - the RegEx. Use that as what to put in the constructor of my original example.

    You can also use String.Replace, but RegEx is probably a better choice.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, September 21, 2017 7:20 PM
  • Okay, I have a demonstration project which shows how to ensure the child form is running and permits pushing information to the non modal child form. 

    The following is not in that project but you can add it and play with it.

    I added a ListBox and a button (button code below)

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
        Dim ipAddress As String = System.Text.RegularExpressions.Regex.Replace(ListBox1.Text, "[^.0-9]", "")
        If Not My.Application.IsFormOpen("ChildForm") Then
            mChildForm = New ChildForm
            mChildForm.Owner = Me
            mFirstTime = True
        End If
        mChildForm.Show()
        If mFirstTime Then
            mChildForm.Location = New Point(Me.Left + Me.Width, Top)
            mFirstTime = False
        End If
        mChildForm.TextBox1.Text = ipAddress
    End Sub
    That copies the ip address to TextBox1 on the child form.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by tommywick Saturday, September 23, 2017 8:50 AM
    Thursday, September 21, 2017 11:27 PM
    Moderator