Está en la página 1de 9

RadioButton (Clase)

Representa un control RadioButton de Windows Forms que se puede agregar a una hoja de
clculo de Microsoft Office Excel.
Jerarqua de herencia
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ButtonBase
System.Windows.Forms.RadioButton
Microsoft.Office.Tools.Excel.Controls.RadioButton
Espacio de nombres: Microsoft.Office.Tools.Excel.Controls
Ensamblado: Microsoft.Office.Tools.Excel.v4.0.Utilities (en
Microsoft.Office.Tools.Excel.v4.0.Utilities.dll)
Sintaxis
C#
VB
[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
public class RadioButton : RadioButton
Sintaxis
C#
VB
'Declaracin
<PermissionSetAttribute(SecurityAction.Demand, Name := "FullTrust")> _
Public Class RadioButton _
Inherits RadioButton

RadioButton (Constructor)
Inicializa una nueva instancia de la clase de RadioButton .
Espacio de nombres: System.Windows.Forms
Ensamblado: System.Windows.Forms (en System.Windows.Forms.dll)

Sintaxis
C#
public RadioButton()
VB
'Declaracin
Public Sub New
Comentarios
La vista predeterminada de RadioButton tiene su texto alineado a la derecha del botn y la
propiedad deAutoCheck se establece en true.
Ejemplos
El siguiente ejemplo de cdigo crea e inicializa RadioButton, proporciona el aspecto de un
botn de alternancia, establezca su propiedad de AutoCheck a false, y la agrega a Form.
C#
VB

Private Sub InitializeMyRadioButton()
' Create and initialize a new RadioButton.
Dim radioButton1 As New RadioButton()

' Make the radio button control appear as a toggle button.
radioButton1.Appearance = Appearance.Button

' Turn off the update of the display on the click of the control.
radioButton1.AutoCheck = False

' Add the radio button to the form.
Controls.Add(radioButton1)
End Sub

C#
VB

private void InitializeMyRadioButton()
{
// Create and initialize a new RadioButton.
RadioButton radioButton1 = new RadioButton();

// Make the radio button control appear as a toggle button.
radioButton1.Appearance = Appearance.Button;

// Turn off the update of the display on the click of the control.
radioButton1.AutoCheck = false;

// Add the radio button to the form.
Controls.Add(radioButton1);
}


Radiobutton en Visual C# 2010.En este tutorial breve ilustraremos con un ejemplo didctico y
sencillo de cmo se usa y para qu sirven los RADIOBUTTON una herramienta ms de Visual
C#, por cierto esta herramienta es muy til a la hora de hacer programas en los cuales
tenemos que hacer que se elijan opciones pero solo podemos elegir una opcin entre muchas
y no ambas a la vez solo una opcin quedando sin activacin las dems opciones.
Controlar que solo se active la opcin que hemos elegido desactivando automticamente las
dems opciones, cada vez que hacemos clic en una opcin; es bastante fcil.
Es especialmente para esto que esta creado los Radiobutton para gestionar este tipo de
selecciones, los Radiobutton nos gestionan automticamente la tarea de activar y desactivar
las opciones nosotros no tenemos que hacer ningn cdigo solo arrastrar esta herramienta al
formulario y listo lo que nosotros hacemos mediante cdigo es recuperar la opcin elegida
para poder procesarla en nuestro particular problema.
Para desarrollar lo anterior expuesto vmonos a la prctica creando un nuevo proyecto de
visual C# 2010 y empecemos a ilustrar el uso de los Radiobutton, las siguientes imgenes
ensean cmo crear un nuevo proyecto en visual C#:



Lo que vamos a hacer es un sistema pequeo que sea una simulacin de compra en la cual la
cajera pueda elegir si va a imprimir una factura, un recibo, o una proforma, pero solo podr
elegir una de las opciones mencionadas, para este ejemplo necesitamos tres Radiobutton y un
botn.
Has que tu formulario quede como la siguiente imagen con estas dos herramientas el
Radiobutton y botn:


Por defecto cuando arrastramos una herramienta al formulario este nos viene con un texto por
defecto, por ejemplo los Radiobutton vienen con el nombreradiobutton1, radiobutton2 y
radiobutton3, para cambiar ese texto que se mostrara en el formulario u dejarlo como se ve en
la imagen anterior hacemos clic en el radiobutton1 y despus nos vamos a sus propiedades y
escribimos el texto que queramos en la propiedad texto, como en la siguiente imagen, es lo
mismo para cualquier herramienta en visual C# 2010:

Ahora para entender cmo saber que Radiobutton se eligi entre los tres Radiobutton
simplemente lo hacemos con un cdigo sencillo, entonces para escribirlo vamos al diseo de
nuestro formulario y le hacemos doble clic en el botn nico que arrastramos y nos saldr su
funcin en el editor de cdigo:

Escribimos el siguiente cdigo entre las llaves:
if (radioButton1.Checked) {
MessageBox.Show("Elegiste: "+radioButton1.Text);
}

if (radioButton2.Checked)
{
MessageBox.Show("Elegiste: " + radioButton2.Text);
}

if (radioButton3.Checked)
{
MessageBox.Show("Elegiste: " + radioButton3.Text);
}
La siguiente imagen muestra el cdigo arriba escrito directamente en el Visual C# 2010:




The RadioButton control is used to provide a set of mutually exclusive options. The user can select one
radio button in a group. If you need to place more than one group of radio buttons in the same form, you
should place them in different container controls like a GroupBox control.
Let's create three radio buttons by dragging RadioButton controls from the Toolbox and dropping on the
form.

The Checked property of the radio button is used to set the state of a radio button. You can display text,
image or both on radio button control. You can also change the appearance of the radio button control by
using the Appearance property.
Properties of the RadioButton Control
The following are some of the commonly used properties of the RadioButton control:
S.N Property Description
1 Appearance
Gets or sets a value determining the appearance of the radio
button.
2 AutoCheck
Gets or sets a value indicating whether the Checked value and the
appearance of the control automatically change when the control is
clicked.
3 CheckAlign
Gets or sets the location of the check box portion of the radio
button.
4 Checked Gets or sets a value indicating whether the control is checked.
5 Text Gets or sets the caption for a radio button.
6 TabStop
Gets or sets a value indicating whether a user can give focus to the
RadioButton control using the TAB key.
Methods of the RadioButton Control
The following are some of the commonly used methods of the RadioButton control:
S.N Method Name & Description
1
PerformClick
Generates a Click event for the control, simulating a click by a user.
Events of the RadioButton Control
The following are some of the commonly used events of the RadioButton control:
S.N Event Description
1 AppearanceChanged
Occurs when the value of the Appearance property of the RadioButton
control is changed.
2 CheckedChanged
Occurs when the value of the Checked property of the RadioButton
control is changed.
Consult Microsoft documentation for detailed list of properties, methods and events of the RadioButton
control.
Example
In the following example, let us create two groups of radio buttons and use their CheckedChanged events
for changing the BackColor and ForeColor property of the form.

Let's double click on the radio buttons and put the follow code in the opened window.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
' Set the caption bar text of the form.
Me.Text = "tutorialspont.com"
End Sub

Private Sub RadioButton1_CheckedChanged(sender As Object, _
e As EventArgs) Handles RadioButton1.CheckedChanged
Me.BackColor = Color.Red
End Sub
Private Sub RadioButton2_CheckedChanged(sender As Object, _
e As EventArgs) Handles RadioButton2.CheckedChanged
Me.BackColor = Color.Green
End Sub
Private Sub RadioButton3_CheckedChanged(sender As Object, _
e As EventArgs) Handles RadioButton3.CheckedChanged
Me.BackColor = Color.Blue
End Sub

Private Sub RadioButton4_CheckedChanged(sender As Object, _
e As EventArgs) Handles RadioButton4.CheckedChanged
Me.ForeColor = Color.Black
End Sub
Private Sub RadioButton5_CheckedChanged(sender As Object, _
e As EventArgs) Handles RadioButton5.CheckedChanged
Me.ForeColor = Color.White
End Sub
Private Sub RadioButton6_CheckedChanged(sender As Object, _
e As EventArgs) Handles RadioButton6.CheckedChanged
Me.ForeColor = Color.Red
End Sub
End Class
When the above code is executed and run using Start button available at the Microsoft Visual Studio tool
bar, it will show the following window:


C# RadioButton Control
A radio button or option button enables the user to select a single option from a
group of choices when paired with other RadioButton controls. When a user clicks
on a radio button, it becomes checked, and all other radio buttons with same group
become unchecked

The RadioButton control can display text, an Image, or both. Use the Checked
property to get or set the state of a RadioButton.
radioButton1.Checked = true;
The radio button and the check box are used for different functions. Use a radio
button when you want the user to choose only one option. When you want the user
to choose all appropriate options, use a check box. Like check boxes, radio buttons
support a Checked property that indicates whether the radio button is selected.
Next : C# CheckBox Control



using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
radioButton1.Checked = true;
}

private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
MessageBox.Show ("You are selected Red !! ");
return;
}
else if (radioButton2.Checked == true)
{
MessageBox.Show("You are selected Blue !! ");
return;
}
else
{
MessageBox.Show("You are selected Green !! ");
return;
}
}
}
}

También podría gustarte