Csharp/CSharp Tutorial/GUI Windows Forms/RadioButton — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:15, 26 мая 2010

Color Radio Buttons

using System;
using System.Drawing;
using System.Windows.Forms;
class ColorRadioButtons : Form
{
    public static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new ColorRadioButtons());
    }
    public ColorRadioButtons()
    {
        Color[] aclr = { Color.Red, Color.Orange, Color.Yellow};
        int y = Font.Height;
        foreach (Color clr in aclr)
        {
            RadioButton radio = new RadioButton();
            radio.Parent = this;
            radio.Location = new Point(Font.Height, y);
            radio.Text = clr.Name;
            radio.Tag = clr;
            radio.CheckedChanged += RadioButtonOnCheckedChanged;
            y += radio.Height;
        }
    }
    void RadioButtonOnCheckedChanged(object objSrc, EventArgs args)
    {
        RadioButton radio = objSrc as RadioButton;
        BackColor = (Color)radio.Tag;
    }
}

Put RadioButton to panel group

using System;
using System.ruponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Text;
class MyDialog : System.Windows.Forms.Form
{
  private Button okButton;
  private Button cancelButton;
  private CheckBox checkbox;
  private GroupBox radiogroup;
  private RadioButton radio1,radio2,radio3;
  public int Radio;
  public bool Check {
    get { return checkbox.Checked; }
    set { checkbox.Checked = value;}
    }
  void OnRadio(Object sender,EventArgs e)
  {
    int n=0;
    foreach(Object o in radiogroup.Controls)
    {
      if(o is RadioButton)
      {
        RadioButton r=(RadioButton)o;
        if(r.Checked)
          Radio=n;
        n++;
      }
    }
  }
  public MyDialog()
  {
    Size = new Size(400,300);
    FormBorderStyle = FormBorderStyle.FixedDialog;
    Text = "Dialog test";
    okButton = new Button();    
    okButton.DialogResult = DialogResult.OK;
    okButton.Location = new Point(20,230);
    okButton.Size = new Size(80,25);
    okButton.Text = "OK";
    Controls.Add(okButton);
    cancelButton = new Button();
    cancelButton.Location = new Point(300,230);
    cancelButton.Size = new Size(80,25);
    cancelButton.Text = "Cancel";
    cancelButton.DialogResult = DialogResult.Cancel;
    Controls.Add(cancelButton);
    checkbox = new CheckBox();
    checkbox.Location = new Point(20,30);
    checkbox.Size = new Size(300,25);
    checkbox.Text = "CheckBox";
    Controls.Add(checkbox);
    radiogroup = new GroupBox();
    radiogroup.Text = "Radio Buttons";
    radiogroup.Location = new Point(10,60);
    radiogroup.Size = new Size(380,110);
    Controls.Add(radiogroup);
    radio1 = new RadioButton();
    radio1.Location = new Point(10,15); 
    radio1.Size = new Size(360,25);
    radio1.Click += new EventHandler(OnRadio);
    radio1.Text = "Radio Button #1";
    radiogroup.Controls.Add(radio1);

    radio2 = new RadioButton();
    radio2.Location = new Point(10,40); 
    radio2.Size = new Size(360,25);
    radio2.Click += new EventHandler(OnRadio);
    radio2.Text = "Radio Button #2";
    radiogroup.Controls.Add(radio2);

    radio3 = new RadioButton();
    radio3.Location = new Point(10,70); 
    radio3.Size = new Size(360,25);
    radio3.Click += new EventHandler(OnRadio);
    radio3.Text = "Radio Button #3";
    radiogroup.Controls.Add(radio3);
  }
}
public class CustomDialogTest{
  public static void Main()
  {
    MyDialog dlg = new MyDialog();
    DialogResult r=dlg.ShowDialog();
    
      Console.WriteLine(dlg.Radio);
      Console.WriteLine(dlg.Check);
    
  }
}

RadioButton Click Event

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public partial class Form1 : Form
{
    string ticketClass;
    
    public Form1()
    {
        InitializeComponent();
    }
    private void okButton_Click(object sender, EventArgs e)
    {
        MessageBox.Show(ticketClass);
    }
    private void TicketTypeChanged(object sender, EventArgs e)
    {
        RadioButton button = (RadioButton)sender;
        if (button.Checked)
            ticketClass = button.Text;
    }
}
partial class Form1
{
    private void InitializeComponent()
    {
        this.label1 = new System.Windows.Forms.Label();
        this.economyRadio = new System.Windows.Forms.RadioButton();
        this.premiumRadio = new System.Windows.Forms.RadioButton();
        this.businessRadio = new System.Windows.Forms.RadioButton();
        this.upperRadio = new System.Windows.Forms.RadioButton();
        this.firstRadio = new System.Windows.Forms.RadioButton();
        this.okButton = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(12, 9);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(168, 13);
        this.label1.TabIndex = 0;
        this.label1.Text = "Choose the cabin class you require";
        // 
        // economyRadio
        // 
        this.economyRadio.AutoSize = true;
        this.economyRadio.Location = new System.Drawing.Point(46, 44);
        this.economyRadio.Name = "economyRadio";
        this.economyRadio.Size = new System.Drawing.Size(65, 17);
        this.economyRadio.TabIndex = 1;
        this.economyRadio.Text = "Economy";
        this.economyRadio.CheckedChanged += new System.EventHandler(this.TicketTypeChanged);
        // 
        // premiumRadio
        // 
        this.premiumRadio.AutoSize = true;
        this.premiumRadio.Location = new System.Drawing.Point(46, 68);
        this.premiumRadio.Name = "premiumRadio";
        this.premiumRadio.Size = new System.Drawing.Size(108, 17);
        this.premiumRadio.TabIndex = 2;
        this.premiumRadio.Text = "Premium Economy";
        this.premiumRadio.CheckedChanged += new System.EventHandler(this.TicketTypeChanged);
        // 
        // businessRadio
        // 
        this.businessRadio.AutoSize = true;
        this.businessRadio.Location = new System.Drawing.Point(46, 92);
        this.businessRadio.Name = "businessRadio";
        this.businessRadio.Size = new System.Drawing.Size(90, 17);
        this.businessRadio.TabIndex = 3;
        this.businessRadio.Text = "Business class";
        this.businessRadio.CheckedChanged += new System.EventHandler(this.TicketTypeChanged);
        // 
        // upperRadio
        // 
        this.upperRadio.AutoSize = true;
        this.upperRadio.Location = new System.Drawing.Point(46, 115);
        this.upperRadio.Name = "upperRadio";
        this.upperRadio.Size = new System.Drawing.Size(123, 17);
        this.upperRadio.TabIndex = 4;
        this.upperRadio.Text = "Upper Business Class";
        this.upperRadio.CheckedChanged += new System.EventHandler(this.TicketTypeChanged);
        // 
        // firstRadio
        // 
        this.firstRadio.AutoSize = true;
        this.firstRadio.Location = new System.Drawing.Point(46, 139);
        this.firstRadio.Name = "firstRadio";
        this.firstRadio.Size = new System.Drawing.Size(67, 17);
        this.firstRadio.TabIndex = 5;
        this.firstRadio.Text = "First class";
        this.firstRadio.CheckedChanged += new System.EventHandler(this.TicketTypeChanged);
        // 
        // okButton
        // 
        this.okButton.Location = new System.Drawing.Point(212, 194);
        this.okButton.Name = "okButton";
        this.okButton.Size = new System.Drawing.Size(75, 23);
        this.okButton.TabIndex = 6;
        this.okButton.Text = "OK";
        this.okButton.Click += new System.EventHandler(this.okButton_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(299, 229);
        this.Controls.Add(this.okButton);
        this.Controls.Add(this.firstRadio);
        this.Controls.Add(this.upperRadio);
        this.Controls.Add(this.businessRadio);
        this.Controls.Add(this.premiumRadio);
        this.Controls.Add(this.economyRadio);
        this.Controls.Add(this.label1);
        this.Name = "Form1";
        this.Text = "Flight Booker";
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.RadioButton economyRadio;
    private System.Windows.Forms.RadioButton premiumRadio;
    private System.Windows.Forms.RadioButton businessRadio;
    private System.Windows.Forms.RadioButton upperRadio;
    private System.Windows.Forms.RadioButton firstRadio;
    private System.Windows.Forms.Button okButton;
}
public class RadioButtonClickEvent
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}

RadioButton Image

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.RadioButton radioButton1;
    private System.Windows.Forms.RadioButton radioButton2;
    private System.Windows.Forms.RadioButton radioButton3;
    public Form1() {
        this.groupBox1 = new System.Windows.Forms.GroupBox();
        this.radioButton3 = new System.Windows.Forms.RadioButton();
        this.radioButton2 = new System.Windows.Forms.RadioButton();
        this.radioButton1 = new System.Windows.Forms.RadioButton();
        this.groupBox1.SuspendLayout();
        this.SuspendLayout();
        this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.radioButton3,
this.radioButton2,
this.radioButton1});
        this.groupBox1.Location = new System.Drawing.Point(8, 16);
        this.groupBox1.Size = new System.Drawing.Size(160, 120);
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "Group 1";
        this.radioButton3.Appearance = System.Windows.Forms.Appearance.Button;
        this.radioButton3.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(255)), ((System.Byte)(255)));
        this.radioButton3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.radioButton3.Location = new System.Drawing.Point(16, 88);
        this.radioButton3.Name = "radioButton3";
        this.radioButton3.Size = new System.Drawing.Size(120, 24);
        this.radioButton3.TabIndex = 2;
        this.radioButton3.Text = "Option3";
        this.radioButton2.Appearance = System.Windows.Forms.Appearance.Button;
        this.radioButton2.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192)));
        this.radioButton2.Location = new System.Drawing.Point(16, 56);
        this.radioButton2.Size = new System.Drawing.Size(120, 24);
        this.radioButton2.Text = "Option2";
        this.radioButton2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
        this.radioButton1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(192)), ((System.Byte)(192)));
        this.radioButton1.Location = new System.Drawing.Point(16, 24);
        this.radioButton1.Size = new System.Drawing.Size(120, 24);
        this.radioButton1.Text = "Option1";
        this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(184, 149);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.groupBox1});
        this.Text = "Radio Button";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.groupBox1.ResumeLayout(false);
        this.ResumeLayout(false);
    }
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    private void Form1_Load(object sender, System.EventArgs e) {
        Image img = Image.FromFile("EYE.ICO");
        radioButton1.Image = img;
        radioButton1.ImageAlign = ContentAlignment.MiddleRight;
        img = Image.FromFile("WRENCH.ICO");
        radioButton2.Image = img;
        radioButton2.ImageAlign = ContentAlignment.MiddleLeft;
    }
    private void radioButton1_CheckedChanged(object sender, System.EventArgs e) {
        if (radioButton1.Checked)
            MessageBox.Show("Checked!");
        else
            MessageBox.Show("Not checked!");
    }
}

Set DialogResult.OK/DialogResult.Cancel to OK/Cancel button

using System;
using System.ruponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Text;
class DialogValid : System.Windows.Forms.Form
{
    private Button okButton;
    private Button cancelButton;
    private NumericUpDown num;
    public decimal Num {
        get { return num.Value; }
        set { num.Value = value;    }
    }
    void OnValidating(Object sender, CancelEventArgs e)
    {
        MessageBox.Show("NumericUpDown is validating");
    } 
    void OnValid(Object sender,EventArgs e)
    {
        MessageBox.Show("NumericUpDown is valid");
    } 
    public DialogValid()
    {
        Size = new Size(400,100);
        FormBorderStyle = FormBorderStyle.FixedDialog;
        Text = "Dialog test";
        okButton = new Button();        
        okButton.DialogResult = DialogResult.OK;
        okButton.Location = new Point(20,28);
        okButton.Size = new Size(80,25);
        okButton.Text = "OK";
        Controls.Add(okButton);
        cancelButton = new Button();
        cancelButton.Location = new Point(300,28);
        cancelButton.Size = new Size(80,25);
        cancelButton.Text = "Cancel";
        cancelButton.DialogResult = DialogResult.Cancel;
        Controls.Add(cancelButton);
        Label l = new Label();
        l.Text = "NumericUpDown";
        l.Location = new Point(20,5);
        l.Size = new Size(120,25);
        Controls.Add(l);
        num = new NumericUpDown();
        num.Location = new Point(140,5);
        num.Size = new Size(80,25);
        num.Minimum = (decimal)10.0;
        num.Maximum = (decimal)100.0;
        num.Value = (decimal)10.0;
        
        num.Validating+=new CancelEventHandler(OnValidating);
        num.Validated+=new EventHandler(OnValid);
        Controls.Add(num);
    }
}
public class NumericUpDownValidationEvent{
    public static void Main()
    {
        DialogValid dlg = new DialogValid();
        DialogResult r = dlg.ShowDialog();
        
        Console.WriteLine(dlg.Num);
    }
}