I'm writing a routine where the user can populate up to 8 textboxes with numbers. Since there is potential for these numbers to be unsorted, I want to sort these prior to saving. However it's probable that all of the textboxes will not be used. My challenge then is how to ignore the blank textboxes and not include them in the sort.

In my test, I've used 4 of the 8 boxes. It sorts perfectly - except it puts the blank textboxes first.

I've tried various versions of the following with no success. Are there any thoughts as to how to fix this?

        Dim IntArr(7) As String
        If tbxProp1.Text <> "" Then
            IntArr(0) = tbxProp1.Text
        End If
        If tbxProp2.Text <> "" Then
            IntArr(1) = tbxProp2.Text
        End If
        If tbxProp3.Text <> "" Then
            IntArr(2) = tbxProp3.Text
        End If
        If tbxProp4.Text <> "" Then
            IntArr(3) = tbxProp4.Text
        End If
        If tbxProp5.Text <> "" Then
            IntArr(4) = tbxProp5.Text
        End If
        If tbxProp6.Text <> "" Then
            IntArr(5) = tbxProp6.Text
        End If
        If tbxProp7.Text <> "" Then
            IntArr(6) = tbxProp7.Text
        End If
        If tbxProp8.Text <> "" Then
            IntArr(7) = tbxProp8.Text
        End If
        Array.Sort(IntArr)
        tbxProp1.Text = IntArr(0)
        tbxProp2.Text = IntArr(1)
        tbxProp3.Text = IntArr(2)
        tbxProp4.Text = IntArr(3)
        tbxProp5.Text = IntArr(4)
        tbxProp6.Text = IntArr(5)
        tbxProp7.Text = IntArr(6)
        tbxProp8.Text = IntArr(7)

In advance, thanks for your assistance.

Don

take the array you declared i.e IntArr() as a dynamic array and do you jobs.
The codes are as like

 'declare the variables
        Dim IntArr() As String
        Dim i As Integer = 0

        'Redimentioning the array
        ReDim IntArr(i)

        For Each tb As Control In Me.Controls
            If TypeOf tb Is TextBox Then
                If Not String.IsNullOrWhiteSpace(tb.Text) Then
                    'redimentioning the array and store the data
                    ReDim Preserve IntArr(i)
                    IntArr(i) = tb.Text
                    i = i + 1
                End If
            End If
        Next

        'Trying to sort the array
        Array.Sort(IntArr)


        'Now time to display data which are stored in array
        i = 0
        Do
            For Each tb As Control In Me.Controls
                If TypeOf tb Is TextBox Then
                    tb.Text = ""
                    If tb.Name = "tbxProp" & (i + 1).ToString Then
                        tb.Text = IntArr(i)
                        Exit For
                    End If
                End If
            Next
            i += 1
        Loop Until i > IntArr.GetUpperBound(0)

I think it can help you.

You are assigning either text to your array elements or nothing, that means if a textbox1.text="" then this element IntArr(0)="". So your array might look like this: "",3,7,"" etc. For sure if you return these elements there will be "" or 0 in it. So what you can do is remove all the empty array entries with removeat or you dont add these to your array, which means you don't assign the textbox.text directly to the array items but add these to the array. Also I can't see where you change the text to numbers, e.g. you are sorting text?

I agree with @Sanu

@Mr.M
Your comment is useless as Sanu also tries to sort text instead of numbers.
@ Sanu
As the OP stated he wants to sort numbers.
These few lines of code will do all this:

Private Sub Sort_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim numArr As New List(Of Integer) 'assuming integers
        'here we add the textbox elements if they are not empty
        For Each txt As Control In Me.Controls
            If txt.GetType Is GetType(TextBox) Then
                If Len(txt.Text) > 0 Then
                    numArr.Add(CInt(txt.Text)) ' assuming integers
                End If
            End If
        Next
        'here we sort these
        numArr.Sort()
        'here we check that all went well
        For i = 0 To numArr.Count - 1
            Debug.Print(numArr(i).ToString)
        Next
    End Sub

@Minimalist:

My challenge then is how to ignore the blank textboxes and not include them in the sort.

In my test, I've used 4 of the 8 boxes. It sorts perfectly - except it puts the blank textboxes first.

If you take a numerical datatype Array, for a blank element it returns 0. In sort process there is no problem to declare it as numerical or string type, what ever may be.

@Shark1
I don't understan your post. From my code you can see that I only add text from the textboxes with length > 0 so there are no blank elements in my list. I also change the type to Integer(well the OP didn't say what type of numbers he wants to sort. O.K. to check the validity of my code just make up 8 textboxes and add elements at form load like this:

TextBox1.Text = "12"
        TextBox2.Text = "1"
        TextBox3.Text = "2"
        TextBox4.Text = ""
        TextBox5.Text = "125"
        TextBox6.Text = "12"
        TextBox7.Text = ""
        TextBox8.Text = "121"

Copy my other code in a button click event and run. The list only contains non empty entries and the sort is done on Intger values.

@Minimalist:
I am not critisizing your codes. Yours is nodoubt well. You are a good programmer in vb.net.
So I give you a vote.

In my line 6 I should have checked that there are no spaces in a textbox via the spacebar and it would be better to trim the text as in:

   If Len(Trim(txt.Text)) > 0 Then 'line 6

Otherwise thanks for the vote.

Minimalist, I have a few questions for you (I'm learning and want to understand):

I'm guessing that -

        If txt.GetType Is GetType(TextBox) Then
                If Len(txt.Text) > 0 Then
                numArr.Add(CInt(txt.Text)) ' assuming integers
        End If

looks at each textbox, checks to see if there is something in it, if there is it THEN adds the number to the array (after it converts it to an integer). Am I correct?

I presume then the statement before -

For Each txt As Control In Me.Controls

is the loop that looks at each of my 8 textboxes. Yes?

I'm not sure how this works here:

            For i = 0 To numArr.Count - 1
                Debug.Print(numArr(i).ToString)
            Next

Truthfully I'm not sure what you're actually doing here. Can you explain further?

Thanks for the help.

Don

Rather than checking the text length you should check if the textbox contains a number. This wouuld not require trimming and would also ignore non-numeric entries such as "abc". The following code should be reasonably self evident.

Dim nums As New ArrayList

For Each txt As TextBox In Me.Controls.OfType(Of TextBox)()
    If IsNumeric(txt.Text) Then
        nums.Add(CInt(txt.Text))
    End If
Next

nums.Sort()

If the form contains other text boxes you could always put the related boxes in a container and do the for loop on that container.

The code looks at each control in the form and if finds a textbook it executes the code.
So if you only have the 8 textboxes you don't need to address these in paricular, othrewise if you have other textboxes as well you have to address only the ones you want, but if the others would contain also letters besides numbers you are also o.k.
Error checking is always a must.

This code prints the sorted numbers to the debug.window and allows this way a quick check if everything worked as expected.

For i = 0 To numArr.Count - 1
                Debug.Print(numArr(i).ToString)
 Next

Thank you, Minimalist and Rev. Jim. I'm glad I asked the question. This was great learning.

Don

You are welcome.

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.