I need to sort three numbers that are entered in 10 different textboxes and display these numbers ascending and when I press a different button display them descending. Now I think that I need to put these three numbers into an array, sort and display, but I'm not really too proficient in code to accomplish this. Is there a way to do this without putting them into an Array. Here is my code so far? Which is not much of anything so far. Any guidance would be greatly appreciated, Thanks in advance.

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click



        Dim str1 As String = TextBox1.Text
        Dim str2 As String = TextBox2.Text
        Dim str3 As String = TextBox3.Text
        Dim str4 As String = TextBox4.Text
        Dim str5 As String = TextBox5.Text
        Dim str6 As String = TextBox6.Text
        Dim str7 As String = TextBox7.Text
        Dim str8 As String = TextBox8.Text
        Dim str9 As String = TextBox9.Text
        Dim str10 As String = TextBox10.Text


        RichTextBox1.Text = ""

        Dim db1 As Double
        Dim db2 As Double
        Dim db3 As Double
        Dim db4 As Double
        Dim db5 As Double
        Dim db6 As Double
        Dim db7 As Double
        Dim db8 As Double
        Dim db9 As Double
        Dim db10 As Double




        If IsNumeric(db1) Then

            db1 = Double.Parse(str1)
        ElseIf IsNumeric(db2) Then
            db2 = Double.Parse(str2)
        ElseIf IsNumeric(db3) Then
            db3 = Double.Parse(str3)
        ElseIf IsNumeric(db4) Then
            db4 = Double.Parse(str4)
        ElseIf IsNumeric(db5) Then
            db5 = Double.Parse(str5)
        ElseIf IsNumeric(db6) Then
            db6 = Double.Parse(str6)
        ElseIf IsNumeric(db7) Then
            db4 = Double.Parse(str7)
        ElseIf IsNumeric(db8) Then
            db5 = Double.Parse(str8)
        ElseIf IsNumeric(db9) Then
            db6 = Double.Parse(str9)
        ElseIf IsNumeric(db10) Then
            db6 = Double.Parse(str10)
        End If



        RichTextBox1.Text += "Before Sorting" + vbCrLf

        RichTextBox1.Text += str1 & " "
        RichTextBox1.Text += str2 & " "
        RichTextBox1.Text += str3 & " "
        RichTextBox1.Text += str4 & " "
        RichTextBox1.Text += str5 & " "
        RichTextBox1.Text += str6 & " "
        RichTextBox1.Text += str7 & " "
        RichTextBox1.Text += str8 & " "
        RichTextBox1.Text += str9 & " "
        RichTextBox1.Text += str10 & " "


        RichTextBox1.Text += vbCrLf


        RichTextBox1.Text += "After Sorting" + vbCrLf

Recommended Answers

All 4 Replies

There are great many option to accomplish this. One way, that doesn't require explicitly creating an extra array, is to use the Lines property of the RichTextBox, which is a string array already. The RichTextBox seems to like adding extra blank lines at the end, so before sorting, you'll need to remove them:

RichTextBox1.Lines = RichTextBox1.Lines.Where(Function(x) IsNumeric(x)).ToArray

To sort the lines:

RichTextBox1.Lines = RichTextBox1.Lines.OrderBy(Function(x) Integer.Parse(x)).ToArray

To reverse the existing sort:

RichTextBox1.Lines = RichTextBox1.Lines.Reverse.ToArray

where will i find the line properties in the richtextbox?
im sorry, im new in vb.net

this is my design. there are 10 textboxes and 1 richtextbox. when you click button, numbers that you input will show and then on the next line it will sort the numbers. how will i do that?

The lines property will be in the list of properties that show up when you type the control name(RichTextBox1) then add a . to it.

To add the sorted numbers to the existing numbers you could use the Concat extension:

RichTextBox1.Lines = RichTextBox1.Lines.Concat(RichTextBox1.Lines.OrderBy(Function(x) Integer.Parse(x))).ToArray
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.