|
-
Apr 15th, 2013, 01:11 PM
#1
Thread Starter
New Member
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
Last edited by ser nicky; Apr 15th, 2013 at 01:18 PM.
-
Apr 15th, 2013, 01:21 PM
#2
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
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
-
Apr 15th, 2013, 01:22 PM
#3
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 15th, 2013, 01:24 PM
#4
Frenzied Member
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
-
Apr 15th, 2013, 01:47 PM
#5
Thread Starter
New Member
Re: Finding max value of textbox
Thank you all guys, I really appreciate it. Cheers!
-
Apr 15th, 2013, 02:14 PM
#6
Re: Finding max value of textbox
vb Code:
Dim textboxes = Me.Controls.OfType(Of TextBox).ToArray().OrderBy(Function(tb) CInt(tb.TextLength)).Last()
-
Apr 15th, 2013, 03:25 PM
#7
Frenzied Member
Re: Finding max value of textbox
 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
-
Apr 16th, 2013, 01:19 AM
#8
Re: Finding max value of textbox
I meant Text lol since would not be converting the length., slight over sight in rushing.
-
Apr 16th, 2013, 01:22 AM
#9
Re: Finding max value of textbox
 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.
-
Apr 16th, 2013, 01:34 AM
#10
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))
-
Apr 16th, 2013, 12:36 PM
#11
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|