Results 1 to 7 of 7

Thread: VS CheckListBox msgbox on no entry selected

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    6

    VS CheckListBox msgbox on no entry selected

    I have a userform with a CheckListBox and a submit button. I would like an error to appear if nothing is selected. Everything I try works great...the first time. If I check an item, then uncheck it, my form thinks an item is still checked. ie:

    Nothing checked
    If CheckedListBox1.SelectedIndex = -1 Then
    MsgBox("Worked")
    MsgBox(CheckedListBox1.SelectedIndex)
    End If
    I get a message box that says "Worked"/"-1", but if I check a box, then uncheck it, nothing appears and the selected index changes to "0"

    I seem to have the same issue with any code I use to check for an empty CheckListBox.

  2. #2
    Addicted Member t3cho's Avatar
    Join Date
    Mar 2014
    Posts
    234

    Re: VS CheckListBox msgbox on no entry selected

    If you mean on something like this

    Name:  a60b2209d78328420fefb962cd43a31b.jpg
Views: 241
Size:  13.7 KB

    One line code :

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MsgBox(CheckedListBox1.SelectedItem)
        End Sub
    End Class
    It will be changing while you selecting and pressing button. Otherwise if you want to change the value of msgbox each time you change the value then use
    Code:
    CheckedListBox1_SelectedIndexChanged
    Anel

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    6

    Re: VS CheckListBox msgbox on no entry selected

    Thanks for the reply.
    This does work for me, but then uncheck item3 and the msgbox will still say "Item3". The issue is, it wont go back to a null value.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: VS CheckListBox msgbox on no entry selected

    Try this:
    Code:
    If CheckedListBox1.CheckedItems.Count = 0 Then
        MessageBox.Show("No value selected.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    6

    Re: VS CheckListBox msgbox on no entry selected

    Thanks agian for the replies. I ended up just using a ListBox instead of CheckedListBox and it worked fine.

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: VS CheckListBox msgbox on no entry selected

    Same principal would apply, only a different property would be used:
    Code:
    If ListBox1.SelectedItems.Count = 0 Then
        MessageBox.Show("No value selected.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    Addicted Member t3cho's Avatar
    Join Date
    Mar 2014
    Posts
    234

    Re: VS CheckListBox msgbox on no entry selected

    Quote Originally Posted by dereksouthard View Post
    Thanks for the reply.
    This does work for me, but then uncheck item3 and the msgbox will still say "Item3". The issue is, it wont go back to a null value.
    Than you can do like this

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If CheckedListBox1.SelectedItem = Nothing Then
                MsgBox("Nothing checked")
            Else
                MsgBox(CheckedListBox1.SelectedItem)
            End If
        End Sub
    End Class
    Anel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width