VB disable checkbox checking by user?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Leftic
    New Member
    • Dec 2013
    • 5

    VB disable checkbox checking by user?

    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.
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    #2
    If I'm understanding correctly, then you'd want to setup the Textbox.TextCha nged 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.Checke d = True. Then, setup a Checkbox.CheckC hanged... your code would say, If Checkbox.checke d = 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.

    Comment

    • Leftic
      New Member
      • Dec 2013
      • 5

      #3
      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 "highlighte d".

      If that explanation helps.

      Comment

      • Luk3r
        Contributor
        • Jan 2014
        • 300

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

        Code:
        Public Class Form1
            Dim btnClick As Boolean = False
        
             Private Sub CheckForChecks(ByVal e As System.Windows.Forms.ItemCheckEventArgs)
                If btnClick = True Then
                    btnClick = False
                Else
                    e.NewValue = e.CurrentValue
                End If
            End Sub
        
            Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                btnClick = True
                CheckedListBox1.SetItemChecked(1, True)
            End Sub
        
            Private Sub CheckedListBox1_ItemCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
                Call CheckForChecks(e)
            End Sub
        
            Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
                Dim txtBoxInt As Integer = TextBox1.Text
                Dim cLBInt As Integer = CheckedListBox1.SelectedItem
                If txtBoxInt > cLBInt Then
                    MsgBox(CheckedListBox1.SelectedIndex)
                End If
            End Sub
        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.
        Last edited by zmbd; Jan 27 '14, 01:30 PM. Reason: [z{fixed code per Luk3r's post}]

        Comment

        • Leftic
          New Member
          • Dec 2013
          • 5

          #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.

          Comment

          • Luk3r
            Contributor
            • Jan 2014
            • 300

            #6
            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).
            Last edited by zmbd; Jan 27 '14, 01:31 PM. Reason: [z{fixed the posted code to reflect change}]

            Comment

            Working...