Tricky mathematical problem using textboxs

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
samandrewAsked:
Who is Participating?
 
MrRobotConnect With a Mentor Commented:
if you want to get the divider from a textbox, you'll put a textbox named "txtdivider" and use this code :


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/numbers2.zip



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/numbers3.zip


all of this based on my first answer.

good luck =)
0
 
Patrick MatthewsCommented:
Sure it's possible, but you need to elaborate some more on what you're trying to do, and
how many controls you plan on having.
0
 
MrRobotCommented:
greetings samandrew,


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
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
MrRobotCommented:
you can expand it. the control array will be 0 based (beginning from 0) so you actually need to minus the array number by 1, like n - 1. and lets use variables instead of 240 and 72. other than this, put a check if the result is 0.



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
0
 
MrRobotCommented:
ah, textbox(n) has to be textbox(i), a syntax error =) and lets put a check if the number of results will exceed the number of textboxes.



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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I must need more coffee...going with matthewspatrick's comment...what ~exactly~ is going on with these numbers?...
0
 
MrRobotCommented:
hehe! the author wants to divide the number into parts with given size, and put the remainder in the last box. like
10 10 10 5 for 35 / 10
0
 
MrRobotCommented:
oh, and you want to get the number from a textbox, so,


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
0
 
MrRobotCommented:
and here is the cleaner second way, does all textboxes in a single pass so the other textboxes are reset to 0, and no more checks needed.


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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Thanx for the explanation MrRobot...  ;)

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 * cnt)
            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 * cnt)
            Text3(cnt).Visible = True
        End If
        Text3(Text3.UBound).Text = dividend
    End If
End Sub
0
 
samandrewAuthor Commented:
Hi Mr Robot
I get an error message on (txtMyNumber.Text) the .Text says Method or data member not found

Thanks

Andy
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Did you try the instructions/code in my last post samandrew?...
0
 
samandrewAuthor Commented:
Sorry Idle Mind

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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Huh?...so you want a FIXED number of textboxes?

...and what if more textboxes were required?  Would the last number be larger than the divisor then?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
For instance if we used 300 and 72:
(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
0
 
samandrewAuthor Commented:
Hi Idle Mind

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

Thanks

Andy
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
So do you have any solutions that you are happy with or do you need something else?

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

I would like to use fixed textboxes please

Thanks

andy
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Your original description was:

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?
0
 
samandrewAuthor Commented:
hi

72 is from a text box as well

thanks

andy
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
How about this then?
(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
0
 
samandrewAuthor Commented:
Hi

I get a runtime error on the line

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

Object doesnt support this property or method



Thanks

aNDY
0
 
MrRobotCommented:
samandrew,

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


0
 
MrRobotCommented:
here is the working project file :

www.onursafak.com/files/numbers.zip

good luck =)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
MrRobot...

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
0
 
MrRobotCommented:
Idle Mind,

You're right, it can be accessed that way, I used a translator cause it makes it possible to use textboxes with any names. I guess using a control array was no trouble for the author anyway, "fixed" meant textboxes not dynamically created I guess, as in my first approach.
0
All Courses

From novice to tech pro — start learning today.