Uncheck checkbox when other checkbox is selected

Posted on 2005-05-02
Last Modified: 2008-03-17
I have three checkboxes:


I have been trying to write some code that will uncheck the other checkboxes that are NOT selected.  I thought that I could just write something like:

If tankCheck.Checked = True Then
    prodCheck.Checked = False
End If

But, that doesn't work.  In other words, you can only have one checkbox selected at a time.
Question by:johnqtr
    LVL 13

    Accepted Solution

    You need to access the Checked properties with the enumerator:
    if tankCheck.Checked = CheckState.Checked then
      prodCheck.Checked = CheckState.Unchecked
      dateCheck.Checked = CheckState.Unchecked
    elseif prodCheck.Checked = CheckState.Checked then
      tankCheck.Checked = CheckState.Unchecked
      dateCheck.Checked = CheckState.Unchecked
    elseif dateCheck.Checked = CheckState.Checked then
      prodCheck.Checked = CheckState.Unchecked
      tankCheck.Checked = CheckState.Unchecked
    end if

    HOWEVER: I wouldn't do this - I'd use option boxes or even a combo dropdown. It's much cleaner and the user will know which action to expect. Checkboxes that automatically uncheck when you select a different option are kind of scary :)

    LVL 24

    Expert Comment

    by:Jeff Certain
    Create a single event handler than handles teh events for all 3 check boxes. Then, use this code:

    If prodCheck.Checked OrElse tankCheck.Checked then dateCheck.Checked = False
    If tankCheck.Checked OrElse dateCheck.Checked then prodCheck.Checked = False
    If prodCheck.Checked OrElse dateCheck.Checked then tankCheck.Checked = False
    LVL 35

    Expert Comment

    use RadioButton instead

    Author Comment

    Dropdown would be better.  I just didn't think about that.  Thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now