how many controls you plan on having.

Solved

Posted on 2007-10-10

Hi

Is it possible to take a number from a text box lets say 240 then divide it by 72 and display the result as numbers in other text boxs. For example so I end up with the following

txtbox1 reads 280 - divide this by 72

so that...........................

txtbox2 reads 72

txtbox3 reads 72

txtbox4 reads 72

txtbox5 reads 64

Is it possible to take a number from a text box lets say 240 then divide it by 72 and display the result as numbers in other text boxs. For example so I end up with the following

txtbox1 reads 280 - divide this by 72

so that......................

txtbox2 reads 72

txtbox3 reads 72

txtbox4 reads 72

txtbox5 reads 64

26 Comments

how many controls you plan on having.

make the textboxes a control array.

dim n as integer, i as integer, m as integer

n = int(240 / 72)

for i = 1 to n

textbox(n) = 72

next

m = 240 mod 72

if m > 0 then

textbox(n + 1) = m

end if

dim n as integer, i as integer, m as integer

dim num1 as integer, num2 as integer

num1 = 240

num2 = 72

n = int(num1 / num2 )

if n > 0 then

for i = 0 to n - 1

textbox(n) = num2

next

end if

m = num1 mod num2

if m > 0 then

textbox(n) = m

end if

dim n as integer, i as integer, m as integer

dim num1 as integer, num2 as integer

num1 = 240

num2 = 72

n = int(num1 / num2 )

if n > 0 then

for i = 0 to n - 1

if i <= textbox.ubound then

textbox(i) = num2

end if

next

end if

m = num1 mod num2

if m > 0 and n <= textbox.ubound then

textbox(n) = m

end if

10 10 10 5 for 35 / 10

dim n as integer, i as integer, m as integer

dim num1 as integer, num2 as integer

num1 = cint(txtMyNumber.text) 'the textbox holds the number to divide

num2 = 72 'size of each part

n = int(num1 / num2 )

if n > 0 then

for i = 0 to n - 1

if i <= textbox.ubound then

textbox(i) = num2

end if

next

end if

m = num1 mod num2

if m > 0 and n <= textbox.ubound then

textbox(n) = m

end if

dim n as integer, m as integer, i as integer

dim num1 as integer, num2 as integer

num1 = cint(txtMyNumber.text) 'the textbox holds the number to divide

num2 = 72 'size of each part

n = int(num1 / num2 )

m = num1 mod num2

for i = txtBox.lbound to txtBox.ubound

select case i + 1

case is <= n

txtBox(i) = num2

case n + 1

txtBox(i) = m

case else

txtBox(i) = 0

end select

next i

Start a New Project.

Add three TextBoxes and a CommandButton to the Form.

Set the Index property for Text3 to 0 (zero).

Option Explicit

Private Sub Form_Load()

Text1.Text = "280"

Text2.Text = "72"

End Sub

Private Sub Command1_Click()

Dim i As Integer

For i = Text3.UBound To Text3.LBound + 1 Step -1

Unload Text3(i)

Next i

Dim dividend As Integer

Dim divisor As Integer

Dim cnt As Integer

cnt = 0

dividend = CInt(Text1.Text)

divisor = CInt(Text2.Text)

While dividend > divisor

If cnt > Text3.UBound Then

Load Text3(cnt)

Text3(cnt).Left = Text3(Text3.LBound).Left + (Text3(Text3.LBound).Width

Text3(cnt).Visible = True

End If

Text3(Text3.UBound).Text = divisor

dividend = dividend - divisor

cnt = cnt + 1

Wend

If dividend > 0 Then

If cnt > Text3.UBound Then

Load Text3(cnt)

Text3(cnt).Left = Text3(Text3.LBound).Left + (Text3(Text3.LBound).Width

Text3(cnt).Visible = True

End If

Text3(Text3.UBound).Text = dividend

End If

End Sub

I get an error message on (txtMyNumber.Text) the .Text says Method or data member not found

Thanks

Andy

Yes I tried it and it works great only problem is I dont want flying textboxs across my forms as i do not have the space.

Many Thanks

Andy

...and what if more textboxes were required? Would the last number be larger than the divisor then?

(this requires five boxes to get a remainder less than 72)

72, 72, 72, 72, 12

But you would want?

(because you only want four boxes?)

72, 72, 72, 84

Sorry on reflection I have probably misled you a little i will never need more than 5 txtboxs

Thanks

Andy

If not, do you want to use the Fixed TextBoxes (Text1, Text2, Text3, etc...) or the TextBox Control Array approach?

txtbox1 reads 280 - divide this by 72

so that......................

txtbox2 reads 72

txtbox3 reads 72

txtbox4 reads 72

txtbox5 reads 64

So is the value 72 coming from a TextBox too?...or this a fixed value?

(form has Command1, txtBoxDividend, txtBoxDivisor, txtBox1, txtBox2, txtBox3, txtBox4, txtBox5)

Option Explicit

Private Sub Form_Load()

txtBoxDividend.Text = "280"

txtBoxDivisor.Text = "72"

End Sub

Private Sub Command1_Click()

Dim i As Integer

Dim dividend As Integer

Dim divisor As Integer

dividend = CInt(txtBoxDividend.Text)

divisor = CInt(txtBoxDivisor.Text)

For i = 1 To 5

If dividend > 0 Then

If dividend >= divisor Then

Me.Controls("txtBox" & i).Text = divisor

dividend = dividend - divisor

Else

Me.Controls("txtBox" & i).Text = dividend

dividend = 0

End If

Else

Me.Controls("txtBox" & i).Text = ""

End If

Next

End Sub

I get a runtime error on the line

Me.Controls("txtBox" & i).Text = divisor

Object doesnt support this property or method

Thanks

aNDY

the code I gave was for a form with the controls put on it. so here you'll need;

- a textbox on the form, name it txtMyNumber this is where you'll put the number to be divided

- 5 textboxes which will display the numbers. put a textbox, name it txtBox, select it, hit ctrl-c to copy it, select the form, hit ctrl-v to paste it, it will ask you if you want to make a control array, choose 'yes', this will simply put a number ( 0 ) to the 'index' property of the control and paste the new copy with the index ( 1 ) so the controls are now parts of an array. a control array is controls with the same name, each having a unique index number. use this procedure to have 5 textboxes with the name 'txtBox'. or if you already have 5 boxes, simply give all of them an index number from 0 to 4 and then rename all to 'txtBox'

and paste this to your form :

Option Explicit

Private Sub Form_Load()

txtMyNumber = ""

End Sub

Private Sub txtMyNumber_Change()

Dim n As Integer, m As Integer, i As Integer

Dim num1 As Integer, num2 As Integer

num1 = Val(txtMyNumber.Text) 'the textbox holds the number to divide

num2 = 72 'size of each part

n = Int(num1 / num2)

m = num1 Mod num2

For i = txtBox.lbound To txtBox.ubound

Select Case i + 1

Case Is <= n

txtBox(i) = num2

Case n + 1

txtBox(i) = m

Case Else

txtBox(i) = 0

End Select

Next i

End Sub

Option Explicit

Private Sub Form_Load()

txtDivider = 72

txtMyNumber = ""

End Sub

Private Sub txtMyNumber_Change()

Dim n As Integer, m As Integer, i As Integer

Dim num1 As Integer, num2 As Integer

num1 = Val(txtMyNumber.Text) 'the textbox holds the number to divide

num2 = Val(txtDivider.Text) 'size of each part

If num2 > 0 Then

n = Int(num1 / num2)

m = num1 Mod num2

For i = txtBox.lbound To txtBox.ubound

Select Case i + 1

Case Is <= n

txtBox(i) = num2

Case n + 1

txtBox(i) = m

Case Else

txtBox(i) = 0

End Select

Next i

End If

End Sub

here is the working project file : www.onursafak.com/files/nu

if you don't want to use control arrays, you'll need to have the controls "txtBox1", "txtBox2"... "txtBox5", and this code :

Option Explicit

Private Sub Form_Load()

txtDivider = 72

txtMyNumber = ""

End Sub

Private Sub txtDivider_Change()

C

End Sub

Private Sub txtMyNumber_Change()

C

End Sub

Private Sub C()

Dim n As Integer, m As Integer, i As Integer

Dim num1 As Integer, num2 As Integer

num1 = Val(txtMyNumber.Text) 'the textbox holds the number to divide

num2 = Val(txtDivider.Text) 'size of each part

If num2 > 0 Then

n = Int(num1 / num2)

m = num1 Mod num2

For i = 0 To 4

Select Case i + 1

Case Is <= n

GetTBox(i) = num2

Case n + 1

GetTBox(i) = m

Case Else

GetTBox(i) = 0

End Select

Next i

End If

End Sub

Private Function GetTBox(n As Integer) As TextBox

Select Case n

Case 0

Set GetTBox = txtBox1

Case 1

Set GetTBox = txtBox2

Case 2

Set GetTBox = txtBox3

Case 3

Set GetTBox = txtBox4

Case 4

Set GetTBox = txtBox5

End Select

End Function

here is the working project file : www.onursafak.com/files/nu

all of this based on my first answer.

good luck =)

Instead of a hard-wired GetTBox() function, you can just use:

Me.Controls("txtBox" & i)

samandrew...

You are working in VB6 right?...If not, then what?

For my last submission make sure you have all the necessary controls on your form and properly named:

Command1, txtBoxDividend, txtBoxDivisor, txtBox1, txtBox2, txtBox3, txtBox4, txtBox5

Title | # Comments | Views | Activity |
---|---|---|---|

Macro which simplifies accounts | 6 | 49 | |

SSRS expression Issue finding a string | 10 | 33 | |

vb script Microsoft Word tables portion of based on bookmark, count and add rows | 1 | 31 | |

How does CurrentUser work? | 10 | 10 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**19** Experts available now in Live!