Results 1 to 11 of 11

Thread: Finding max value of textbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    6

    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.

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    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

  4. #4
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    6

    Re: Finding max value of textbox

    Thank you all guys, I really appreciate it. Cheers!

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Finding max value of textbox

    vb Code:
    1. Dim textboxes = Me.Controls.OfType(Of TextBox).ToArray().OrderBy(Function(tb) CInt(tb.TextLength)).Last()

  7. #7
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Finding max value of textbox

    Quote Originally Posted by ident View Post
    vb Code:
    1. 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

  8. #8
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Finding max value of textbox

    I meant Text lol since would not be converting the length., slight over sight in rushing.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Finding max value of textbox

    Quote Originally Posted by Zach_VB6 View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    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
  •  



Click Here to Expand Forum to Full Width