Acquiring User's Radio Button Selection using C#

Provided by the FMS Development Team

C# Example

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

 

namespace RadioButtons

{

   ///

   /// Summary description for Form1.

   ///

   public class Form1 : System.Windows.Forms.Form

   {

      internal System.Windows.Forms.Button btnColorSelection;

      internal System.Windows.Forms.GroupBox gpbColors;

      internal System.Windows.Forms.RadioButton colorGreen;

      internal System.Windows.Forms.RadioButton colorBlue;

      internal System.Windows.Forms.RadioButton colorRed;

      ///

      /// Required designer variable.

      /// 

      private System.ComponentModel.Container components = null;

 

      public enum ColorSelections

      {

         Red = 1,

         Blue = 2,

         Green =3

      }

 

      int mColorSelectedByInteger;

      ColorSelections mColorSelectedByEnumeration;

 

      public Form1()

      {

         //

         // Required for Windows Form Designer support

         //

         InitializeComponent();

         //

         // TODO: Add any constructor code after InitializeComponent call

         //

      }

 

      ///

      /// Clean up any resources being used.

      ///

      protected override void Dispose( bool disposing )

      {

         if( disposing )

         {

            if (components != null)

            {

               components.Dispose();

            }

         }

 

         base.Dispose( disposing );

      }

 

      #region Windows Form Designer generated code

      ///

      /// Required method for Designer support - do not modify

      /// the contents of this method with the code editor.

      ///

      private void InitializeComponent()

      {

         this.btnColorSelection = new System.Windows.Forms.Button();

         this.gpbColors = new System.Windows.Forms.GroupBox();

         this.colorGreen = new System.Windows.Forms.RadioButton();

         this.colorBlue = new System.Windows.Forms.RadioButton();

         this.colorRed = new System.Windows.Forms.RadioButton();

         this.gpbColors.SuspendLayout();

         this.SuspendLayout();

         //

         // btnColorSelection

         //

         this.btnColorSelection.Location = new System.Drawing.Point(16, 184);

         this.btnColorSelection.Name = "btnColorSelection";

         this.btnColorSelection.Size = new System.Drawing.Size(144, 24);

         this.btnColorSelection.TabIndex = 3;

         this.btnColorSelection.Text = "Color Selection";

         this.btnColorSelection.Click += new System.EventHandler(this.btnColorSelection_Click);

         //

         // gpbColors

         //

         this.gpbColors.Controls.Add(this.colorGreen);

         this.gpbColors.Controls.Add(this.colorBlue);

         this.gpbColors.Controls.Add(this.colorRed);

         this.gpbColors.Location = new System.Drawing.Point(16, 16);

         this.gpbColors.Name = "gpbColors";

         this.gpbColors.Size = new System.Drawing.Size(144, 152);

         this.gpbColors.TabIndex = 2;

         this.gpbColors.TabStop = false;

         this.gpbColors.Text = "Select a color";

         //

         // colorGreen

         //

         this.colorGreen.BackColor = System.Drawing.SystemColors.Control;

         this.colorGreen.Location = new System.Drawing.Point(40, 112);

         this.colorGreen.Name = "colorGreen";

         this.colorGreen.Size = new System.Drawing.Size(56, 24);

         this.colorGreen.TabIndex = 2;

         this.colorGreen.Tag = "3";

         this.colorGreen.Text = "Green";

         this.colorGreen.CheckedChanged += new System.EventHandler(this.colorRed_CheckedChanged);

         //

         // colorBlue

         //

         this.colorBlue.BackColor = System.Drawing.SystemColors.Control;

         this.colorBlue.Location = new System.Drawing.Point(40, 68);

         this.colorBlue.Name = "colorBlue";

         this.colorBlue.Size = new System.Drawing.Size(56, 24);

         this.colorBlue.TabIndex = 1;

         this.colorBlue.Tag = "2";

         this.colorBlue.Text = "Blue";

         this.colorBlue.CheckedChanged += new System.EventHandler(this.colorRed_CheckedChanged);

         //

         // colorRed

         //

         this.colorRed.BackColor = System.Drawing.SystemColors.Control;

         this.colorRed.Location = new System.Drawing.Point(40, 24);

         this.colorRed.Name = "colorRed";

         this.colorRed.Size = new System.Drawing.Size(56, 24);

         this.colorRed.TabIndex = 0;

         this.colorRed.Tag = "1";

         this.colorRed.Text = "Red";

         this.colorRed.CheckedChanged += new System.EventHandler(this.colorRed_CheckedChanged);

         //

         // Form1

         //

         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

         this.ClientSize = new System.Drawing.Size(176, 222);

         this.Controls.Add(this.btnColorSelection);

         this.Controls.Add(this.gpbColors);

         this.Name = "Form1";

         this.Text = "Colors";

         this.gpbColors.ResumeLayout(false);

         this.ResumeLayout(false);

      }

      #endregion

 

      ///

      /// The main entry point for the application.

      [STAThread] static void Main()

      {

         Application.Run(new Form1());

      }

  

      private void btnColorSelection_Click(object sender, System.EventArgs e)

      {

         MessageBox.Show(mColorSelectedByInteger.ToString(), "By Number");

         MessageBox.Show(mColorSelectedByEnumeration.ToString(), "By Enumeration");

      }

  

      private void colorRed_CheckedChanged(object sender, System.EventArgs e)

      {

         mColorSelectedByInteger = int.Parse((string)(((RadioButton)sender).Tag));

         mColorSelectedByEnumeration = (ColorSelections)int.Parse((string)(((RadioButton)sender).Tag));

      }

   }

}

 


Additional Resources

 

 

Thank you! Thank you! I just finished reading this document, which was part of a link in the recent Buzz newsletter. I have printed it for others to read, especially those skeptical on the powers of Access and its capabilities.

Darren D.


View all FMS products for Microsoft Access All Our Microsoft Access Products

 

 

Free Product Catalog from FMS