Radiobuttons
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Radiobuttons

  1. #1
    Join Date
    Jan 2013
    Posts
    9

    Radiobuttons

    Hi all,

    I am using two radiobuttons in visual studio window form. Now I want some buttons to be factional when radiobutton1 is pressed and the same thing with radiobutton2.

    To do so, I copied and paste the coding of the buttons in the radiobutton1 loop, as show below:

    Code:
            private void radioButton1_CheckedChanged(object sender, EventArgs e)
            {
                private void button1_Click_MouseDown(object sender, MouseEventArgs e) 
               {
                label1.Text = "X"; 
               }
    
                private void button1_Click_MouseUp(object sender, MouseEventArgs e)  
               {
                label1.Text = "Y";   
               }
            }
    When running, the program is giving errors. Can someone please tell me the way to do this ??

    Thanks in advance.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,477

    Re: Radiobuttons

    Quote Originally Posted by Dritech View Post
    ....Now I want some buttons to be factional when radiobutton1 is pressed and the same thing with radiobutton2. .
    what is "factional"? Also, what type of errors are you seeing? We can't help if you only say you're getting an error - you need to be specific on what isn't working and the actual error you're getting.

  3. #3
    Join Date
    Jan 2013
    Posts
    9

    Re: Radiobuttons

    Hi, thanks for the reply.

    what is "factional"?
    functional.

    The errors are:

    1) } expected ... but I cannot see a missing }

    2) Expected class, delegate, enum, interface, or struc

    3) Type or namespace definition, or end-of-file expected


    Below is the coding:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace aa
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void radioButton1_CheckedChanged(object sender, EventArgs e)
            {
                private void button1_Click(object sender, EventArgs e)
               {
                 label1.Text = "Left";
                 label2.Text = "Label2";
               }
            }
    
            private void radioButton2_CheckedChanged(object sender, EventArgs e)
            {
               private void button2_Click(object sender, EventArgs e)
               {
                 label2.Text = "Right";
                 label1.Text = "Label1";
               }
            }
    
    
        }
    }
    The aim of this program is to ignore buttom2 when radiobutton1 is ticked and ignore buttom1 when radiobutton2 is true.

    Thanks in advance.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,101

    Re: Radiobuttons

    You cannot have a procedure inside another procedure. Meaning, you cannot contain void button1_Click inside radioButton1_CheckedChanged . You would have to use some sore of boolean logic to determine which radio button was selected or not.

  5. #5
    Join Date
    Jan 2013
    Posts
    9

    Re: Radiobuttons

    Hi, thanks for the reply.

    I used boolean but it will not look neat when having lets say 10 buttons for each radiobutton. This is because you need to add "(if bool == true)" for every button as shown below:

    Code:
    private void button1_Click(object sender, EventArgs e)
    {
    (if bool == true)
    
    blabla
    
    }
    Is there other ways of for to solve this please?

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,477

    Re: Radiobuttons

    Quote Originally Posted by Dritech View Post
    I used boolean but it will not look neat when having lets say 10 buttons for each radiobutton. This is because you need to add "(if bool == true)" for every button as shown below:

    Code:
    private void button1_Click(object sender, EventArgs e)
    {
    (if bool == true)
    
    blabla
    
    }
    Is there other ways of for to solve this please?
    Code:
    (if bool == true)
    isn't valid code syntax. Perhaps this is a typo in your post, but if it isn't you might want review C# syntax basics. Also, try to be clearer on what you are actually trying to do. I'm guessing that you want to enable/disable some buttons based which radio button is selected, but I'm not sure from your description.

    At any rate, check out the following post for a couple of different approaches toward this end.

    http://forums.codeguru.com/showthrea...tton-questions

  7. #7
    Join Date
    May 2002
    Location
    Boston
    Posts
    67

    Re: Radiobuttons

    Hi,

    You could use arrays of controls. Try something like this.

    Add 2 labels to a form then do this....
    Code:
     public partial class Form1 : Form
        {
    
            RadioButton[] RadioButtons = new RadioButton[10];
            Button[] Buttons = new Button[10] ;
    
            public Form1()
            {
                InitializeComponent();
            }
    
    
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
                int x = 100;
                int y = 20;
    
                try
                {
    
                    for (int i = 0; i < RadioButtons.Length; i++)
                    {
                        RadioButtons[i] = new System.Windows.Forms.RadioButton();
                        RadioButtons[i].AutoSize = true;
                        RadioButtons[i].Location = new System.Drawing.Point(x, y);
                        RadioButtons[i].Name = "radioButton" + i;
                        RadioButtons[i].Size = new System.Drawing.Size(85, 17);
                        RadioButtons[i].TabIndex = 8;
                        RadioButtons[i].TabStop = true;
                        RadioButtons[i].Text = "radioButton" + i;
                        RadioButtons[i].Tag = i.ToString();
                        RadioButtons[i].UseVisualStyleBackColor = true;
                        Controls.Add(RadioButtons[i]);
                        RadioButtons[i].CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged);
                
                        y = y + 24;
                    }
    
                    x = 300;
                    y = 20;
    
                    for (int i = 0; i < Buttons.Length ; i++)
                    {
                        Buttons[i] = new System.Windows.Forms.Button();
                        Buttons[i].Location = new System.Drawing.Point(x, y);
                        Buttons[i].Name = "button" + i;
                        Buttons[i].Size = new System.Drawing.Size(86, 22);
                        Buttons[i].TabIndex = 3;
                        Buttons[i].Text = "button" + i;
                        Buttons[i].UseVisualStyleBackColor = true;
                        Buttons[i].Tag = i.ToString();
                        Controls.Add(Buttons[i]);
                        Buttons[i].Click += new System.EventHandler(this.button_Click);
                        y = y + 24;
                    }
                }
    
                catch
                
                {
                
                }
            }
    
            private void radioButton_CheckedChanged(object sender, EventArgs e)
            {
                
                try
                {
    
                    RadioButton myNewType = default(RadioButton);
                    myNewType = (RadioButton)sender;
                    label1.Text = myNewType.Tag.ToString();
                   // add your code here to change text, labels
    
                }
                catch (Exception ex)
                {
    
                    
                }
            }
    
            private void button_Click(object sender, EventArgs e)
            {
                try
                {
    
                    Button myNewType = default(Button);
                    myNewType = (Button)sender;
                    label2.Text = myNewType.Tag.ToString();
                   // add your code here to change text, labels
    
                }
                catch (Exception ex)
                {
    
    
                }
            }
    
     
        }

    Curt

  8. #8
    Join Date
    Jan 2013
    Posts
    9

    Re: Radiobuttons

    Hi, thanks for the replies.

    Code:
    isn't valid code syntax. Perhaps this is a typo in your post, but if it isn't you might want review C# syntax basics.
    Yes that's a typo.

    Code:
    Also, try to be clearer on what you are actually trying to do. I'm guessing that you want to enable/disable some buttons based which radio button is selected, but I'm not sure from your description.
    That's what I was trying to do. I solved the problem by using 'button.Enabled = false;' as shown below:

    Code:
            private void radioButton1_CheckedChanged(object sender, EventArgs e)
            {         
                button1.Enabled = true;
                button2.Enabled = true;
                button3.Enabled = true;
    
                button4.Enabled = false;
                button5.Enabled = false;
                button6.Enabled = false;
            }
    
            private void radioButton2_CheckedChanged(object sender, EventArgs e)
            {         
                button1.Enabled = false;
                button2.Enabled = false;
                button3.Enabled = false;
    
                button4.Enabled = true;
                button5.Enabled = true;
                button6.Enabled = true;
            }

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,477

    Re: Radiobuttons

    Quote Originally Posted by Dritech View Post
    That's what I was trying to do. I solved the problem by using 'button.Enabled = false;' as shown below:

    Code:
            private void radioButton1_CheckedChanged(object sender, EventArgs e)
            {         
                button1.Enabled = true;
                button2.Enabled = true;
                button3.Enabled = true;
    
                button4.Enabled = false;
                button5.Enabled = false;
                button6.Enabled = false;
            }
    
            private void radioButton2_CheckedChanged(object sender, EventArgs e)
            {         
                button1.Enabled = false;
                button2.Enabled = false;
                button3.Enabled = false;
    
                button4.Enabled = true;
                button5.Enabled = true;
                button6.Enabled = true;
            }
    That's one approach but it doesn't scale well. Consider what would happen if you had two groups of 5 radio buttons managing the enable/disable state of 10 buttons each?

    Check out the two sample code projects in the link I posted and try to understand those approaches. Both eliminate the need to create check changed handlers for every radio button which should result in smaller, more maintainabile code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center