+ Reply to Thread
Results 1 to 11 of 11

Thread: VB.Net Programming Help     submit to reddit submit to twitter

  1. #1
    Salvage Bans
    Join Date
    Apr 2006
    Posts
    763
    BG Level
    5
    FFXI Server
    Lakshmi

    VB.Net Programming Help

    I'm taking an online .Net course with a surprisingly absent teacher. Well the chapter 7 lab is referencing chapter 11 stuff so I'm pretty much lost and can't seem to find my exact issue online so I was hoping someone here would be able to lend a hand.

    I have a ListBox with numbers 1 to 8. On the Leave event for it I am reading a simple text file with:

    1 John Smith
    2 Bob Loblaw
    3 Blah blah
    etc etc...

    So when I click on "3" in the list box and leave it, I want it to display "Blah Blah" in a separate text box or label.

    I was able to read through the text file, but couldn't find a way to store each field into an array or however would be a good way to do it... anyways, any help is appreciate, thanks.

  2. #2
    Chram
    Join Date
    Nov 2007
    Posts
    2,668
    BG Level
    7
    FFXIV Character
    Avelle Asteria
    FFXIV Server
    Hyperion
    FFXI Server
    Phoenix
    WoW Realm
    Alleria

    Quote Originally Posted by Datonare View Post
    I'm taking an online .Net course with a surprisingly absent teacher. Well the chapter 7 lab is referencing chapter 11 stuff so I'm pretty much lost and can't seem to find my exact issue online so I was hoping someone here would be able to lend a hand.

    I have a ListBox with numbers 1 to 8. On the Leave event for it I am reading a simple text file with:

    1 John Smith
    2 Bob Loblaw
    3 Blah blah
    etc etc...

    So when I click on "3" in the list box and leave it, I want it to display "Blah Blah" in a separate text box or label.

    I was able to read through the text file, but couldn't find a way to store each field into an array or however would be a good way to do it... anyways, any help is appreciate, thanks.
    I'm not sure what you need help with. Are you having trouble storing the text in the file into variables?

  3. #3
    Fake Numbers
    Join Date
    Jun 2005
    Posts
    86
    BG Level
    2
    FFXI Server
    Bahamut
    WoW Realm
    Mal'Ganis

    I'm a bit rusty in VB since I usually work in C# but this should get you on the right track.

    To set the text based on the listbox selected item:

    TextBox1.Text = ListBox1.SelectedItem.ToString()

    You might want to handle the SelectedIndexChanged event instead if you want to make the textbox change as soon as an item is clicked, instead of when you leave the listbox's focus.

    To load an array you can use a string array or a List<string>. Arrays in VB have to be ReDim'd based on the number of elements you want to free up. If you have a pre set number of items every time, you don't have to ReDim the array. You can also set the assign the dimensions of the array to large number that you'll never hit. This isn't really good practice though. Lists can be used similarly to arrays, but you can add and remove elements a little easier. Up to you.

    Imports System.IO

    List:

    Dim R As List(Of String) = New List(Of String)
    Dim buffer As String
    Dim SR as StreamReader = new StreamReader(filepath)
    While Not (String.IsNullOrEmpty(buffer = sr.ReadLine()))
    R.Add(buffer)
    End While

    You can refer to each element just like an array. So the first item would be R(0).
    To clear the array of values, use R.Clear(). If you load the array up more than once, make sure you clear it before you run the above code


    String Array:

    Dim strArray(0 to 7) As String
    Dim buffer As String
    Dim i As Integer
    While Not (String.IsNullOrEmpty(buffer = sr.ReadLine()))
    strArray(i) = buffer
    i = i + 1
    End While

    Edit:

    To load the list/array based on the listBox items do this:

    Dim index As Integer 'For array
    For Each strItem As String In listBox1.Items
    R.Add(strItem) 'List
    strArray(index) = strItem
    index = index + 1
    Next

    Edit 2:

    Not really sure what you're trying to do, but if you are just trying to load the listbox from a file, you don't even need a list or array if you modify the above code to:

    Dim buffer As String
    Dim SR as StreamReader = new StreamReader(filepath)
    While Not (String.IsNullOrEmpty(buffer = sr.ReadLine()))
    listBox1.Items.Add(buffer)
    End While

  4. #4
    Salvage Bans
    Join Date
    Apr 2006
    Posts
    763
    BG Level
    5
    FFXI Server
    Lakshmi

    Awesome, thank you Altimus. Winding down for the night but I'ma give that a shot tomorrow.

  5. #5
    Chram
    Join Date
    Nov 2007
    Posts
    2,668
    BG Level
    7
    FFXIV Character
    Avelle Asteria
    FFXIV Server
    Hyperion
    FFXI Server
    Phoenix
    WoW Realm
    Alleria

    Well I work in C# as well, but have no VB experience so I'll just defer to Altimus. Post back if you still run into more issues Datonare.

  6. #6
    Salvage Bans
    Join Date
    Apr 2006
    Posts
    763
    BG Level
    5
    FFXI Server
    Lakshmi

    Yeah, I wish I could be doing C#, but this course is mandatory

    Okay I was too eager to try it out, so I tried out the List option instead of the array. But when I click on a listbox index the while loop seems to be infinite.

  7. #7
    Fake Numbers
    Join Date
    Jun 2005
    Posts
    86
    BG Level
    2
    FFXI Server
    Bahamut
    WoW Realm
    Mal'Ganis

    Whoops, VB is weird... do this:

    While Not (sr.EndOfStream)
    R.Add(sr.ReadLine())
    End While

  8. #8
    Salvage Bans
    Join Date
    Apr 2006
    Posts
    763
    BG Level
    5
    FFXI Server
    Lakshmi

    Oh geez you are the best. Works great.

    Since the text file's field is "1 John Smith" would I be able to separate the 1 and John with a comma and use a delimiter so I can pick out just the name and display that? Teacher is super picky and will dock me a point or two if I have the number in there... at this point I'm so friggin psyched it's working this far, but I figure I can try to go for a perfect if possible lol

  9. #9
    Fake Numbers
    Join Date
    Jun 2005
    Posts
    86
    BG Level
    2
    FFXI Server
    Bahamut
    WoW Realm
    Mal'Ganis

    If your input lines are always in the format "<number><space><name>"

    Dim line As String()
    Dim R As List(Of String) = New List(Of String)
    Dim sr As StreamReader = New StreamReader("file.dat")
    While Not (sr.EndOfStream)
    line = sr.ReadLine().Split(" ")
    Dim name As String = ""
    For i As Integer = 1 To line.Length - 1
    name = name & line(i) & " "
    Next
    R.Add(name.TrimEnd(" "))
    End While


    It will include everything after the first space.

    Edit:

    Added the TrimEnd

    Also, if you use a comma instead of a space for the delimiter between the number and name you can simply use:

    R.Add(sr.ReadLine().Split(",")(1))

  10. #10
    Salvage Bans
    Join Date
    Apr 2006
    Posts
    763
    BG Level
    5
    FFXI Server
    Lakshmi

    You know your stuff. You made my weekend, thank you very much!

  11. #11
    Fake Numbers
    Join Date
    Jun 2005
    Posts
    86
    BG Level
    2
    FFXI Server
    Bahamut
    WoW Realm
    Mal'Ganis

    Sure, hope it helped.

Similar Threads

  1. Programming help
    By Lost in forum Tech
    Replies: 13
    Last Post: 2012-02-28, 09:28
  2. Java program Help: KI tracker
    By Skie in forum Tech
    Replies: 19
    Last Post: 2011-04-08, 09:31
  3. Programming help
    By Bloodfire in forum Tech
    Replies: 15
    Last Post: 2011-04-07, 16:40
  4. Beginner Programming Help Thread
    By Callisto in forum Tech
    Replies: 13
    Last Post: 2011-03-04, 18:24
  5. Replies: 8
    Last Post: 2010-02-20, 01:21