Display characters Vertically in Listbox?

Greenhorn

Newcomer
Joined
Mar 15, 2011
Messages
1
Hey guys, new to the forum as of today and am looking forward to learning a lot from you all. Currently I'm working on a VB basic form for my college programming class. One of the requirements is to display the text in a list box in reverse order and vertically. I have this for the reversal of the text:

lstbxItemsPurchased.Items.Add(StrReverse(txtItemPurch.Text))

However, I have no idea how to make characters displayed vertically and seperately from each other in terms of the line they are one.

Here's a sample but I want this to work regardless of the number of characters entered.

"laptop computer" should become:

r
e
t
u
p
m
o
c

p
o
t
p
a
l

Thanks in advance guys
 
Welcome to the forum. :)

Since each letter should be displayed on a seperate line, there should be a .Add() call for each letter. So, you have to loop through the string from front to back, or back to front and add each letter to the listbox seperately.
 
It sounds to me like the purpose of this excersise is to learn and practice loops. To that end, StrReverse is not the correct approach. That would be like a student using a calculator on a long division test. If you're looking to do the reversing yourself, here are my thoughts.

You can get individual characters from a string using the Chars property. (Since Chars is the "default property", you could also just use parens.)
Code:
Dim SomeString As String = "ABCDEFG"

[COLOR="Green"]' Get the third character of a string[/COLOR]
MessageBox.Show(SomeString.Chars(2))
[COLOR="Green"]' This is equivalent and produces identical results.[/COLOR]
MessageBox.Show(SomeString(2))

You can reverse a string with a loop that goes from the last character to the first, taking each character and appending it to a variable. Remember that the character indecies start at zero, so the last character would be one less than the length of the string:
Code:
Dim LastIndex As Integer = SomeString.Length - 1
To run a loop backwards, you need to specify a negative step.
Code:
[COLOR="Green"]' A value of -1 is added to i each iteration, until endValue is reached[/COLOR]
For i As Integer = startValue To endValue [COLOR="Blue"]Step - 1[/COLOR]
And, just to cover all my bases, you use the & symbol to combine strings.
Code:
Dim ABCD As String = "ABCD"
Dim DCBA As String = ABCD(3) [COLOR="Green"]'D'[/COLOR]
DCBA = DCBA & ABCD(2) [COLOR="Green"]'D' & 'C' -> 'DC'[/COLOR]
DCBA = DCBA & ABCD(1) [COLOR="Green"]'DC' & 'B' -> 'DCB'[/COLOR]
DCBA = DCBA & ABCD(0) [COLOR="Green"]'DCB' & 'A' -> 'DCBA'[/COLOR]
That's everything you would need to reverse a string if StrReverse did not exist.
 
Back
Top