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 ?
Printable View
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 ?
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'.
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...
OK, show your code so far.
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.:rolleyes:
(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)
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?Quote:
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
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...
Here's what I did, I created an array of strings of varying lengths, and added them to the listbox like so:
VB Code:
Dim strArr(6) As String strArr(0) = "34" strArr(1) = "2349823" strArr(2) = "3" strArr(3) = "af2323" strArr(4) = "3asdf4" strArr(5) = "23ash49823" strArr(6) = ".." Dim intCount As Integer For intCount = 0 To strArr.GetUpperBound(0) If strArr(intCount).Length > 3 Then ListBox1.Items.Add(strArr(intCount).Substring(0, 3) & "...") Else ListBox1.Items.Add(strArr(intCount)) End If 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.
Thank you very much, you are a VB Angel, bringing me to VB heaven....
You flirt. :blush:Quote:
Originally posted by dinosaur_uk
Thank you very much, you are a VB Angel, bringing me to VB heaven....