By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,407 Members | 2,842 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,407 IT Pros & Developers. It's quick & easy.

VB disable checkbox checking by user?

P: 5
I have a program where a checkbox greatly affects its outcome.
What it is suppose to do is, when the value of an item is less then the value of a text box, then the program will check it off, and then re-calculate the remaining numbers and update a label.

I want to be able to programically check/uncheck items BUT I dont want the user to manually be able to do so ALL AT THE SAME TIME, I want the item to still be "selected" (highlighted) when they click on it.
Jan 22 '14 #1

✓ answered by Luk3r

Here's what I've come up with to meet your requirements. Hopefully you can work with it and shape it to your desires.

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Dim btnClick As Boolean = False
  3.  
  4.      Private Sub CheckForChecks(ByVal e As System.Windows.Forms.ItemCheckEventArgs)
  5.         If btnClick = True Then
  6.             btnClick = False
  7.         Else
  8.             e.NewValue = e.CurrentValue
  9.         End If
  10.     End Sub
  11.  
  12.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  13.         btnClick = True
  14.         CheckedListBox1.SetItemChecked(1, True)
  15.     End Sub
  16.  
  17.     Private Sub CheckedListBox1_ItemCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
  18.         Call CheckForChecks(e)
  19.     End Sub
  20.  
  21.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  22.         Dim txtBoxInt As Integer = TextBox1.Text
  23.         Dim cLBInt As Integer = CheckedListBox1.SelectedItem
  24.         If txtBoxInt > cLBInt Then
  25.             MsgBox(CheckedListBox1.SelectedIndex)
  26.         End If
  27.     End Sub
  28. End Class
I suggest you create a new project and play with this code first. It only requires a Button, a CheckedListBox, and a TextBox.

Share this Question
Share on Google+
5 Replies


100+
P: 299
If I'm understanding correctly, then you'd want to setup the Textbox.TextChanged event. When the text is changed, you want your code to run where it's checking for the value of the item and then tell it that Checkbox.Checked = True. Then, setup a Checkbox.CheckChanged... your code would say, If Checkbox.checked = True then re-calculate. Hope this helps! If it's not a good explanation, show us what you're working with or give us a little more detail and I'd be more than happy to show you an example of how I would personally do it.
Jan 22 '14 #2

P: 5
Well, what I am exactly trying to do is:

In the checkedlistbox1, the user CANNOT check off items during runtime
BUT, they can still click on the item and it will highlight the item as it would if they COULD check off items.
So now, they can ONLY control the checking of the items off by this textbox1.

So, lets say the first item in listbox was 50
And they enter 100 in Textbox1
Since 100 is infact > 50, the program will simply find this item that = "50" and check it off

But, if you so wish, you can click on the item, and it will highlight the item from the listbox so you can see where you are at.

Right no I dont have any code that deals with that, but on the load function of the form, I have the listbox's selection mode set to "none". The issue with this method though is, they CANT check off any items (which is what I want), but at the same time, they also cannot select (highlight) any items, which is a problem. I want them to be able to both NOT be able to check off items, AND still be able to select (highlight) the item. And when I say select (highlight), I literally mean, how listboxes do, when you click on an item, it will literally select it, and you know this by the background of that item becoming dark blue like it is "highlighted".

If that explanation helps.
Jan 23 '14 #3

100+
P: 299
Here's what I've come up with to meet your requirements. Hopefully you can work with it and shape it to your desires.

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Dim btnClick As Boolean = False
  3.  
  4.      Private Sub CheckForChecks(ByVal e As System.Windows.Forms.ItemCheckEventArgs)
  5.         If btnClick = True Then
  6.             btnClick = False
  7.         Else
  8.             e.NewValue = e.CurrentValue
  9.         End If
  10.     End Sub
  11.  
  12.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  13.         btnClick = True
  14.         CheckedListBox1.SetItemChecked(1, True)
  15.     End Sub
  16.  
  17.     Private Sub CheckedListBox1_ItemCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
  18.         Call CheckForChecks(e)
  19.     End Sub
  20.  
  21.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  22.         Dim txtBoxInt As Integer = TextBox1.Text
  23.         Dim cLBInt As Integer = CheckedListBox1.SelectedItem
  24.         If txtBoxInt > cLBInt Then
  25.             MsgBox(CheckedListBox1.SelectedIndex)
  26.         End If
  27.     End Sub
  28. End Class
I suggest you create a new project and play with this code first. It only requires a Button, a CheckedListBox, and a TextBox.
Jan 23 '14 #4

P: 5
Wow, thank you! (after making some slight variations so it worked for my program) the code worked perfectly and did exactly what I wanted it to do! Thanks again for the help.
Jan 25 '14 #5

100+
P: 299
You're welcome and glad I could help.

Edit**: Also, for anyone else that may use this, Call CheckForChecks2(e) should just be Call CheckForChecks(e).
Jan 26 '14 #6

Post your reply

Sign in to post your reply or Sign up for a free account.