calculation with textboxes

mzone

Member
Joined
May 18, 2006
Messages
5
Programming Experience
Beginner
I desperately need help here. Ive designed a form with 5 textboxes, i need to write a program (vb.net) to take each value in each of the textboxes and give me the highest value. For example textbox1 holds 3, textbox2 hols 7, textbox3 holds 9, textbox4 holds 6 and textbox5 holds 2, therefore when i ask for the hihest value it should give me 9 as a result, thanks! Im a beginner and not too good with arrays, I'd really appreciate an sample code, thanks
 
Last edited:
Just note that TextBoxes contains String objects, while it appears that you want to deal with numbers. There are various way s to convert from a string to a number but most of them will crash your app if the user enters invalid characters. The safest and most efficient way to convert the contents of a TextBox to an Integer in .NET 1.1 is like this:
VB.NET:
Dim numDbl As Double
Dim numInt As Integer

If Double.TryParse(Me.TextBox1.Text, _
                   Globalization.NumberStyles.Integer, _
                   Nothing, _
                   numDbl) Then
    'The conversion was successful.
    numInt = Convert.ToInt32(numDbl)
Else
    MessageBox.Show("Please enter a valid integral value.")
End If
The other safe way that some may suggest is to use the IsNumeric function but it i less efficient and cannot guarantee that the number is an Integer like TryParse can.
 
Creating a numerical TextBox would certainly be preferable but to do it properly is more complex than what I've posted. If you really only want integral values then a NumericUpDown would be the better way to go I think. I'm guessing that this is an assigned learning exercise though, so I think that what I have posted is the simplest way to prevent any run time excpetions being thrown while still using a TextBox. If you attempt to create a numerical TextBox and don't do it properly, like preventing the user pasting invalid values from the clipboard, then you'll either have to validate as I did anyway or else you could end up with your app crashing.
 
Sure John, you are alright ... i just thought that checking each textbox separately is a bit odd especially if you have numerous such controls.
Ok, Mzone ... assuming that you followed John's suggestion(s) (preserving entering of non numerical values) you could find the highest value as it follows:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myArray [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ArrayList[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]For [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] xControl [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Control [/SIZE][SIZE=2][COLOR=#0000ff]In [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Controls
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] xControl.GetType [/SIZE][SIZE=2][COLOR=#0000ff]Is [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2](TextBox) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2] myArray.Add([/SIZE][SIZE=2]xControl.Text)
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2]myArray.Sort()[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] intHighest [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = myArray.Count - 1
MessageBox.Show(myArray.Item(intHighest))
[/SIZE]

Just in case i attached the demo below

HTH
Regards ;)
 

Attachments

  • FindHighestValue.zip
    27.8 KB · Views: 25
Thanks a milllion but....

thank for the code, it work well, the only problem is that its lookinh through all the textboexes i have on my form and then giving the result. I only need it to check through some boxes, ive attached a snapshot of my form so you'll understand wot i mean, pls help!:confused::eek:

Ps Dont go crazy at work!!!!!
 

Attachments

  • my form.doc
    61 KB · Views: 25
Ok i got you ... just rename those fields (that group of controls) with some specific names i.e. txtFinalvalue1, txtFinalvalue2, txtFinalvalue3 etc.
Then only check if xControl's name starts with "txtFinalvalue" and only those controls add to the arrayList. Ok i will attach a demo again ;)
 

Attachments

  • FindHighestValue.zip
    31.4 KB · Views: 18
oof, how complicated. try this:
VB.NET:
Dim myDoubles(4) as Double
myDoubles(0) = CDbl(Text6.Text)
myDoubles(1) = CDbl(Text12.Text)
... and so on
Then implment the standard array looping routine:
VB.NET:
'set the max to a really really low number
Dim maxFound as Double = Double.MinimumValue
 
Loop For Each x as Double in myDoubles
if x is greater than maxFound Then store value of x in maxFound
Look at Next x
thats pseudocode.. i want you to do it yourself as a learning point. your gui looks pretty complex, and to be honest im really curious why youre designing something so in-depth looking when you arent sure of how to do a few basic things like arrays and "find the max" algorithms.. be careful you dont confuse yourself or bite off more than you can chew! :)
 
Last edited by a moderator:
Well, cjard you are absolutelly right about your last statement that he is designing something complex while have no knowledge about the array ... but, actually what he is doing is absolutely very simple. What he suppose to do is this:
just an additional if statement in the already existing code:
VB.NET:
[COLOR=#0000ff]For [SIZE=2]Each[/SIZE][/COLOR][SIZE=2] xControl [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Control [/SIZE][SIZE=2][COLOR=#0000ff]In [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Controls
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] xControl.GetType [/SIZE][SIZE=2][COLOR=#0000ff]Is [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2](TextBox) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]myArray.Add([/SIZE][SIZE=2]xControl.Text)
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
now make as it follows:
VB.NET:
[COLOR=#0000ff]For [SIZE=2]Each[/SIZE][/COLOR][SIZE=2] xControl [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Control [/SIZE][SIZE=2][COLOR=#0000ff]In [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Controls
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] xControl.GetType [/SIZE][SIZE=2][COLOR=#0000ff]Is [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2](TextBox) [/SIZE][SIZE=2][COLOR=#0000ff]Then [/COLOR][COLOR=seagreen]'now this is optional[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] If [COLOR=black]xControl.Name.StartsWith("txtPattern")[/COLOR] Then
[/COLOR][/SIZE][SIZE=2]     myArray.Add([/SIZE][SIZE=2]xControl.Text)[/SIZE]
[SIZE=2] [COLOR=blue]End If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]

Regards ;)
 
oh ya, i dont doubt that there are a million ways to skin this particular cat.. i tried to pick a simple one, but in the past i've used method similar to yours, like.. maybe enumerating all the controls ina form and seeing if the tag stored a hashtable, and if it did, asking the hastable for certain things.. then i can do other things with what is stored in the has.. oh, it gets very complicated in the end :) tag is very useful, so if he had to keep his names he could follow your advice but put soemthing in the tag.. like

If Tag.ToString = "find max" then
...

;)

good mmethods, all of them.. i was just trying as simply as possible to fit into what i presumed the OPs domain of knowledge to be.. maybe he hasnt even seen a FOR EACH loop yet, in which case I'd say he should really really be programming a Hello World console app, no offence of course.. but the world's fastest runner started out by taking baby steps :) so we should guide him and maybe caution him on this i think...
 
Back
Top