Solved

checkbox in 4,5,6 column of listview

Posted on 2014-11-25
18
206 Views
Last Modified: 2014-11-27
Just wonder how to put checkboxes in 4,5,6 column of a listview
0
Comment
Question by:Jason
  • 13
  • 5
18 Comments
 
LVL 32

Expert Comment

by:it_saige
ID: 40464724
To be honest, it might be easier to use a custom control.  Here is a good one:

http://www.codeproject.com/Articles/16009/A-Much-Easier-to-Use-ListView

-saige-
0
 

Author Comment

by:Jason
ID: 40466487
For some reason it error
can you help????
0
 

Author Comment

by:Jason
ID: 40466489
sorry see attachment
Error.png
0
 

Author Comment

by:Jason
ID: 40466506
This is my code

        Dim item1 As New ListViewItem("1")
        item1.SubItems.Add("2.20")
        Dim item2 As New ListViewItem("7")
        item2.SubItems.Add("4.40")
        Dim item3 As New ListViewItem("8")
        item3.SubItems.Add("6.20")
        Dim item4 As New ListViewItem("9")
        item4.SubItems.Add("7.40")
        Dim item5 As New ListViewItem("6")
        item5.SubItems.Add("10.80")
        Dim item6 As New ListViewItem("5")
        item6.SubItems.Add("26.80")
        Dim item7 As New ListViewItem("3")
        item7.SubItems.Add("33.80")
        Dim item8 As New ListViewItem("2")
        item8.SubItems.Add("34.80")
        Dim item9 As New ListViewItem("4")
        item9.SubItems.Add("74.30")

        ObjectListView1.Items.AddRange(New ListViewItem() {item1, item2, item3, item4, item5, item6, item7, item8, item9})

Open in new window

0
 

Author Comment

by:Jason
ID: 40466609
Worked out this part to add single item
        Dim LvItm As New RunnersData With {.RunnerNum = "1", .WinOdds = "2.20"}
        Dim LvLst As New List(Of RunnersData)
        LvLst.Add(LvItm)
        ObjectListView1.SetObjects(LvLst)

Open in new window

But I need to add multiple item

then convert data from the columns first column to intarray:
       

For Each objItem As ListViewItem In ObjectListView1.Items
MyStringArray.Add(Convert.ToInt32(objItem.SubItems(0).Text))
FieldCount += 1
Next

Open in new window

0
 
LVL 32

Expert Comment

by:it_saige
ID: 40466764
Ok.  So where do you stand as of now?  I see that you create an instance of an Object called RunnersData.  Could you post the code for this class.

-saige-
0
 

Author Comment

by:Jason
ID: 40467855
haven't worked out how to add multiple items to objectlistview.

then i need to collect entries in first and convet to intarray

For Each objItem As ListViewItem In ListView1.Items
MyintArray.Add(Convert.ToInt32(objItem.SubItems(0).Text))
FieldCount += 1
Next

Imports BrightIdeasSoftware

Public Class RunnersData
    Public Property RunnerNum As String
    Public Property WinOdds As String
End Class

Open in new window

0
 

Author Comment

by:Jason
ID: 40468411
I ended up doing this
        Dim LvLst As New List(Of RunnersData)
        Dim LvItm As New RunnersData With {.RunnerNum = "1", .WinOdds = "2.20"}
        LvLst.Add(LvItm)
        Dim LvItm1 As New RunnersData With {.RunnerNum = "7", .WinOdds = "4.40"}
        LvLst.Add(LvItm1)
        ObjectListView1.SetObjects(LvLst)

Open in new window


Is there a better way
0
 

Author Comment

by:Jason
ID: 40468786
Hey Saige

Really confused with this control
Finding it hard to even simple things like
gettlng subitemcheckbox functionality
Clearing objectlistview
Not strong on C#

Is there any other custom listview controls
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 32

Expert Comment

by:it_saige
ID: 40468987
I am working on an example project...  What is the purpose for each checkbox?

-saige-
0
 

Author Comment

by:Jason
ID: 40469601
Basically this is a betting application
If you checked either 1, 2 and/or 3 the system uses the runner number to work out possible combinations
0
 

Author Comment

by:Jason
ID: 40469605
Need 3  checkboxes for every entry
0
 

Author Comment

by:Jason
ID: 40469620
from the checked item i need to output an intarray of the runners numbers but only one instance of the numbers.

i can then use this array to calc the number of combinations and calc the outlay.

the objectlistview needs to be sorted by the second column (Win Odds) from favorite to least favorite
0
 
LVL 32

Expert Comment

by:it_saige
ID: 40469698
So the user can check only one of the three checkboxes?

-saige-
0
 

Author Comment

by:Jason
ID: 40469880
No can check all 3 or 2 or 1
0
 

Author Comment

by:Jason
ID: 40469904
You can check all 3 check boxes
example for the fav you would check 1 2 and 3 because you estimate that they will land on of those positions
where as the second place you would check only 2 AND 3 due to estimating that they are likely to only come a 2nd or 3rd place
and one that you only this may run a third place only check the 3rd check box

Hope this helps
0
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
ID: 40469951
Something like this might be what you are after:

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.ObjectListView1 = New BrightIdeasSoftware.ObjectListView()
		Me.colRunnerNum = CType(New BrightIdeasSoftware.OLVColumn(), BrightIdeasSoftware.OLVColumn)
		Me.colWinOdds = CType(New BrightIdeasSoftware.OLVColumn(), BrightIdeasSoftware.OLVColumn)
		Me.colCheck1 = CType(New BrightIdeasSoftware.OLVColumn(), BrightIdeasSoftware.OLVColumn)
		Me.colCheck2 = CType(New BrightIdeasSoftware.OLVColumn(), BrightIdeasSoftware.OLVColumn)
		Me.colCheck3 = CType(New BrightIdeasSoftware.OLVColumn(), BrightIdeasSoftware.OLVColumn)
		CType(Me.ObjectListView1, System.ComponentModel.ISupportInitialize).BeginInit()
		Me.SuspendLayout()
		'
		'ObjectListView1
		'
		Me.ObjectListView1.AllColumns.Add(Me.colRunnerNum)
		Me.ObjectListView1.AllColumns.Add(Me.colWinOdds)
		Me.ObjectListView1.AllColumns.Add(Me.colCheck1)
		Me.ObjectListView1.AllColumns.Add(Me.colCheck2)
		Me.ObjectListView1.AllColumns.Add(Me.colCheck3)
		Me.ObjectListView1.AllowColumnReorder = True
		Me.ObjectListView1.AllowDrop = True
		Me.ObjectListView1.AlternateRowBackColor = System.Drawing.Color.FromArgb(CType(CType(255, Byte), Integer), CType(CType(240, Byte), Integer), CType(CType(220, Byte), Integer))
		Me.ObjectListView1.BackColor = System.Drawing.SystemColors.Window
		Me.ObjectListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.colRunnerNum, Me.colWinOdds, Me.colCheck1, Me.colCheck2, Me.colCheck3})
		Me.ObjectListView1.Cursor = System.Windows.Forms.Cursors.Default
		Me.ObjectListView1.EmptyListMsg = "This list is empty."
		Me.ObjectListView1.FullRowSelect = True
		Me.ObjectListView1.Location = New System.Drawing.Point(13, 13)
		Me.ObjectListView1.Name = "ObjectListView1"
		Me.ObjectListView1.OwnerDraw = True
		Me.ObjectListView1.Size = New System.Drawing.Size(421, 237)
		Me.ObjectListView1.TabIndex = 0
		Me.ObjectListView1.UseAlternatingBackColors = True
		Me.ObjectListView1.View = System.Windows.Forms.View.Details
		'
		'colRunnerNum
		'
		Me.colRunnerNum.AspectName = "RunnerNum"
		Me.colRunnerNum.Groupable = False
		Me.colRunnerNum.Text = "Running Numbers"
		Me.colRunnerNum.Width = 100
		'
		'colWinOdds
		'
		Me.colWinOdds.AspectName = "WinOdds"
		Me.colWinOdds.Groupable = False
		Me.colWinOdds.Text = "Odds of Winning"
		Me.colWinOdds.Width = 100
		'
		'colCheck1
		'
		Me.colCheck1.AspectName = "Check1"
		Me.colCheck1.CheckBoxes = True
		Me.colCheck1.Text = "CheckBox1"
		Me.colCheck1.Width = 38
		'
		'colCheck2
		'
		Me.colCheck2.AspectName = "Check2"
		Me.colCheck2.CheckBoxes = True
		Me.colCheck2.Text = "CheckBox2"
		Me.colCheck2.Width = 38
		'
		'colCheck3
		'
		Me.colCheck3.AspectName = "Check3"
		Me.colCheck3.CheckBoxes = True
		Me.colCheck3.Text = "CheckBox3"
		Me.colCheck3.Width = 38
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(446, 262)
		Me.Controls.Add(Me.ObjectListView1)
		Me.Name = "Form1"
		Me.Text = "Form1"
		CType(Me.ObjectListView1, System.ComponentModel.ISupportInitialize).EndInit()
		Me.ResumeLayout(False)

	End Sub
	Friend WithEvents ObjectListView1 As BrightIdeasSoftware.ObjectListView
	Friend WithEvents colRunnerNum As BrightIdeasSoftware.OLVColumn
	Friend WithEvents colWinOdds As BrightIdeasSoftware.OLVColumn
	Friend WithEvents colCheck1 As BrightIdeasSoftware.OLVColumn
	Friend WithEvents colCheck2 As BrightIdeasSoftware.OLVColumn
	Friend WithEvents colCheck3 As BrightIdeasSoftware.OLVColumn

End Class

Open in new window


Form1.vb -
Imports BrightIdeasSoftware

Public Class Form1
	Private Runners As List(Of RunnersData)
	Private TypedList As TypedObjectListView(Of RunnersData)
	Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
		Try
			Runners = New List(Of RunnersData) From { _
			 New RunnersData With {.RunnerNum = "1", .WinOdds = "2.20", .Check1 = False, .Check2 = True, .Check3 = False}, _
			 New RunnersData With {.RunnerNum = "7", .WinOdds = "4.40", .Check1 = True, .Check2 = False, .Check3 = True} _
			}

			For Each Runner In Runners
				AddHandler Runner.CheckChanged, AddressOf OnCheckChanged
			Next
			ObjectListView1.SetObjects(Runners)
			TypedList = New TypedObjectListView(Of RunnersData)(ObjectListView1)
			TypedList.GenerateAspectGetters()
		Catch ex As Exception

		End Try
	End Sub

	Private Sub OnCheckChanged(ByVal sender As Object, ByVal e As CheckChangedEventArgs)
		If TypeOf (sender) Is RunnersData Then
			Dim data As RunnersData = CType(sender, RunnersData)
			MessageBox.Show(String.Format("Checked value has changed for: Runner Number - {0}; Check Property - {1} is {2}", data.RunnerNum, e.PropertyName, e.PropertyValue))
		End If
	End Sub
End Class

Public Class RunnersData
	Private ReadOnly _checkChangedEventHandlers As New List(Of CheckChangedEventHandler)
	Public Custom Event CheckChanged As CheckChangedEventHandler
		AddHandler(ByVal value As CheckChangedEventHandler)
			_checkChangedEventHandlers.Add(value)
		End AddHandler

		RemoveHandler(ByVal value As CheckChangedEventHandler)
			_checkChangedEventHandlers.Remove(value)
		End RemoveHandler

		RaiseEvent(ByVal sender As Object, ByVal e As CheckChangedEventArgs)
			For Each handler As CheckChangedEventHandler In _checkChangedEventHandlers
				Try
					handler.Invoke(sender, e)
				Catch ex As Exception
					Debug.WriteLine(String.Format("Exception while invoking event handler: {0}", ex))
				End Try
			Next
		End RaiseEvent
	End Event

	Protected Overridable Sub OnCheckChanged(ByVal sender As Object, ByVal e As CheckChangedEventArgs)
		RaiseEvent CheckChanged(sender, e)
	End Sub

	Private _check1 As Boolean
	Private _check2 As Boolean
	Private _check3 As Boolean

	Public Property RunnerNum() As String
	Public Property WinOdds() As String
	Public Property Check1() As Boolean
		Get
			Return _check1
		End Get
		Set(value As Boolean)
			If Not value.Equals(_check1) Then
				_check1 = value
				RaiseEvent CheckChanged(Me, New CheckChangedEventArgs("Check1", _check1))
			End If
		End Set
	End Property
	Public Property Check2() As Boolean
		Get
			Return _check2
		End Get
		Set(value As Boolean)
			If Not value.Equals(_check2) Then
				_check2 = value
				RaiseEvent CheckChanged(Me, New CheckChangedEventArgs("Check2", _check2))
			End If
		End Set
	End Property
	Public Property Check3() As Boolean
		Get
			Return _check3
		End Get
		Set(value As Boolean)
			If Not value.Equals(_check3) Then
				_check3 = value
				RaiseEvent CheckChanged(Me, New CheckChangedEventArgs("Check3", _check3))
			End If
		End Set
	End Property
End Class

Public Delegate Sub CheckChangedEventHandler(sender As Object, e As CheckChangedEventArgs)

Public Class CheckChangedEventArgs
	Inherits EventArgs
	Private ReadOnly _propertyName As String
	Public ReadOnly Property PropertyName() As String
		Get
			Return _propertyName
		End Get
	End Property

	Private ReadOnly _propertyValue As Boolean
	Public ReadOnly Property PropertyValue() As Boolean
		Get
			Return _propertyValue
		End Get
	End Property

	Private Sub New()

	End Sub

	Public Sub New(ByVal PropertyName As String, ByVal PropertyValue As Boolean)
		_propertyName = PropertyName
		_propertyValue = PropertyValue
	End Sub
End Class

Open in new window


Produces the following form -Capture.JPG
And when I change one of the checkboxes -Capture.JPG
I had set the initial values for testing, but you can obviously change that.  Attached you will also find the project zipped.

Once you unzip the attached project, you will have to add the ObjectListView project or DLL and readd the reference as I could not include the dll directly.

-saige-
EE-Q28569127.zip
0
 

Author Closing Comment

by:Jason
ID: 40470046
Great think i can work with that.

Thx for your help
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now