Too busy now, but check out the ListView control, Details view, I think that you can sort by any column.
Assigning a value to radio buttons
Collapse
X
-
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
-
Originally posted by jpmcafeeThat 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
-
Originally posted by SammyBI 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
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
-
Originally posted by jpmcafeeSammy,
Thank you that is the code I used and worked. However its giving me grief with 3 columns.... Not quite sure what to do.- Add a ListView. In it's properties change the Name to LvResults and the View to Details.
- 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
- On my Tab Pages I made the name TextBoxes TxtName1, TxtName2, etc. and the score TextBoxes TxtScore1, TxtScore2, etc.
- I stole the ListViewColumnS orter class without changes from http://support.microsoft.com/kb/319399
- 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
Comment
-
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 SammyBThis is what I did:- Add a ListView. In it's properties change the Name to LvResults and the View to Details.
- 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
- On my Tab Pages I made the name TextBoxes TxtName1, TxtName2, etc. and the score TextBoxes TxtScore1, TxtScore2, etc.
- I stole the ListViewColumnS orter class without changes from http://support.microsoft.com/kb/319399
- 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
Comment
-
Originally posted by Killer42Just a note - the expression is "object-oriented", not "orientated ".Comment
-
Originally posted by jpmcafeeOK,
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
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
Comment