VS 2015 Sorting Text boxes on Form based on Value.-VBForums
Results 1 to 11 of 11

Thread: Sorting Text boxes on Form based on Value.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    80

    Sorting Text boxes on Form based on Value.

    I have a button, what I need it to do is sort all the text boxes based on the Value input. For example.

    Name:  Capture.PNG
Views: 86
Size:  137.9 KB

    Based on this, I need the button to sort the Init Value from Highest to lowest. While it does that it will need to move the corresponding Name with it. So it would order "Blink Dog 20, Azer 17, etc." Then I need to reset it all back to default with the reset button.

    I am currently getting the value from a dataset + random roll. Here is the code for that. I have 10 different instances of this. So all the 1, is 2 and 3, etc etc.

    Boxes are moninit1.text 2, 3, etc (This is for Init that needs to be sorted)... and n1.text 2, 3, etc...(This is for the monster name that needs to move with it.

    Code:
    Private Sub importmob1_CheckedChanged(sender As Object, e As EventArgs) Handles importmob1.CheckedChanged
            'imports selected monster into import table
            If importmob1.Checked = True Then n1.Text = Mob1.Text
            If importmob1.Checked = False Then n1.Text = ""
            Try
                'rolls a random 20 sided dice and adds modifiers from amod and dmmod
                'TODO - On Error must be replaced with Try, Catch, Finally
                Dim initotal As Integer
                Dim rannum As Integer
                rannum = Int((20 * Rnd()) + 1)
                initotal = moninit1.Text
    
                init1.Text = rannum + initotal
                If importmob1.Checked = False Then init1.Text = ""
            Catch ex As Exception
                MsgBox("Make Sure you selected a monster", MsgBoxStyle.Critical, "Error in Application " & Me.Name)
            End Try
        End Sub

    Thanks in Advance!

    -Abe

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    80

    Re: Sorting Text boxes on Form based on Value.

    I have been playing around trying to figure this out and tried an Array. (New to Arrays) I am not getting an error but it also does not do anything. Here is what I tried.

    Code:
     Private monClasses(9) As String
    
      Private Sub Order_Click(sender As Object, e As EventArgs) Handles Order.Click
            Dim listArray As New ArrayList
            monClasses(0) = moninit1.Text
            monClasses(1) = moninit2.Text
            monClasses(2) = moninit3.Text
            monClasses(3) = moninit4.Text
            monClasses(4) = moninit5.Text
            monClasses(5) = moninit6.Text
            monClasses(6) = moninit7.Text
            monClasses(7) = moninit8.Text
            monClasses(8) = moninit9.Text
            monClasses(9) = moninit10.Text
    
            Array.Sort(monClasses)
    
            For num As Integer = 0 To monClasses.Length - 1
                listArray.Add(monClasses(num))
            Next
    
        End Sub
    End Class

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    29,450

    Re: Sorting Text boxes on Form based on Value.

    What I would do would be to create a small class (you could even use a structure, but I'd still use a class) that had members for Name and Init, and even that checkbox, if that's relevant. I'd then have a List of that class. The class might look as simple as this:
    Code:
    Public Class Monster
     Public Property Name As String
     Public Property InitValue As Integer
    End Class
    I'd then implement the IComparable interface for the class (I might have the wrong one, but I think that's it). You'd do that by adding a line below the Public Class line like:

    Implements IComparable(of Monster)

    Once you press enter, you'll get a stub of a method that you need to complete. This can be quite complicated, because what this stub is doing is saying, "given a new object of type Monster, should this one sort higher or lower?" That's a question that can have quite complicated answers, but in this case, it's dead simple, because all you'd be sorting on is the .InitValue property, so it would just be:

    Code:
    Public Function CompareTo(other As Monster) As Integer Implements System.IComparable(Of Monster).CompareTo
            Return InitValue.CompareTo(other.InitValue)
    End Function
    Now, with a List(of Monster), to sort it, all you'd have to do is call the .Sort method on the list and the list will be sorted by InitValue. So, all that remains is to refill the textboxes with the values from the objects in the list. There may be a way to do this automatically by binding them, but I've never actually tried it, so I'm not sure whether it would work, and especially whether it is worth it. After all, filling the textboxes manually would be as simple as looping from 0 to yourList.Count-1 and filling each textbox with the values from the object.

    In other words, rather than sorting textboxes, you'd be sorting the contents of the textboxes.

    By the way, you'd be better off using a NumericUpDown control for the Init value rather than a textbox. Textboxes ONLY contain strings. If you aren't explicitly converting the string to an integer using something like Integer.TryParse, then....that would be liable to cause trouble, eventually. With the NumericUpDown control, the .Value property will be type Decimal, and ONLY numbers can be entered into it. You can use CInt() to convert the .Value from Decimal to Integer, and it will work every time.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    80

    Re: Sorting Text boxes on Form based on Value.

    Thanks for this Shaggy, I will give it a shot and report back.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    80

    Re: Sorting Text boxes on Form based on Value.

    So here is what I have. I am a little lost on what to put in place of the .text

    Code:
    Public Class Monster
            Implements IComparable(Of Monster)
            Public Property name As String
            Public Property InitValue As Integer
    Code:
    Public Function CompareTo(other As Monster) As Integer Implements System.IComparable(Of Monster).CompareTo
                Return InitValue.CompareTo(other.InitValue)
            End Function
    Code:
    Private Sub Order_Click(sender As Object, e As EventArgs) Handles Order.Click
    
                Dim monClasses(10) As Array
                monClasses(0) = moninit1.Text
                monClasses(1) = moninit2.Text
                monClasses(2) = moninit3.Text
                monClasses(3) = moninit4.Text
                monClasses(4) = moninit5.Text
                monClasses(5) = moninit6.Text
                monClasses(6) = moninit7.Text
                monClasses(7) = moninit8.Text
                monClasses(8) = moninit9.Text
                monClasses(9) = moninit10.Text
    
                Array.Sort(monClasses)
            End Sub
        End Class

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    29,450

    Re: Sorting Text boxes on Form based on Value.

    That Order_Click method doesn't belong in the class. It belongs on the form (you may have it there, depends on which class that End Class refers to), and it isn't quite right. If there will ALWAYS be just 10 monsters, then you can use an array of monster. If there could be some other number, then a List(of Monster) would be better. Arrays are good if the number of elements never changes, lists are the thing to use if the number of elements can vary.

    You'd have to create the monsters for the list. If you know the names for them, you could do something like this:

    Step 1: Move monClasses out of the method and put it at form scope:
    Code:
    Private monClasses(10) as Monster
    
    Step 2:
    'This could be in the Load event, or the contructor.
    'This creates a Monster class for each slot in the array, otherwise the array is like an empty egg carton. This is putting the eggs in the carton.
    For x = 0 to 9
     monClasses(x) = New Monster
    Next
    
    'Step 3:
    'This could also be in the Load event following that first one.
    
    monClasses(0).Name = "Anime Armor"
    monClasses(1).Name = "Andogenous Sphinx"
    'etc...
    What that does is sets up the array of monsters. It creates a Monster for each slot in the array, then sets the names for all the monsters. You could then have a method to load the textboxes, which I can't fully show:

    Code:
    Public Sub DisplayArray()
     Me.firstMonsterNameBox.Text = monClasses(0).Name
     Me.firstMonsterValueBox.Text = monClasses(0).InitValue.ToString
     Me.secondMonsterNameBox.Text = monClasses(1).Name
     Me.secondMonsterValueBox.Text = monClasses(1).InitValue.ToString
     'etc...
    There may be a way to replace that with binding, I just haven't tried it. The concept is clear enough. The DisplayArray method loads the data from the array into the controls. You'd call that method in the last line of the load event, after the other stuff, but now your Order_Click becomes:
    Code:
    Array.Sort(monClasses)
    DisplayArray()
    The first line sorts the array, the second line re-populates the controls with the new array order.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    80

    Re: Sorting Text boxes on Form based on Value.

    Thank you again for taking the time.

    So that would be an issue, let me clarify what exactly I am trying to do.

    There are 1,000 + Monsters. Each of them has a static modifier from a random roll, so I use
    Code:
    Try
                'rolls a random 20 sided dice and adds modifiers from amod and dmmod
                'TODO - On Error must be replaced with Try, Catch, Finally
                Dim initotal As Integer
                Dim rannum As Integer
                rannum = Int((20 * Rnd()) + 1)
                initotal = moninit1.Text
    
                init1.Text = rannum + initotal
                If importmob1.Checked = False Then init1.Text = ""
            Catch ex As Exception
                MsgBox("Make Sure you selected a monster", MsgBoxStyle.Critical, "Error in Application " & Me.Name)
            End Try
    There can be up-to 10 monsters. Could be 1 could be 8. Each of their rolls will be random as it is a dice roll. So the Value of initative changes every time. What I need to do is sort the initiative Highest > Lowest. The the monster name needs to move with it. I do not want to sort by monster at all as you can select any of the 1,000+ Monsters.

    Hope this clarifies a little better?

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    29,450

    Re: Sorting Text boxes on Form based on Value.

    Yeah, and it does sound better, too. 10 monsters is just a start.

    The design is still ok, though crude. From what you describe, I would still be creating a class for Monster, but I would also expect that it would have several more properties than just the ones shown. That may be going further than you want to at this time, it's just the way I'd do it.

    If I were to assume that at whatever time, you randomly select ten monsters from the set of monsters. Those ten monsters would each get a random initiative, then they'd be sorted as shown, and the DisplayArray method would also work as shown. What would really be different is that there'd be that master list of 1000 monsters, and the array would be populated by selecting 10 at random. So, it's the stuff that I was saying would happen in Load that isn't right.

    And I kind of think that I might be pushing you towards a certain design, which wouldn't be right. I would strongly suggest that you use a class for individual monsters, as that's pretty much what object oriented programming is all about. All examples use things like fruit, or animals as classes, and I've seen some that say, "you'll never have an object like this in a real programming problem, but this is just an example." However, in games and simulations, you DO have objects that are quite like that, so the example would be a good one.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    80

    Re: Sorting Text boxes on Form based on Value.

    I will play around and see what I can come up with based on your suggestions. Will post once I figure it out. Thanks again.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    80

    Re: Sorting Text boxes on Form based on Value.

    Also if it helps clarify monsterworkbench.com is the app if you wanted to see it actually function. (In its current state)

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    80

    Re: Sorting Text boxes on Form based on Value.

    So I have been reading up on Arrays and searching the inter-webs for a solution and and still coming up dry. It looks like you need to declare an Array, but all my values are random rolls and user selected from a massive monster list. I will try to explain it with this image.
    Name:  goblin.jpg
Views: 16
Size:  46.4 KB

    Here are the steps the user takes.

    Select 1 of 1,000 monster from the combo box. (Under the first column Monster) I chose Goblin & Goblin Boss

    The HP & AC auto fill based on monster selection.

    When your check box = True under the monster image Icon on the far right, it takes the monster select from combo box and adds it to the Name Field. It also rolls a random number from 1-20 + the modifier from the monster stat. (I have that hidden on load).

    So now you have a Goblin Boss and Goblin listed. What I need done, is to press the sort button, and have the Init Field order from top to bottom. So Goblin would go first and Goblin Boss would go Second. (Which is the current order in the example.)

    Here is where it gets tricky, Any of the 10 Name text boxes and 10 Init text boxes can be any value at any time. Sometimes there will only be 2 monsters, sometimes there will be 10. The numbers in the init field will also be random at all times.

    So the end goal here is to sort it based on highest number init, and have the Name section move with it.

    Combobox = monstername1.text, 2, 3 etc
    Init Textboxes = moninit1.text, 2, 3 etc.
    Name Textboxes = n1.text 2, 3 etc.

    I hope this clarifies my very confusing question. Not sure where I should go from here.

    Thanks again for all your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.