Sorting inputs in Textbox

  • 13 years ago
    Greetings everyone! I'm currently having issues with using the sort function as it is very new to me. Currently I've created 3 textboxes in which I can enter a single letter such B, C, K in each. When I push a button that I've created it will sort the contents of the 3 textboxes in order. Such as if they enter C,B,K and press the button which should activate the click event procedure. Textbox 1 will have B appear in it, while Textbox 2 will have C appear in it, and TextBox 3 will have K appear in it. I'm thinking that my problem lies in not being able to call a general sub procedure that accepts three strings and sorts them in ascending order correctly. I'm trying to learn the basics in hoping in the future to use this technique sort out customer names or titles of books and such. Below is the code I've been working on. If anyone could offer some advice I'd be very greatful. Thank you for your time. Have a wonderful day!

    Public Class Form1
        Dim A As String
        Dim B As String
        Dim C As String
        Dim Letters() As String = {A, B, C}

        Private Sub btnOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOrder.Click
            A = txt1.Text
            B = txt2.Text
            C = txt3.Text
        End Sub
    End Class

  • 13 years ago

    Strings aren't reference types.  It appears you've tried to use them in this manner.  When you create the array what has happened is 3 values (whatever A, B, C represent at that time) are added to the array.  After that the array and the variables are seperate.  If A changes the array will not reflect this.  In your case A, B, and C have no value when you created the array, so you ended up with an array of 3 null values. 

    In addition you will need to output the values again so the textboxes reflect the correct sorted values.  In your code you sorted the array but never displayed the result.

    Also your variables were declared globally.  From what I see this is unecessary as the variables are only used in the one procedure.  It's good practice to declare variables with as limited scope as possible.

    A, B, and C aren't necessary.  Although if you think it makes your code clearer by all means use them.  But you can create your array like this.  Dim Letters() as string = {txt1.text, txt2.text, txt3.text}

    If you choose to use A, B, and C make sure they have been assigned the correct values before creating the array.

    I think you should have enough to go on now.  Give it another shot.  If you have any more trouble feel free to ask.

  • 13 years ago

    Below is the code I've been working on. I'm trying to enable click event procedure to be able to  call a general (sub) procedure that accepts three strings and sorts them in ascending order. However it doesn't seem to work currently. I think the outline of procedure makes sense but I'm sure I'm not implementing things correct. Can anyone help me with this? thank you

    Public Class Form1

    Public Sub Order(ByVal Box1 As String, ByVal Box2 As String, ByVal Box3 As String)

    Dim chrText(3) As Char

    chrText(1) = Box1

    chrText(2) = Box2

    chrText(3) = Box3


    Box1 = chrText(1)

    Box2 = chrText(2)

    Box3 = chrText(3)

    End Sub


    Private Sub btnOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOrder.Click

    Dim a As String = txt1.Text

    Dim b As String = txt2.Text

    Dim c As String = txt3.Text

    Order(a, b, c)

    End Sub


  • 13 years ago

    The only problem you've got is your not outputing the result.  The textboxes need to be assigned new values once the sort is done.  Your procedure accepts 'strings'.  Setting Box1, Box2, Box3 to the new values won't update the actual textboxes on the form as these are just 'string' variables.  There is no connection between them and the textboxes on the form.

    You could accomplish what you want two ways.
    1) Pass the textboxes as parameters you want to sort
    2) Create a function that returns the sorted values and use the result to popullate the textboxes

    With the first option your routine would accept textboxes.  So you would call it like this: Order(txt1,txt2,txt3).  You want to pass the actual textbox not just the text.  Your routine would then popullate the array using the text from each of the boxes.  It would then sort the array.  Once the sort is complete you can set the text in each textbox to represent the change.

    With the second option you would pass the strings like you've got now.  However instead of a sub you would have a function.  The function would return an array of strings.  So once you sort the array you can just return it.  The routine that calls the function would get the array returned and then add the values to the appropriate textboxes.

    Also ALL arrays are zero based.  When you create an array like you have "chrText(3)" you've actually created an array with 4 elements: chrText(0), chrText(1), chrText(2), chrText(3).  Although you can do what you've done I think generally you would just want to create an array with only 3 elements.

    Hopefully that clears things up and will help you finish your routine.  I don't mean to be a pain but I don't want to just give you the code as you seem to be doing this as a learning experience.  I've given you an outline of what should work.  If I was clear you should be able to do this now.  More importantly I hope you understand what you've done wrong.  If you don't understand something please let me know and I'll try to clarify.  Or if you prefer I can give you an example of how it could be done.  Sometimes an example is better then words.


    Now that I think about it you could create a routine that accepts strings and would update the textboxes.  You would have to use ByRef instead of ByVal however and you would need to pass txt1.Text directly, you couldn't assign it to 'A' and then pass 'A'.  To demonstate this all you would need to do is change the 3 ByVals you have in your procedure to ByRef.  Then call the routine like this: Order(txt1.text,txt2.text,txt3.text).  This should work without any other changes.  I'm not sure I like that solution.  I think I would rather accept textboxes instead if you were going to do it that way, but that's your call.

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“I invented the term Object-Oriented, and I can tell you I did not have C++ in mind.” - Alan Kay