Break Down numbers program

vickp07

Member
Joined
Sep 8, 2009
Messages
7
Programming Experience
3-5
I am currently writing a program that when a user enters a number such as 1267 and clicks a button the number will then be
broken down by its ones place, tens place, and so on. So it would show up as One place is: 7, Tens place is: 6, and so on.

Well I need a little help trying to figure out what code I would write to test or separate each single integer. So far I have only been able to separate the ones place using:

Dim ones As Integer = Microsoft.VisualBasic.Right(TextBox1.Text, 1)

'display each value in the corresponding place
Label11.Text = ones

of course this code wouldnt work for the rest of the digits entered.
If a user were to enter a bigger number lets say:
12345
then i would need to display
thousands place is: 12
hundreds place is: 3 and so on and so on

Any help is greatly appreciated
Thanks
 
Don't use Right. You can simply index the string itself to get each individual character. Just note that the lowest index is to the left.
VB.NET:
Dim num As String = "481674"

For index As Integer = num.Length - 1 To 0 Step -1
    MessageBox.Show(num(index))
Next
 
ok so i tried the code that was posted above and understand how its being used.

but I need to send each digit to a textbox so when the loop contains the rightmost digit or "ones place" i need to send it out to a textbox, then the next digit would be sent out to another textbox for "Tens place"

how can i do this
 
My code was just an example. It shows you how to get a specific character at a specific index. I would imagine that you also know how to display a string in a TextBox. Just put the two together.
 
well this is what i have written so far with your help:

For index As Integer = TextBox1.Text.Length - 1 To 0 Step -1

MessageBox.Show(TextBox1.Text(index))

Next

but i dont know or understand how to send the first indexed item to the first textbox to be displayed, then continue on
 
As I said, that code is just an EXAMPLE. You are supposed to take the principle and apply it to your own specific situation. The principle is that you can get a single character from a string simply by indexing it. You don't have to use a For loop. If you want to get the characters at one or more specific indexes then you can simply specify those indexes.
 
As I said, that code is just an EXAMPLE. You are supposed to take the principle and apply it to your own specific situation. The principle is that you can get a single character from a string simply by indexing it. You don't have to use a For loop. If you want to get the characters at one or more specific indexes then you can simply specify those indexes.

He is right, with the example he's given you, it should be easy to solve your problem, just look at how he gets the single character. You have to learn to be able apply concepts, rather then just copy/pasting this will help you should you encounter a different problem in the future, that could possibly be solved using the same concept.
 
This is actually a very tricky problem. I solved it by using a For Each Controls block and giving each textbox for displaying the place value a Tag property. (You can type anything in the Tag property for those textboxes.)

Here is the code. It uses a string array of the digits in the input textbox (or it could use a Char array). It takes up to 6 places along with 6 textboxes to display each place. The order of the textbox's creation is important -- it must be done with the Ones place textbox before the others (or else the y increment needs to be reversed to decrement). The textbox used for the user input must have the Maximum property set to 6.


VB.NET:
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim snum As String = TextBox1.Text
		Dim ln As Integer = snum.Length
		If ln < 6 Then snum = Strings.Space(6 - ln) & snum
		Dim dig(5) As String
		For x As Integer = 0 To 5
			dig(x) = snum.Substring(x, 1)
		Next

		Dim y As Integer = 0
		For Each ctl As Control In Controls
			If TypeOf ctl Is TextBox Then
				Dim txt As TextBox = CType(ctl, TextBox)
				If txt.Tag IsNot Nothing Then
					txt.Text = dig(y)
					y += 1
					If y = 6 Then Exit For
				End If
			End If
		Next ctl
	End Sub


I'm sure there is more than one solution. If you have one, I'd like to see it.
 
with 6 textboxes (TextBox1, TextBox2 etc):
VB.NET:
For i As Integer = 1 To 6
    Me.Controls("TextBox" & i.ToString).Text = Me.InputTextBox.Text(i - 1)
Next
or reversed
VB.NET:
Dim chars() As Char = Me.InputTextBox.Text.ToCharArray
Array.Reverse(chars)
For i As Integer = 1 To 6
    Me.Controls("TextBox" & i.ToString).Text = chars(i - 1)
Next
IMO simple as can be :)
 
Good one, JohnH. Nice and short. No need to reverse if using numbered names.

I added code so user may enter any number of digits up to 6:

VB.NET:
		Dim snum As String = txtEnter.Text
		Dim ln As Integer = txtEnter.Text.Length
		If ln < 6 Then snum = Strings.Space(6 - ln) & snum
		Dim y As Integer = 6
		For x As Integer = 0 To 5
			Me.Controls("txt" & y.ToString).Text = snum.Substring(x, 1)
			y -= 1
		Next


Note: I numbered the textboxes in order starting with 1 in the ones place. If you do it in reverse (with 1 in the 6th place), then you don't need the y variable. Just change the y to x + 1.
 
Last edited:
Back
Top