-
Oct 23rd, 2014, 11:30 AM
#1
Thread Starter
New Member
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.
-
Oct 23rd, 2014, 12:46 PM
#2
Addicted Member
Re: VS CheckListBox msgbox on no entry selected
If you mean on something like this
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
-
Oct 23rd, 2014, 12:50 PM
#3
Thread Starter
New Member
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.
-
Oct 23rd, 2014, 12:55 PM
#4
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
-
Oct 23rd, 2014, 12:57 PM
#5
Thread Starter
New Member
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.
-
Oct 23rd, 2014, 01:00 PM
#6
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
-
Oct 23rd, 2014, 01:05 PM
#7
Addicted Member
Re: VS CheckListBox msgbox on no entry selected
Originally Posted by dereksouthard
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|