Results 1 to 13 of 13

Thread: How to get text into list box?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    6

    How to get text into list box?

    Hi i have some text stored in varible which i want to put in a listbox but im having problems getting it to work.

    here is my code so far
    Code:
    Public Class Form5
    
        Dim fname1 As String
        Dim sname1 As String
        Dim money1 As Decimal
        Dim fname2 As String
        Dim sname2 As String
        Dim money2 As Decimal
        Dim fname3 As String
        Dim sname3 As String
        Dim money3 As Decimal
        Dim fname4 As String
        Dim sname4 As String
        Dim money4 As Decimal
        Dim fname5 As String
        Dim sname5 As String
        Dim money5 As Decimal
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim RT As New System.IO.StreamReader("c:\Test.txt")
    
            fname1 = RT.ReadLine()
            sname1 = RT.ReadLine()
            money1 = RT.ReadLine()
            fname2 = RT.ReadLine()
            sname2 = RT.ReadLine()
            money2 = RT.ReadLine()
            fname3 = RT.ReadLine()
            sname3 = RT.ReadLine()
            money3 = RT.ReadLine()
            fname4 = RT.ReadLine()
            sname4 = RT.ReadLine()
            money4 = RT.ReadLine()
            fname5 = RT.ReadLine()
            sname5 = RT.ReadLine()
            money5 = RT.ReadLine()
    
            ListBox1.Text = fname1 + sname1
    
        End Sub
    End Class
    i am just trying to get fname1 and sname1 to show up first then i will put fname and sname2 on the next line in the listbox hopefully.

    thanks

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: How to get text into list box?

    ListBox1.items.add(fname1 & sname1)

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: How to get text into list box?

    You'd add them to the listbox like this:
    VB.Net Code:
    1. ListBox1.Items.Add(fname1 & fname2)

    However may I suggest using a structure like this:

    VB.Net Code:
    1. Private Structure YourStructure
    2.     Dim fname As String
    3.     Dim sname As String
    4.     Dim money As Decimal
    5. End Structure
    6.  
    7. Private instances(4) As YourStructure
    8.  
    9. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10.         Dim RT As New System.IO.StreamReader("c:\Test.txt")
    11.         For i As Integer = 0 to 4
    12.             instances(i).fname = RT.ReadLine()
    13.             instances(i).sname = RT.ReadLine()
    14.             instances(i).money = Convert.ToDecimal(RT.ReadLine())
    15.         Next i
    16.         'Add to listbox here
    17. End Sub

    But of course, change 'YourStructure' and 'instances' to have better names.
    Also, turn Option Strict ON.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    6

    Re: How to get text into list box?

    ok thanks,

    One other problem i have just come across is that im using VB 2008 express and at uni we have VB 2005 express i have just been informed and having just downloaded express 2005 my 2008 project file won't open in 2005 so can i save my project as an older version some how? or is there way to convert it? if not looks like i will have to re-do it in 2005.

    thanks

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: How to get text into list box?

    I dont know if you'll be able to open it in 2005, but try going into the project properties and setting the target framework to 2.0, then save and try to open it in 2005. Though I think you'll have to re-do it in VB2005.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    6

    Re: How to get text into list box?

    Quote Originally Posted by Atheist
    I dont know if you'll be able to open it in 2005, but try going into the project properties and setting the target framework to 2.0, then save and try to open it in 2005. Though I think you'll have to re-do it in VB2005.
    would i be able to just import all my forms from my 2008 project into a new project in 2005?

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: How to get text into list box?

    You could try it.
    If you cant, it wont be too much work just copying over the code and re-constructing the forms from scratch.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    6

    Re: How to get text into list box?

    Quote Originally Posted by .paul.
    ListBox1.items.add(fname1 & sname1)
    Ok thanks for all the help i've got most of it working now. I did have to re do it in 2005 but i could copy the code and copy the objects from 2008 into 2005. Now i another simple question about the listbox how do i put a space between fname and sname when it appears in the listbox?

  9. #9
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Cool Re: How to get text into list box?

    Quote Originally Posted by kuliand
    Ok thanks for all the help i've got most of it working now. I did have to re do it in 2005 but i could copy the code and copy the objects from 2008 into 2005. Now i another simple question about the listbox how do i put a space between fname and sname when it appears in the listbox?
    VB.Net Code:
    1. ListBox1.Items.Add(fname1 & " " & sname1)
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: How to get text into list box?

    ListBox1.Items.Add(fname1 & " " & fname2)

    Looks kind of silly, doesn't it? Yet it's the easiest way to do it. Alternatively, you could append in the space character, which is a good idea if you are using more than one space, since you will have a hard time telling the difference between " " and " ".
    My usual boring signature: Nothing

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: How to get text into list box?

    Agh! Too slow.
    My usual boring signature: Nothing

  12. #12

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    6

    Re: How to get text into list box?

    thanks both of you i was nearly this time but i was putting the quotes after the & symbol without the second & symbol.

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: How to get text into list box?

    That's one of my pet peeves about the language. If you write:

    var=varA+varB

    in both cases, VS will add correct spaces to result in

    var = varA + varB

    But not if you are using the & symbol, then you just get errors.
    My usual boring signature: Nothing

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