locked
[:: CheckBox as RadioButton? ::] RRS feed

  • Question

  • Hello all,

    Is it possible to use CheckBox as RadioButton ?

    If so, please help.

    Thanks,

    Trusted

    Monday, June 28, 2010 9:03 PM

Answers

  • Hi Trusted,

    sorry I was wrong interpreting your request. In fact, I supposed you wanted to do the opposite (use a Radio like a Checkbox) ... because nrmally if you use a Radio button alone, you can't de-select it after selecting. You should do as above.

    However since your request is make CheckBoxes act like Radio Buttons, basically there is no problem on doing that since it's simple to make like this:

    1) Add your checkboxes to the form

    2) Add a "Click" event handler on all the checkboxes, where you will be clearing the checkbox on the other ones and keep checked only the one you just checked... for instance

    And you're done.

    Obviously to make this you should write some lines of code.

    Hope it helps,


    Adriano
    • Marked as answer by Trusted Tuesday, June 29, 2010 1:16 PM
    Tuesday, June 29, 2010 7:44 AM

All replies

  • Yes, you can:

      public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();

                radioButton1.Click+=new EventHandler(radioButton1_Click);
            }

            bool checkStatus = false;

            void radioButton1_Click(object sender, System.EventArgs e)
            {
               checkStatus = !checkStatus;
               radioButton1.Checked = checkStatus;
            }

        }
    Hope it helps,


    Adriano
    Monday, June 28, 2010 9:16 PM
  • Hi Adriano,

    Thanks for your reply.

    I want CheckBoxes to be act like RadioButtons or RadioButtons without using any groupbox.

    Sorry, I did not follow what you wrote above, cause I am a novice [newbie] in VB 2010. Do you mean to create a new class and paste this code? Please explain.

    Thanks again,

    Trusted

     

    Tuesday, June 29, 2010 7:15 AM
  • Hi Trusted,

    sorry I was wrong interpreting your request. In fact, I supposed you wanted to do the opposite (use a Radio like a Checkbox) ... because nrmally if you use a Radio button alone, you can't de-select it after selecting. You should do as above.

    However since your request is make CheckBoxes act like Radio Buttons, basically there is no problem on doing that since it's simple to make like this:

    1) Add your checkboxes to the form

    2) Add a "Click" event handler on all the checkboxes, where you will be clearing the checkbox on the other ones and keep checked only the one you just checked... for instance

    And you're done.

    Obviously to make this you should write some lines of code.

    Hope it helps,


    Adriano
    • Marked as answer by Trusted Tuesday, June 29, 2010 1:16 PM
    Tuesday, June 29, 2010 7:44 AM
  • Adrino,

    Take a look at this thread : Proposing an Answer

     

    ~Peace~


    Thanks

    Quizwith.NET is now open for everyone !

    Living on Earth may be expensive, but did you know that it includes a free trip around the sun? Isn't that worth it?
    Tuesday, June 29, 2010 8:07 AM
  • Trusted , man, why do you do such a thing? CheckBoxes are used to make a multiple choice, and RadioButtons are used to make a single choice.

    So, everybody knows it and is an common use.

     

    If you however want to use images, you can use a toolbox (with buttons (checked - one image, unchecked- other image)


    Best regards, Sergiu
    Tuesday, June 29, 2010 9:19 AM
  • If you're a little creative you can make a selected thing. I know this isn't the most efficient method but it'll work.

     

    void update_boxes(int a)
    
    {
    
    if (a==1) checkBox1.Checked = true; else checkBox1.Checked = false;
    
    if (a==2) checkBox2.Checked = true; else checkBox2.Checked = false;
    
    if (a==3) checkBox3.Checked = true; else checkBox3.Checked = false;
    
    }

     

    Then just add in the checkboxes' changed event:

    if (checkBox1.Checked) update_boxes(1); else checkBox1.Checked = true; // Turn back on if it's unchecked again

     

    Doesn't my awesome scripts look newbie?

    Tuesday, June 29, 2010 9:35 AM
  • Hi Sergiu,

     

    I was using a GroupBox on a PictureBox holding a Bitmap. When I use RadioButtons on GroupBox, some text from Bitmap [which is in picturebox] is not visible. And there is no way I found to make GroupBox backcolor transparent.

     

    So, I decided to use checkboxes as radiobuttons, IF POSSIBLE.

    Trusted.

    Tuesday, June 29, 2010 12:59 PM
  • Hi Yoshi,

    You look smart, but is this the VB code?

    I tried to copy and paste, and I got error on each and every word. ???

    If it can be used in VB 2010, please help.

    Thanks,

    Trusted

    Tuesday, June 29, 2010 1:06 PM
  • 1) Add your checkboxes to the form

    2) Add a "Click" event handler on all the checkboxes, where you will be clearing the checkbox on the other ones and keep checked only the one you just checked... for instance

    And you're done.

    Hi Adriano,

    Thanks a bunch of millions.

    It worked. May Allah give you the best reward.

    The simple code I wrote is follows: [I am writing this so that it can be helpful for others].

    ----------------------------

     

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

            If CheckBox1.Checked = True Then

                CheckBox2.Checked = False

            End If

        End Sub

     

     

    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged

            If CheckBox2.Checked = True Then

                CheckBox1.Checked = False

            End If

        End Sub

     

    -------------------------------

    This way to tooooo easy for a newbie like me.

    I was stuck on this since three days now.

    Thanks again,

    Trusted

    Tuesday, June 29, 2010 1:18 PM
  • Trusted, you can make your GroupBox transparent. See my answer on the corresponding thread.
    Best regards, Sergiu
    Tuesday, June 29, 2010 1:35 PM
  • Hello Sergiu,

    I could not able to make GroupBox transparent ON PictureBox.

    PictureBox size is big, let say about 600x400 pixels carrying an image .gif bitmap. That bitmap has questions, like a exam question paper with "Correct" or "Wrong" check options.

    So, when I try to use GroupBox on each question, some text from bitmap is hiding behind the GroupBox, and I could not able to make it transparent ON PictureBox specailly in VB 2010.

    GroupBox is working fine with transparency without PictureBox.

    I hope you understand,

    Trusted

    Tuesday, June 29, 2010 2:18 PM
  • Oh, I'm sorry, my code is written in C#.

     

    Below you can find the VB translated version.

     


    Private Sub UpdateBoxes(ByVal a As Integer)
    If (a==1) Then checkBox1.Checked = True Else checkBox1.Checked = False End If If (a==2) Then checkBox2.Checked = True Else checkBox2.Checked = False End If
    If (a==3) Then checkBox3.Checked = True Else checkBox3.Checked = False End If
    End Sub

     

    Then just add in the checkboxes' changed event:

     

    If (checkBox1.Checked) Then update_boxes(1) Else checkBox1.Checked = True End If ' Turn back on if it's unchecked again

     

    • Edited by _Yoshi_ Tuesday, June 29, 2010 7:08 PM
    Tuesday, June 29, 2010 5:03 PM
  • Hey Yoshi ???

    You have copied back the same code :)

     

    Trusted

    Tuesday, June 29, 2010 5:29 PM
  • That's false, this one has got if's like VB has :].
    Tuesday, June 29, 2010 5:30 PM
  • sorry, but not working... with VB 2010

     

    Same full of blue lines under each word :)

    Tuesday, June 29, 2010 5:36 PM
  • Oh, I'm sorry, my code is written in C#.

     

    Below you can find the VB translated version.

     


    void update_boxes(int a)
    
    
    
    {
    
    
    
    If (a==1) Then checkBox1.Checked = True Else checkBox1.Checked = False End If
    
    
    
    If (a==2) Then checkBox2.Checked = True Else checkBox2.Checked = False End If
    If (a==3) Then checkBox3.Checked = True Else checkBox3.Checked = False End If
    }

     

    Then just add in the checkboxes' changed event:

     

    If (checkBox1.Checked) Then update_boxes(1) Else checkBox1.Checked = True End If ' Turn back on if it's unchecked again

     


    Actually, that is an unholy mix of VB and C#!

    Try this:

    Private Sub UpdateBoxes(ByVal a As Integer)
      If a = 1 Then
        CheckBox1.Checked = True
      Else
        CheckBox1.Checked = False
      End If
    
      If a = 2 Then
        CheckBox2.Checked = True
      Else
        CheckBox2.Checked = False
      End If
    
      If a = 3 Then
        CheckBox3.Checked = True
      Else
        CheckBox3.Checked = False
      End If
    End Sub
    Tuesday, June 29, 2010 6:57 PM
  • Yea, I'm not using VB often (Or not at all the last few years). I dislike all those words.

    I updated my code in order to not confuse other people.

    @ Trusted, Next time specify with language you're writing your scripts in. I use C# most of the time so I just give answers in that language, it's a forum for all languages here.

    Tuesday, June 29, 2010 7:10 PM
  • Thank you Shawn,

    I have done it in other way, posted above.

    Trusted

    Wednesday, June 30, 2010 2:21 PM
  • OK Yoshi,

    It is my mistake that I did not mention initially, then it is your mistake that you have not seen when I added what language I am using :D

    Thanks anyway for your unuseful help. :D

    Trusted

    Wednesday, June 30, 2010 2:23 PM
  • Hello, here a simple answer to transform the CheckBox to a RadioButton:

    object clickBox = null;
    
     private void checkBox_Click(object sender, EventArgs e)
        {
            clickBox = sender;
    
            foreach (Control c in this.Controls)
            {
                if (c is CheckBox)
                {
                    if (c != clickBox)
                    {
                        ((CheckBox)c).Checked = false;
                    }
                }
            }

    And add this Click event o every Checkbox Finish


    • Edited by viper0013 Thursday, October 10, 2013 10:57 AM
    Thursday, October 10, 2013 10:56 AM
  • Hello,

    Even thou you seem to have a solution I would highly suggest not changing the behavior of standard controls to act like another control as generally speaking this leads to confusion to the customer of the application. One of the base concepts of Windows is commonality in regards to a specific control acts the same way in all applications i.e. a button. If we look at a button for instance you click on the button to trigger something or single click (when there is one) the down arrow on the right side of a button for additional options. The same holds true when talking about RadioButton and CheckBox controls. Of course if business asked for this and you warned them why it's a bad idea and they still want it no matter what then you have no choice but do not simply do this.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Thursday, October 10, 2013 1:43 PM