locked
I need to turn list box items into strings RRS feed

  • Question

  • What im trying to do is as i add an item to the list box, that item also becomes it own seperate string that i can call in another form. I know how to set up a form to form variable, but what i dont know is how to save each item as a string. And there is no limit to how many inputs are allowed, so an infinite number of strings could be created.

    Code:

    dim strEighteenOlder as string

    strEighteenOlder = InputBox("Enter names of guests under 3 years old:" & ControlChars.NewLine & "Press cancel to close the input boxes.", "Enter Guests", )

    Do Until strEighteenOlder = String.Empty
                listOver18.Items.Add(strEighteenOlder & " " & EighteenOlderPrice & ControlChars.NewLine)
                EighteenOlder = InputBox("Enter names of guests under 3 years old:" & ControlChars.NewLine & "Press cancel to close the input boxes.", "Enter Guests", ) 

    Loop

    THANK YOU FOR YOUR HELP!

    Thursday, December 6, 2012 5:29 AM

Answers

  • Hi ThatVBkid,

    I will suggest you to save all items in listbox to a string array. You can add a public string array to the Form you want to use the items. And save all items to it in the main form.

    Here is a sample (Listbox in Form1 and Form2 is another form),you need to edit it to suit for you situation:

        'This line add to Form2
        Public listitems As String()
      'Button1 in Form1 used to create a new Form2 and save the listbox items to string Array in Form2
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim fn2 As New Form2
            fn2.Visible = True
            Dim count As Integer = ListBox1.Items.Count - 1
            ReDim fn2.listitems(count)
            For i As Integer = 0 To count
                fn2.listitems(i) = ListBox1.Items(i).ToString
            Next
        End Sub

    Hope this helps.


    Mark Liu-lxf
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, December 7, 2012 5:48 AM
    Moderator
  • This is another way of doing it 

      Dim lx As New ListBox 'your listbox
      Dim s() As String = lx.Items.Cast(Of String).ToArray
    kaymaf



    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Friday, December 7, 2012 2:59 PM

All replies

  • Hi ThatVBkid,

    I will suggest you to save all items in listbox to a string array. You can add a public string array to the Form you want to use the items. And save all items to it in the main form.

    Here is a sample (Listbox in Form1 and Form2 is another form),you need to edit it to suit for you situation:

        'This line add to Form2
        Public listitems As String()
      'Button1 in Form1 used to create a new Form2 and save the listbox items to string Array in Form2
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim fn2 As New Form2
            fn2.Visible = True
            Dim count As Integer = ListBox1.Items.Count - 1
            ReDim fn2.listitems(count)
            For i As Integer = 0 To count
                fn2.listitems(i) = ListBox1.Items(i).ToString
            Next
        End Sub

    Hope this helps.


    Mark Liu-lxf
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, December 7, 2012 5:48 AM
    Moderator
  • This is another way of doing it 

      Dim lx As New ListBox 'your listbox
      Dim s() As String = lx.Items.Cast(Of String).ToArray
    kaymaf



    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Friday, December 7, 2012 2:59 PM
  • Hi ThatVBkid,

    We haven’t heard from you for several days. I’d like to mark the helpful replies as answers firstly. If you have any additional questions, you also can unmark the reply and post your question here. 

    Sorry for any inconvenience and have a nice day.


    Mark Liu-lxf
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 18, 2012 8:58 AM
    Moderator