Question checkbox plus text weirdly difficult

ntaylor

Member
Joined
Sep 2, 2010
Messages
6
Programming Experience
10+
I'm still pretty new to VB.NET so please forgive if this is obvious.

All I want to do is build some kind of table-like or grid-like control in my form that has two columns. An element in the first column should be a checkbox that is changable by the user, and an element in the second column is some un-editable text. Of course, when the user checks or unchecks a checkbox, I have to be able to handle that event and figure out which row it came from.

I've been trying this all day with no luck. Do I use a ListView or a DataGridView? If the answer is DataGridView, do I add columns and rows to it directly or do I try to make a DataTable which I then assign to the DataGridView's "DataSource" property?

Is is possible to make editable checkboxes in a DataTable? I succeeded in making checkboxes by making a DataColumn of type boolean, but then I couldn't check or uncheck them during runtime.

I tried this:

Dim checkBoxCol As New DataGridViewColumn(New DataGridViewCheckBoxCell())
Form1.DataGridView1.Columns.Insert(0, checkBoxCol)

but then I couldn't figure out how to add rows. The examples online all assume that there is only string data in each row.

Also there is apparently a type called DataGridBoolColumn? What is that all about?

My point is there are a lot of different objects out there with similar sounding names, and it's confusing.

Please help.
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
I would suggest to use ListView. You will easily get familiar with it.
In addition you can't add controls in DataTable as it's not a control.
The datatable is a central object in the ADO.NET library and it represents one table of in-memory data.

Just stick to the listview. Add a column and set the TextBoxes property to True.
Then in a loop add the wanted two rows e.g.

VB.NET:
For i as Integer = 1 to 2
  Me.ListView1.Items.Add("Item " & i)
Next
 

ntaylor

Member
Joined
Sep 2, 2010
Messages
6
Programming Experience
10+
Wow, that is so much easier. Thank you.

While we're on the subject, what is the point of the "checkedListBox" control, when "ListView" has a CheckBox property that accomplishes the same thing?

Or are they different?
 

ntaylor

Member
Joined
Sep 2, 2010
Messages
6
Programming Experience
10+
more trouble with listview and checkboxes

A couple of weeks ago kulrom helped me with my problem of creating a listview with checkboxes (by just setting the "checkboxes" property, which I didn't know about before)

But now I need to make it more flexible. How can I create a listview with checkboxes where some checkboxes are enabled, but others are disabled or absent.

Basically, the user needs to be able to see a list of items, and be able to check some of them, but there should be other items in the list that cannot be checked.

Any help would be much appreciated.
 

ntaylor

Member
Joined
Sep 2, 2010
Messages
6
Programming Experience
10+
That depends on what values go into the rest of the table row. I can determine whether the checkbox should be enabled at the time I add the row element to the list view.

It would be just as good to have no checkbox at all in the row. Basically I just need to be able to isolate the checkbox in a given row of the list so that I can disable it (or remove it)

Is this beyond the abilities of a ListView? Do I need DataGridView maybe?
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
This is a small code snippet but should give you an idea about how to check the items you want and let the others unchecked.

VB.NET:
        ListView1.CheckBoxes = True ' you can set this property in the designer as well
        For i As Integer = 1 To 10
            Dim lvitem As New ListViewItem
            lvitem = ListView1.Items.Add("Something #1 - " & i)
            lvitem.SubItems.Add("Something #2 - " & i)
            lvitem.SubItems.Add("Something #3 - " & i)
            If i Mod 2 Then
                lvitem.Checked = True
            Else
                lvitem.Checked = False
            End If
        Next

And of course your IF statement will be different ... i just used the Mod to show you how ;)
 

ntaylor

Member
Joined
Sep 2, 2010
Messages
6
Programming Experience
10+
Thank you kulrom, but won't this just set the starting state of the checkbox?

I need certain checkboxes to be disabled (or absent) so that the user is *unable* to interact with them.

I'm sorry I can't experiment with your code just at the moment as I'm away from my computer that has Visual Studio installed, but I'm guessing from the name of the property ("checked") that this only reflects the state of the checkbox, not whether or not the user can interact with it. I had hoped there would be a property called "enabled", but there doesn't seem to be one.
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
First thing i can think of is to use ItemCheck event e.g.
VB.NET:
     Private Sub ListView1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView1.ItemCheck
        If e.CurrentValue = CheckState.Unchecked AndAlso ListView1.Items(e.Index).SubItems(0).Text = "Something to be checked" Then
            e.NewValue = CheckState.Unchecked
        End If
    End Sub

But for that you will have to use RemoveHandler and AddHandler statements e.g.

VB.NET:
        RemoveHandler ListView1.ItemCheck, AddressOf ListView1_ItemCheck
        ListView1.CheckBoxes = True
        For i As Integer = 1 To 10
            Dim lvitem As New ListViewItem
            lvitem = ListView1.Items.Add("Something #1 - " & i)
            lvitem.SubItems.Add("Something #2 - " & i)
            lvitem.SubItems.Add("Something #3 - " & i)
            If i Mod 2 Then
                lvitem.Checked = True
            Else
                lvitem.Checked = False
            End If
        Next
        AddHandler ListView1.ItemCheck, AddressOf ListView1_ItemCheck

Not ideal but, will do the job i guess
 
Top Bottom