Results 1 to 6 of 6

Thread: [2005] Checkbox Checkstate

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    48

    [2005] Checkbox Checkstate

    Hello,

    When changing the .Checked or .CheckState property of a checkbox control in runtime, the CheckBox_CheckedChanged event is automatically called, for obvious reasons which I completely understand. Still, is there a way to somehow not let it do this?

  2. #2
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: [2005] Checkbox Checkstate

    As you know, thats exactly what its for, what are you trying to do ?

    Casey.

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    48

    Re: [2005] Checkbox Checkstate

    I'm using the checkboxes as a list for tools. I'm working on a simple tile editor and I have these tools like pencil, color picker etc. I'm using checkboxes with appearance = button and with an image, no text. It's easy this way to see which is selected, and I don't need to create 2 different images or something for when the tool is selected.

    So, when one of the checkboxes is clicked, I also need to uncheck the previously selected tool. When doing this, its checkedChanged event is called, which is not what I want.

  4. #4
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: [2005] Checkbox Checkstate

    If i understand you correctly, why dont you run the code in the mouseclick event so you know the user is actually selecting the checkbox and its not being deselected by code, even better, so you dont have to deselect the others is to use radiobuttons with the same appearance.


    Casey.

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    48

    Re: [2005] Checkbox Checkstate

    Thanks. I somehow never thought of using radiobuttons for this; neither did I think of using the Click event. Probably because the default event it opened was the checkChanged one.

    Thanks a lot for your help, much appreciated

    Now, there's something else that's bothering me. I think it has something to do with the focus of a control. Let me explain this through images.

    Pencil Tool is selected;

    I select the Picker Tool;

    I use it; the Pencil Tool is automatically selected;


    There's my problem; the Picker Tool contains a blue rectangle around the image, probably because it still has focus or something. It goes away when I click on either of the buttons.

    I don't want these controls to be able to get the focus anyway. I looked into the Active, ActiveControl, Focus and Selectable properties of the control, but the radiobutton (or the checkbox) does not seem to have these properties.

    What do I do?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Checkbox Checkstate

    You can't prevent the standard controls from accepting focus. You have to inherit the class of interest (e.g. Button, CheckBox, RadioButton) and add code to prevent it. It just so happens that I've done that myself for a recent project. Here's one I prepared earlier:
    vb.net Code:
    1. ''' <summary>
    2. ''' A radio button control that cannot receive focus.
    3. ''' </summary>
    4. Public Class UnselectableRadioButton
    5.     Inherits System.Windows.Forms.RadioButton
    6.  
    7. #Region " Constructors "
    8.  
    9.     ''' <summary>
    10.     ''' Initialises a new instance of the <see cref="UnselectableRadioButton"/> class.
    11.     ''' </summary>
    12.     Public Sub New()
    13.         MyBase.New()
    14.  
    15.         'The radio button cannot receive focus.
    16.         Me.SetStyle(ControlStyles.Selectable, False)
    17.     End Sub
    18.  
    19. #End Region 'Constructors
    20.  
    21. #Region " Methods "
    22.  
    23.     Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
    24.         MyBase.OnClick(e)
    25.     End Sub
    26.  
    27. #End Region 'Methods
    28.  
    29. End Class
    Once you add that class to your project and build it will appear in your Toolbox, so you can add instances to forms just as you do regular RadioButtons. You don't have to remove the regular RadioButtons you already have though. You can open the designer code file for your form and just do a Find & Replace to replace "System.Windows.Forms.RadioButton" with "UnselectableRadioButton".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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