Wednesday, July 31, 2013

Sort TextBox by position in VB

Sort TextBox by position in VB

Sort TextBox in order by position inside a Winform is not a build in function inside Visual Basic or inside Visual Studio. For a long time, I had to place them in order inside my Winform or renumber the TabIndex properties once my form is completed. It is a pain in the ass. So I decided to build a small function for everyone who reads this.
I haven’t made a code to complicate. The code is good enough for a normal amount of TextBox inside a form. If your form contains over 100 TextBox, your might change the type of sorting for a faster algorithm.

Download the Code Project

Here is a sample project for this article. I use Framework .NET 4.0 and Microsoft Visual Studio 2010 to build this project. Here is a video:

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    End Sub

    ''' <summary>
    ''' Description: This Function helps reorder every TextBox inside a Form.
    ''' TabIndex will be changed base on the position inside the form.
    ''' That way, when a user press TAB on his keyboard,
    ''' the cursor will go to the next TextBox on the left or in the buttom.
    ''' This function is very simple and fast enough for small amount of TextBox.
    ''' Date: July 31st 2013
    ''' Author : Check-Kay Wong
    ''' </summary>
    ''' <remarks>You could make this function better if it handles other types of Windows.Forms.Controls such as ListBox or ComboBox</remarks>
    Private Sub Sort_TextBox()
        Dim index1 As Integer
        Dim index2 As Integer
        Dim index3 As Integer
        Dim ctr As Windows.Forms.Control
        Dim oType As System.Type
        Dim TextBox_temp As Windows.Forms.TextBox
        Dim myCollection_Object1 As Windows.Forms.TextBox
        Dim myCollection As List(Of TextBox)
        Dim item_to_Sort As Boolean

        myCollection = New List(Of TextBox)
        For index1 = 0 To Me.Controls.Count - 1
            ctr = Me.Controls.Item(index1)
            oType = ctr.GetType

            'TextBox.Text = TextBox.Name
            item_to_Sort = False
            If oType.Name = "TextBox" Then
                TextBox_temp = CType(ctr, Windows.Forms.TextBox)
                TextBox_temp.Text = TextBox_temp.Name
                item_to_Sort = True
            End If

            ' 2 dimensions sorting
            If item_to_Sort = True Then
                If myCollection.Count = 0 Then
                    'the first item for an empty collection
                    'for all other items, please insert inside the myCollection
                    'is a 1 dimension array
                    'some people could see it as a 2D sort
                    index2 = 0
                    index3 = myCollection.Count
                        myCollection_Object1 = myCollection.Item(index2)
                        If myCollection_Object1.Top > TextBox_temp.Top Then
                            myCollection.Insert(index2, TextBox_temp)
                            Exit Do
                        ElseIf myCollection_Object1.Top = TextBox_temp.Top Then
                            If myCollection_Object1.Left > TextBox_temp.Left Then
                                myCollection.Insert(index2, TextBox_temp)
                                Exit Do
                            ElseIf myCollection_Object1.Left < TextBox_temp.Left Then
                                index2 = index2 + 1
                            ElseIf myCollection_Object1.Left = TextBox_temp.Left Then
                                '2 textbox with same top value en same left value....
                                myCollection.Insert(index2, TextBox_temp)
                                Exit Do
                            End If
                        ElseIf myCollection_Object1.Top < TextBox_temp.Top Then
                            index2 = index2 + 1
                        End If
                        'index2 = index2 + 1
                    Loop Until index2 >= myCollection.Count
                    'place item at the end if can't plat it between any item
                    If index3 = myCollection.Count Then
                        myCollection.Insert(index2, TextBox_temp)
                    End If
                End If
            End If

        ' new sort, change TabIndex
        For index1 = 0 To myCollection.Count - 1
            myCollection_Object1 = myCollection.Item(index1)
            myCollection_Object1.TabIndex = index1

    End Sub

End Class

Download the project here:
Buy Microsoft Visual Studio Pro 2012 from Amazon web site.

No comments:

Post a Comment