Assigning a value to radio buttons

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SammyB
    Recognized Expert Contributor
    • Mar 2007
    • 807

    #31
    Too busy now, but check out the ListView control, Details view, I think that you can sort by any column.

    Comment

    • jpmcafee
      New Member
      • Apr 2007
      • 22

      #32
      That worked out good, It was a little difficult finding where to place the code so I hope I got it. The only thing I question is that this shows two columns I wonder if I have to do anything to accomodate a third. Alot of the code shows

      Code:
      Imports System.Collections
      Imports System.Windows.Forms
      
      Public Class ListViewColumnSorter
          Implements System.Collections.IComparer
      
          Private ColumnToSort As Integer
          Private OrderOfSort As SortOrder
          Private ObjectCompare As CaseInsensitiveComparer
      
          Public Sub New()
              ' Initialize the column to '0'.
              ColumnToSort = 0
      
              ' Initialize the sort order to 'none'.
              OrderOfSort = SortOrder.None
      
              ' Initialize the CaseInsensitiveComparer object.
              ObjectCompare = New CaseInsensitiveComparer()
          End Sub
      
          Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
              Dim compareResult As Integer
              Dim listviewX As ListViewItem
              Dim listviewY As ListViewItem
      
              ' Cast the objects to be compared to ListViewItem objects.
              listviewX = CType(x, ListViewItem)
              listviewY = CType(y, ListViewItem)
      
              ' Compare the two items.
              compareResult = ObjectCompare.Compare(listviewX.SubItems(ColumnToSort).Text, listviewY.SubItems(ColumnToSort).Text)

      Do I have to add anything for a third column to be figured in
      like

      listviewX = CType(x, ListViewItem)
      listviewY = CType(y, ListViewItem)
      listviewZ = CType(z, ListViewItem)

      ? or should I be ok......

      Comment

      • SammyB
        Recognized Expert Contributor
        • Mar 2007
        • 807

        #33
        Originally posted by jpmcafee
        That worked out good, It was a little difficult finding where to place the code so I hope I got it. The only thing I question is that this shows two columns I wonder if I have to do anything to accomodate a third. Alot of the code shows

        Code:
        Imports System.Collections
        Imports System.Windows.Forms
         
        Public Class ListViewColumnSorter
        Implements System.Collections.IComparer
         
        Private ColumnToSort As Integer
        Private OrderOfSort As SortOrder
        Private ObjectCompare As CaseInsensitiveComparer
         
        Public Sub New()
        ' Initialize the column to '0'.
        ColumnToSort = 0
         
        ' Initialize the sort order to 'none'.
        OrderOfSort = SortOrder.None
         
        ' Initialize the CaseInsensitiveComparer object.
        ObjectCompare = New CaseInsensitiveComparer()
        End Sub
         
        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
        Dim compareResult As Integer
        Dim listviewX As ListViewItem
        Dim listviewY As ListViewItem
         
        ' Cast the objects to be compared to ListViewItem objects.
        listviewX = CType(x, ListViewItem)
        listviewY = CType(y, ListViewItem)
         
        ' Compare the two items.
        compareResult = ObjectCompare.Compare(listviewX.SubItems(ColumnToSort).Text, listviewY.SubItems(ColumnToSort).Text)

        Do I have to add anything for a third column to be figured in
        like

        listviewX = CType(x, ListViewItem)
        listviewY = CType(y, ListViewItem)
        listviewZ = CType(z, ListViewItem)

        ? or should I be ok......
        I usually do the Column stuff with the form designer's Column collection, but here is a sample of adding them in code plus (more importantly) sample code to sort by column when you click on the header. It's a lot more messy than I thought: I was hoping for a SortColumn property, but this looks like an excellent example. You'll be able to apply it to your situation. http://support.microsoft.com/kb/319399

        Comment

        • jpmcafee
          New Member
          • Apr 2007
          • 22

          #34
          Originally posted by SammyB
          I usually do the Column stuff with the form designer's Column collection, but here is a sample of adding them in code plus (more importantly) sample code to sort by column when you click on the header. It's a lot more messy than I thought: I was hoping for a SortColumn property, but this looks like an excellent example. You'll be able to apply it to your situation. http://support.microsoft.com/kb/319399
          Sammy,
          Thank you that is the code I used and worked. However its giving me grief with 3 columns.... Not quite sure what to do.

          Comment

          • SammyB
            Recognized Expert Contributor
            • Mar 2007
            • 807

            #35
            Originally posted by jpmcafee
            Sammy,
            Thank you that is the code I used and worked. However its giving me grief with 3 columns.... Not quite sure what to do.
            This is what I did:
            1. Add a ListView. In it's properties change the Name to LvResults and the View to Details.
            2. Now add the columns. Again in the ListView properties, click on the ... button for the Columns properties. For each column, press Add and enter the header text. I added three: Name, Score, Tab
            3. On my Tab Pages I made the name TextBoxes TxtName1, TxtName2, etc. and the score TextBoxes TxtScore1, TxtScore2, etc.
            4. I stole the ListViewColumnS orter class without changes from http://support.microsoft.com/kb/319399
            5. I just put the Results tab on TabControl1 so there's an extra if in my outer loop:
            Code:
            Public Class Form1
            	Private cPages As New List(Of TabPage)()
            	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            		' Save each tab in cPages
            		For i As Integer = 0 To TabControl1.TabCount - 1
            			cPages.Add(TabControl1.TabPages(i)) 'Save the tab
            		Next i
            		' Remove all but First & Last to make them invisible
            		For i As Integer = TabControl1.TabCount - 2 To 1 Step -1
            			TabControl1.Controls.RemoveAt(i)
            		Next i
            	End Sub
            	Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
            		TabControl1.Controls.Add(cPages(TabControl1.TabCount - 1))
            		If TabControl1.TabCount = cPages.Count Then BtnAdd.Enabled = False
            	End Sub
            	Private Sub CB_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles A1.CheckedChanged, A1Page2.CheckedChanged
            		Dim i As Integer = 1
            		LvResults.Items.Clear()
            		For Each pg As TabPage In TabControl1.TabPages
            			If pg.Text <> "Results" Then
            				Dim sum As Integer = 0
            				Dim TbName As TextBox = Nothing, TbTotal As TextBox = Nothing
            				For Each c As Control In pg.Controls
            					If TypeOf c Is CheckBox Then
            						Dim chk As CheckBox = c
            						If chk.Checked Then
            							If chk.Name.StartsWith("A") Then
            								sum += chk.Tag
            							End If
            						End If
            					ElseIf TypeOf c Is TextBox Then
            						If c.Name.StartsWith("TxtName") Then
            							TbName = c
            						ElseIf c.Name.StartsWith("TxtTotal") Then
            							TbTotal = c
            						End If
            					End If
            				Next c
            				TbTotal.Text = sum.ToString()
            				Dim row As New ListViewItem(New String() {TbName.Text, TbTotal.Text, i.ToString()})
            				LvResults.Items.Add(row)
            				i = i + 1
            			End If
            		Next pg
            		Dim LvSorter As New ListViewColumnSorter()
            		LvSorter.Order = SortOrder.Ascending
            		LvSorter.SortColumn = 1
            		LvResults.ListViewItemSorter = LvSorter
            		LvResults.Sort()
            	End Sub
            End Class
            Now, we need to start over in an object-oriented fashon and make a good, maintaintable, two-tier solution. Are you up to it?

            Comment

            • jpmcafee
              New Member
              • Apr 2007
              • 22

              #36
              OK,
              So how should we do this. Iam up for anything, should I add this code or wait? not sure what you mean by an object orientated enviroment

              Originally posted by SammyB
              This is what I did:
              1. Add a ListView. In it's properties change the Name to LvResults and the View to Details.
              2. Now add the columns. Again in the ListView properties, click on the ... button for the Columns properties. For each column, press Add and enter the header text. I added three: Name, Score, Tab
              3. On my Tab Pages I made the name TextBoxes TxtName1, TxtName2, etc. and the score TextBoxes TxtScore1, TxtScore2, etc.
              4. I stole the ListViewColumnS orter class without changes from http://support.microsoft.com/kb/319399
              5. I just put the Results tab on TabControl1 so there's an extra if in my outer loop:
              Code:
              Public Class Form1
              	Private cPages As New List(Of TabPage)()
              	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
              		' Save each tab in cPages
              		For i As Integer = 0 To TabControl1.TabCount - 1
              			cPages.Add(TabControl1.TabPages(i)) 'Save the tab
              		Next i
              		' Remove all but First & Last to make them invisible
              		For i As Integer = TabControl1.TabCount - 2 To 1 Step -1
              			TabControl1.Controls.RemoveAt(i)
              		Next i
              	End Sub
              	Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
              		TabControl1.Controls.Add(cPages(TabControl1.TabCount - 1))
              		If TabControl1.TabCount = cPages.Count Then BtnAdd.Enabled = False
              	End Sub
              	Private Sub CB_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles A1.CheckedChanged, A1Page2.CheckedChanged
              		Dim i As Integer = 1
              		LvResults.Items.Clear()
              		For Each pg As TabPage In TabControl1.TabPages
              			If pg.Text <> "Results" Then
              				Dim sum As Integer = 0
              				Dim TbName As TextBox = Nothing, TbTotal As TextBox = Nothing
              				For Each c As Control In pg.Controls
              					If TypeOf c Is CheckBox Then
              						Dim chk As CheckBox = c
              						If chk.Checked Then
              							If chk.Name.StartsWith("A") Then
              								sum += chk.Tag
              							End If
              						End If
              					ElseIf TypeOf c Is TextBox Then
              						If c.Name.StartsWith("TxtName") Then
              							TbName = c
              						ElseIf c.Name.StartsWith("TxtTotal") Then
              							TbTotal = c
              						End If
              					End If
              				Next c
              				TbTotal.Text = sum.ToString()
              				Dim row As New ListViewItem(New String() {TbName.Text, TbTotal.Text, i.ToString()})
              				LvResults.Items.Add(row)
              				i = i + 1
              			End If
              		Next pg
              		Dim LvSorter As New ListViewColumnSorter()
              		LvSorter.Order = SortOrder.Ascending
              		LvSorter.SortColumn = 1
              		LvResults.ListViewItemSorter = LvSorter
              		LvResults.Sort()
              	End Sub
              End Class
              Now, we need to start over in an object-oriented fashon and make a good, maintaintable, two-tier solution. Are you up to it?

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #37
                Originally posted by jpmcafee
                So how should we do this. Iam up for anything, should I add this code or wait? not sure what you mean by an object orientated enviroment
                Just a note - the expression is "object-oriented", not "orientated ".

                Comment

                • SammyB
                  Recognized Expert Contributor
                  • Mar 2007
                  • 807

                  #38
                  Originally posted by Killer42
                  Just a note - the expression is "object-oriented", not "orientated ".
                  No excuse...except I was already an hour late for work and TheScripts is scriptless for a spell check button, so I got a little dis-orientatated!

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #39
                    Originally posted by SammyB
                    No excuse...except I was already an hour late for work and TheScripts is scriptless for a spell check button, so I got a little dis-orientatated!
                    It wasn't you who got it wrong, silly.

                    Comment

                    • SammyB
                      Recognized Expert Contributor
                      • Mar 2007
                      • 807

                      #40
                      Originally posted by Killer42
                      It wasn't you who got it wrong, silly.
                      Gee, should I have read the thread? Guess I need reorientation.

                      Comment

                      • SammyB
                        Recognized Expert Contributor
                        • Mar 2007
                        • 807

                        #41
                        Originally posted by jpmcafee
                        OK,
                        So how should we do this. Iam up for anything, should I add this code or wait? not sure what you mean by an object orientated enviroment
                        Oh, definitely make the few changes that you need to your code: it's always wonderful to have something that works! Plus, you'll learn about the ListView: it's my favorite control.

                        Now, object oriented: look at wikipedia, http://en.wikipedia.org/wiki/Object-...ed_programming. In our case, what objects do we have? We have a suspect! What properties does this suspect have? A name, a score, a rank, and a list of answers to questions. So, we should start with this object. Let's just start over with a new Windows Application. Just put the add button on the new form and we will use that to create our suspect objects.

                        To create an object, we need to create a class. The class is like a cookie-cutter and the object is like a cookie. So, after adding the button, use the Project, Add class menu to create a class called Suspect.vb.

                        Now, read the help tutorial about creating a class (Help, Index, classes [Visual Basic], about classes) (or on-line at http://msdn2.microsoft.com/en-us/lib...01(VS.80).aspx) and then see if you can create some code for the Suspect class. Do not worry about the user interface at this point, we'll do that later and auto-magically attach it to the class.

                        But, I do have questions: (1) what is the rank; and (2) for a particular question is there one one answer?

                        Comment

                        • jpmcafee
                          New Member
                          • Apr 2007
                          • 22

                          #42
                          OK Rank I think is what you are refering to is in order from highest to lowest based on the numerical score.
                          The questions have 2 answers minimum some have 3 or so.

                          Comment

                          Working...