We help IT Professionals succeed at work.

Tricky mathematical problem using textboxs

samandrew
samandrew asked
on
283 Views
Last Modified: 2013-12-26
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
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2010

Commented:
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.

Commented:
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

Commented:
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

Commented:
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
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
I must need more coffee...going with matthewspatrick's comment...what ~exactly~ is going on with these numbers?...

Commented:
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

Commented:
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

Commented:
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
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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

Author

Commented:
Hi Mr Robot
I get an error message on (txtMyNumber.Text) the .Text says Method or data member not found

Thanks

Andy
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Did you try the instructions/code in my last post samandrew?...

Author

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
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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?
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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

Author

Commented:
Hi Idle Mind

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

Thanks

Andy
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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?

Author

Commented:
Hi

I would like to use fixed textboxes please

Thanks

andy
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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?

Author

Commented:
hi

72 is from a text box as well

thanks

andy
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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

Author

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

Commented:
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


Commented:
here is the working project file :

www.onursafak.com/files/numbers.zip

good luck =)
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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

Commented:
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.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.