Results 1 to 11 of 11

Thread: How to format strings so that they go into lstBox neater.

  1. #1

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Resolved How to format strings so that they go into lstBox neater.

    Hi there,

    How can i format a string so that it all a fixed length of character so that i can put them in a listbox. I have tried substring, but if the length of the word is less than the specfied end length, it returns an error.

    any ideas folks ?
    Last edited by dinosaur_uk; Oct 25th, 2004 at 09:05 AM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Do a check for length first. If above value x, then use substring, else leave as it is.

    Show your code.

    Also, tell us why you don't want to show the entire string in the listbox... other than it appearing 'neat'.

  3. #3

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098
    Oh i have three strings, like number, description etc etc

    usually the description is too long or too short, so the next variable looks terrible...

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    OK, show your code so far.

  5. #5
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Hi Mendhak, hi Dinosaur......I'm not sure to understand what you want to obtain, dear Dinosaur, but I suggest you to have a look to padright and padleft methods, perhaps thay can be useful for you, if not, sorry.
    Live long and prosper (Mr. Spock)

  6. #6

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098
    (vbcode)
    Private Sub btnDcName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDcName.Click
    'This is for the list of DC channel names.
    lstDcName.Items.Clear()
    For intchannel = 1 To 256
    GetDCData(ThreadId, intchannel, valChannel) 'gets DC data and outputs the value to valChannel
    GetDCName(ThreadId, intchannel, dcname, 1) 'gets DC name and outputs it to dcname

    'This if loop is to format the list to make it look tidier
    If intchannel.ToString.Length = 1 Then
    lstDcName.Items.Add(intchannel & " : " & dcname) 'Adds the items into the array.
    ElseIf intchannel.ToString.Length = 2 Then
    lstDcName.Items.Add(intchannel & " : " & dcname) 'Adds the items into the array.
    Else
    lstDcName.Items.Add(intchannel & ": " & dcname) 'Adds the items into the array.
    End If

    'Puts the entire DC channel namelist into an array
    DcArray(intchannel) = valChannel
    Next
    (/vbcode)

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by dinosaur_uk
    If intchannel.ToString.Length = 1 Then
    lstDcName.Items.Add(intchannel & " : " & dcname) 'Adds the items into the array.
    ElseIf intchannel.ToString.Length = 2 Then
    lstDcName.Items.Add(intchannel & " : " & dcname) 'Adds the items into the array.
    Else
    lstDcName.Items.Add(intchannel & ": " & dcname) 'Adds the items into the array.
    End If
    Here, you are simply taking the numbers, converting to string, and adding to a listbox. And all conditions are doing the same thing. Is this the problem? Which part of this poses the problem to you? three digit numbers?

  8. #8

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098
    This is me trying to format the index. The problem is that the strings pulled from a data file is of a variable length... and i am not sure how to format it...

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Here's what I did, I created an array of strings of varying lengths, and added them to the listbox like so:

    VB Code:
    1. Dim strArr(6) As String
    2.         strArr(0) = "34"
    3.         strArr(1) = "2349823"
    4.         strArr(2) = "3"
    5.         strArr(3) = "af2323"
    6.         strArr(4) = "3asdf4"
    7.         strArr(5) = "23ash49823"
    8.         strArr(6) = ".."
    9.  
    10.  
    11.         Dim intCount As Integer
    12.         For intCount = 0 To strArr.GetUpperBound(0)
    13.             If strArr(intCount).Length > 3 Then
    14.                 ListBox1.Items.Add(strArr(intCount).Substring(0, 3) & "...")
    15.             Else
    16.                 ListBox1.Items.Add(strArr(intCount))
    17.             End If
    18.         Next

    Is this what you were looking for? If not, post a snapshot of what you have, and an image of what you'd like to have.

  10. #10

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    All sorted !

    Thank you very much, you are a VB Angel, bringing me to VB heaven....

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: All sorted !

    Originally posted by dinosaur_uk
    Thank you very much, you are a VB Angel, bringing me to VB heaven....
    You flirt. :blush:

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