Results 1 to 13 of 13

Thread: [RESOLVED] How to rank 5 textbox value to another 5 textbox?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2024
    Posts
    22

    Resolved [RESOLVED] How to rank 5 textbox value to another 5 textbox?

    Hello,

    I want to rank my 5 textbox data to another 5 textbox. For example I have 5 data in my Five text box.

    TextBox1.text = 10
    TextBox2.text= 12
    TextBox3.text= 9
    TextBox4.text=15
    TextBox5.text=21

    I want to put this five textbox data to another five textbox (highest value to lowest value.

    Is it possible? And how?

    Thanks

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,709

    Re: How to rank 5 textbox value to another 5 textbox?

    Throw the values of your first 5 textboxes into an Array, sort the Array, then assign the sorted Array-Values to your second 5 Textboxes going backwards through the Array
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3
    Hyperactive Member
    Join Date
    Jul 2022
    Posts
    353

    Re: How to rank 5 textbox value to another 5 textbox?

    You could put the values in an array and use Reverse(), you need to import System.Array (edit 2: fixed typo)

    Here is an example:
    Code:
        Dim sortArray() As Int16 = {10, 12, 9, 15, 21}
        Dim displayResults As String = ""
    
        'Sort(sortArray)
        Reverse(sortArray)
    
        For Each i As Int16 In sortArray
            displayResults += i.ToString & ","
        Next
    
        MsgBox($"Array sorted is {displayResults}")
    have to switch browsers to upload a screenshot, Edge is not working EDIT: added screenshot

    edit: use the Reverse function.
    Attached Images Attached Images  
    Last edited by jdelano; Jul 25th, 2024 at 05:17 AM.

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,709

    Re: How to rank 5 textbox value to another 5 textbox?

    Quote Originally Posted by jdelano View Post
    You could put the values in an array and use Sort(), you need to import System.Array (edit 2: fixed typo)

    Here is an example:
    Code:
        Dim sortArray() As Int16 = {10, 12, 9, 15, 21}
        Dim displayResults As String = ""
    
        Sort(sortArray)
    
        For Each i As Int16 In sortArray
            displayResults += i.ToString & ","
        Next
    
        MsgBox($"Array sorted is {displayResults}")
    have to switch browsers to upload a screenshot, Edge is not working EDIT: added screenshot
    OP wants highest Value first.....
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5
    Hyperactive Member
    Join Date
    Jul 2022
    Posts
    353

    Re: How to rank 5 textbox value to another 5 textbox?

    Quote Originally Posted by Zvoni View Post
    OP wants highest Value first.....
    Uhg what a bonehead I am.

  6. #6
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,709

    Re: How to rank 5 textbox value to another 5 textbox?

    Quote Originally Posted by jdelano View Post
    Uhg what a bonehead I am.
    You do realize, that your Screenshot is not SORTED?

    EDIT: And let's not forget, that those values are in textboxes, wich would make them Strings, so assigning those Values to an Integer-Array would ne a TypeCast
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  7. #7
    Hyperactive Member
    Join Date
    Jul 2022
    Posts
    353

    Re: How to rank 5 textbox value to another 5 textbox?

    Quote Originally Posted by Zvoni View Post
    You do realize, that your Screenshot is not SORTED?

    EDIT: And let's not forget, that those values are in textboxes, wich would make them Strings, so assigning those Values to an Integer-Array would ne a TypeCast
    Goooodd grief ... I'm going back to bed and starting today over. (egg faced loser - out)


    EDIT: okay LAST time
    In order to sort the numbers properly they need to be an integer, so add them to an array like

    using this code:
    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim sortArray(4) As Int16
            Dim displayResults As String = ""
            Dim textboxIndex As Integer
            Dim txtBox As TextBox
    
            For textboxIndex = 1 To 5
                ' find each textbox and get the value entered
                txtBox = CType(Me.Controls.Find("TextBox" & textboxIndex.ToString(), True)(0), TextBox)
    
                ' add the value to the array
                sortArray(textboxIndex - 1) = Convert.ToInt16(txtBox.Text)
            Next textboxIndex
    
            Sort(sortArray)
            Reverse(sortArray)
    
            For Each i As Int16 In sortArray
                displayResults += i.ToString & ","
            Next
    
            MsgBox($"Array sorted is {displayResults}")
    
        End Sub
    I'm going to find a rock to hide under now.

    You'll need to test for whether an actual number is in the textbox, you can then use a second for loop to write the array to the second set of 5 textboxes (presumably TextBox6, 7, 8, 9, 10) in much the same way the first loop finds the first 5.
    Attached Images Attached Images  
    Last edited by jdelano; Jul 25th, 2024 at 07:10 AM.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jul 2024
    Posts
    22

    Re: How to rank 5 textbox value to another 5 textbox?

    Attachment 192362

    thank you but I want to transfer 5 textbox data to another5 textbox.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jul 2024
    Posts
    22

    Re: How to rank 5 textbox value to another 5 textbox?

    Attachment 192363

    Attachment 192362

    thank you but I want to transfer 5 textbox data to another5 textbox. like the image.

  10. #10
    Hyperactive Member
    Join Date
    Jul 2022
    Posts
    353

    Re: How to rank 5 textbox value to another 5 textbox?

    Use the for loop I used to read the first 5 textboxes as an example to write to the other 5 textboxes.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jul 2024
    Posts
    22

    Re: How to rank 5 textbox value to another 5 textbox?

    Sorry to say I am new in VB. Can you help me to write the code? Please.

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,669

    Re: How to rank 5 textbox value to another 5 textbox?

    Code:
    Dim inputTextBoxes() As TextBox = {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5}
    Dim values(4) As Integer
    
    For x As Integer =0 To 4
        Dim v As Integer
        Integer.TryParse(inputTextBoxes(x).Text, v)
        values(x) = v
    Next
    
    Array.Sort(values)
    Array.Reverse(values)
    
    Dim outputTextBoxes() As TextBox = {TextBox6, TextBox7, TextBox8, TextBox9, TextBox10}
    
    For x As Integer =0 To 4
        outputTextBoxes(x).Text = values(x).ToString
    Next
    Last edited by .paul.; Jul 25th, 2024 at 01:13 PM.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jul 2024
    Posts
    22

    Re: How to rank 5 textbox value to another 5 textbox?

    Thank you so much.

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