Checkedlistbox

vbron399

New member
Joined
Jun 20, 2017
Messages
4
Programming Experience
5-10
I need to know how to select the indexes in a checked list box and get the text. I also need to know how to go through the indexes and set the check state.
 
If you want to know how to use any particular type or member then the first thing you should do is read the documentation for that type or member. The documentation for the CheckedlistBox is here. That page is for the Australian culture and .NET 7 but you can change both for your own needs. You would be able to glean plenty of useful information from that for yourself, like the fact that it has GetItemText, SetItemChecked and SetItemCheckState methods. It also has CheckedItems and CheckedIndices properties. You can also loop over the Items collection using a For or For Each loop, just as you can any other collection. That's everything you need to do what you described.

I'd be interested to see you make an attempt to put the pieces together yourself. We can always help further if what you try doesn't work but you can at least try first. Put some thought into the logic required, formalise the steps into an algorithm and then write code to implement that algorithm. If what you write doesn't work, you can show us the algorithm and the code and point out exactly where you're having trouble.
 
If you want to know how to use any particular type or member then the first thing you should do is read the documentation for that type or member. The documentation for the CheckedlistBox is here. That page is for the Australian culture and .NET 7 but you can change both for your own needs. You would be able to glean plenty of useful information from that for yourself, like the fact that it has GetItemText, SetItemChecked and SetItemCheckState methods. It also has CheckedItems and CheckedIndices properties. You can also loop over the Items collection using a For or For Each loop, just as you can any other collection. That's everything you need to do what you described.

I'd be interested to see you make an attempt to put the pieces together yourself. We can always help further if what you try doesn't work but you can at least try first. Put some thought into the logic required, formalise the steps into an algorithm and then write code to implement that algorithm. If what you write doesn't work, you can show us the algorithm and the code and point out exactly where you're having trouble.

I have tried many things but I just can't get anything to work. I have even looked online but no luck. just something to point me in the right direction would be helpful.
 
I have tried many things but I just can't get anything to work.
What have you tried? What happened when you tried it? We can't read your mind. If you don't explain the actual problem then we can't help you solve it.
I have even looked online but no luck
You don't need luck. You need to do actual software development, as I have already described. Consider the logic first, formalise the logic into an algorithm and then write code to specifically implement that algorithm. Why would you expect to be able to write code if you don't know what it has to do? I'm not talking about just the end result but rather the steps to get there.
just something to point me in the right direction would be helpful.
That's exactly what I did in post #2. I pointed you in the right direction and now I'd like to see you take some steps in that direction for yourself. I'm still quite willing to help if you encounter an actual issue but that won't happen until you actually try something. If you have tried something and it didn't work then explain that to us. Show us what you did and explain what happened and exactly how that doesn't meet your expectations. You should be able to explain exactly what the code you have is supposed to do.
 
Hello,
Is that what you meant? I did not fully understand your question.
However, I can offer you an example. I am a novice at this. I am learning with you.


VB.NET:
Public Class Form1
    Dim file As New OpenFileDialog
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CheckedListBox1.Items.Add("Sunday", CheckState.Unchecked)
        CheckedListBox1.Items.Add("Mondey", CheckState.Unchecked)
        CheckedListBox1.Items.Add("Tuesday", CheckState.Unchecked)
        CheckedListBox1.Items.Add("Wednesday", CheckState.Unchecked)
        CheckedListBox1.Items.Add("Thursday", CheckState.Checked)
        CheckedListBox1.Items.Add("Friyday", CheckState.Unchecked)
        CheckedListBox1.Items.Add("Saturday", CheckState.Checked)
        CheckedListBox1.SelectedIndex = 0
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If file.ShowDialog = DialogResult.OK Then
            CheckedListBox1.Items.Add(file.SafeFileName)
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Label1.Text = "Checked :" & CheckedListBox1.CheckedItems.Count
        Label2.Text = "Checked :" & CheckedListBox1.Items.Count - CheckedListBox1.CheckedItems.Count

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim i As Integer
        For i = 0 To CheckedListBox1.Items.Count - 1
            CheckedListBox1.SetItemChecked(i, False)
        Next
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim i As Integer
        For i = 0 To CheckedListBox1.Items.Count - 1
            CheckedListBox1.SetItemChecked(i, True)
        Next
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

        For i = CheckedListBox1.Items.Count - 1 To 0 Step -1
            If CheckedListBox1.GetItemChecked(i) Then
                CheckedListBox1.Items.RemoveAt(i)
            End If
        Next
    End Sub
End Class

example sourch code download link..
chekedlistbox.jpeg
 

Attachments

  • listbox example chekbox.zip
    74.7 KB · Views: 8
Last edited by a moderator:
@Karsli36, please do not post unformatted code snippets. They are unnecessarily hard to read, primarily because they lack indenting. This is the second time I have formatted code for you. Please do so for yourself in future posts.
 
Back
Top