Finding max value of textbox
Hi guys,
I need assistance with following code. There are 4 textboxes on the left side of the form and user is entering values in each of them. There is also fifth textbox on the right side of the form. After checking all values, fifth textbox (on the right side) need to move on the same height as height of textbox with maximum value entered. For example, if you enter following values:
textbox1(3)
textbox2(4)
textbox3(1)
textbox4(2)
Fifth textbox needs to move on the same height as textbox2.
I tried with following code:
Dim First As Integer
Dim Second As Integer
Dim Third As Integer
Dim Fourth As Integer
Dim Max As Integer
First = val(textbox1.text)
Second = val(textbox2.text)
Third = val(textbox3.text)
Fourth = val(textbox4.text)
If (First>Second) and (First>Third) and (First > Fourth) then
Max = First
textbox5.height = textbox1.height
ElseIf (Second>First) and (Second>Third) and (Second>Fourth) then
Max = Second
textbox5.height = textbox2.height
ElseIf (Third>First) and (Third>Second) and (Third>Fourth) then
Max = Third
textbox5.height = textbox3.height
Else
Max = Fourth
Textbox5.height = textbox4.height
I am absolute begginer wth VB, so please if somebody can help me with this code or some other idea is welcomed also.
Thanks in advance
Re: Finding max value of textbox
I would loop through the textboxes to find the one with the highest value.
Code:
'find text box with highest value
Dim maxValue As String = ""
Dim maxTextBox As TextBox
For Each txt In New TextBox() {textbox1, textbox2, textbox3, textbox4}
If txt.Text > maxValue Then
maxValue = txt.Text
maxTextBox = txt
End If
Next
'set height of tb5
textbox5.Height = maxTextBox.Height
Re: Finding max value of textbox
try this:
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim First As Integer
Dim Second As Integer
Dim Third As Integer
Dim Fourth As Integer
If Integer.TryParse(TextBox1.Text, First) And Integer.TryParse(TextBox2.Text, Second) And Integer.TryParse(TextBox3.Text, Third) And Integer.TryParse(TextBox4.Text, Fourth) Then
Dim values(,) As Object = {{TextBox1, First}, {TextBox2, Second}, {TextBox3, Third}, {TextBox4, Fourth}}
Dim highestIndex As Integer = -1
Dim highest As Integer = -1
For x As Integer = 0 To 3
If CInt(values(x, 1)) > highest Then
highestIndex = x
highest = CInt(values(x, 1))
End If
Next
TextBox5.Top = DirectCast(values(highestIndex, 0), TextBox).Top
End If
End Sub
End Class
Re: Finding max value of textbox
How about putting the textbox values in an arrray and just use the max function. once toy find the mx value then se the height
here is an example:
http://www.dotnetperls.com/math-max-min-vbnet
Re: Finding max value of textbox
Thank you all guys, I really appreciate it. Cheers!
Re: Finding max value of textbox
vb Code:
Dim textboxes = Me.Controls.OfType(Of TextBox).ToArray().OrderBy(Function(tb) CInt(tb.TextLength)).Last()
Re: Finding max value of textbox
Quote:
Originally Posted by
ident
vb Code:
Dim textboxes = Me.Controls.OfType(Of TextBox).ToArray().OrderBy(Function(tb) CInt(tb.TextLength)).Last()
Code:
Dim textboxes = Me.Controls.OfType(Of TextBox).ToArray().OrderBy(Function(tb) CInt(tb.Text)).Last()
Not TextLength :P
Re: Finding max value of textbox
I meant Text lol since would not be converting the length., slight over sight in rushing. ;)
Re: Finding max value of textbox
Quote:
Originally Posted by
Zach_VB6
Code:
Dim textboxes = Me.Controls.OfType(Of TextBox).ToArray().OrderBy(Function(tb) CInt(tb.Text)).Last()
Not TextLength :P
As well as that correction, the ToArray call serves no useful purpose. Also, rather than calling OrderBy and then Last, just call Max. That code also will fail if any TextBox doesn't contain a valid integer.
Re: Finding max value of textbox
Here is a couple of examples that fix all those issues:
Code:
Dim maxTextBox = Controls.OfType(Of TextBox)().Max(Function(tb) If(Integer.TryParse(tb.Text, Nothing),
Integer.Parse(tb.Text),
Integer.MinValue))
Code:
Dim maxTextBox = Controls.OfType(Of TextBox)().
Where(Function(tb) Integer.TryParse(tb.Text, Nothing)).
Max(Function(tb) Integer.Parse(tb.Text))
Re: Finding max value of textbox
I agree with validating parsing the Integer but it's the same with exception handling. I wont include it in my examples. Maybe i should rethink including in the future validation.
Didn't no about Max. Appreciated John.