• [C#][SAMPLE][CODE][Control]RadioBox、CheckBox和Validating事件的相关处理


    [示例出处]:本示例来自《C#入门经典》第三版中文版,P349-P353
    [示例涉及]:
    1、RadioBox、CheckBox控件的基本使用
    2、Validating事件的使用(同[C#][SAMPLE][CODE][Control]TextBox和Validating事件的相关处理 )
    3、多委托处理同一事件方法(同[C#][SAMPLE][CODE][Control]TextBox和Validating事件的相关处理 )
    [示例代码]:2文件(其余默认)

    Form1.Designer.cs
      1namespace WA_TextBoxTest
      2{
      3    partial class Form1
      4    {
      5        /// <summary>
      6        /// 必需的设计器变量。
      7        /// </summary>

      8        private System.ComponentModel.IContainer components = null;
      9
     10        /// <summary>
     11        /// 清理所有正在使用的资源。
     12        /// </summary>
     13        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>

     14        protected override void Dispose(bool disposing)
     15        {
     16            if (disposing && (components != null))
     17            {
     18                components.Dispose();
     19            }

     20            base.Dispose(disposing);
     21        }

     22
     23        Windows 窗体设计器生成的代码
    223
    224        private System.Windows.Forms.Label label1;
    225        private System.Windows.Forms.Label label2;
    226        private System.Windows.Forms.TextBox textBoxName;
    227        private System.Windows.Forms.TextBox textBoxAddress;
    228        private System.Windows.Forms.TextBox textBoxAge;
    229        private System.Windows.Forms.TextBox textBoxOutput;
    230        private System.Windows.Forms.Label label4;
    231        private System.Windows.Forms.Label label5;
    232        private System.Windows.Forms.Button buttonOK;
    233        private System.Windows.Forms.Button buttonHelp;
    234        private System.Windows.Forms.CheckBox checkBoxProgrammer;
    235        private System.Windows.Forms.GroupBox groupBoxSex;
    236        private System.Windows.Forms.RadioButton radioButtonMale;
    237        private System.Windows.Forms.RadioButton radioButtonFemale;
    238    }

    239}

    240
    241

    Form1.cs
      1using System;
      2using System.Collections.Generic;
      3using System.ComponentModel;
      4using System.Data;
      5using System.Drawing;
      6using System.Text;
      7using System.Windows.Forms;
      8
      9namespace WA_TextBoxTest
     10{
     11    public partial class Form1 : Form
     12    {
     13        public Form1()
     14        {
     15            InitializeComponent();
     16            this.buttonOK.Enabled = false;
     17
     18            //Tag values for testing if the data is valid.
     19            this.textBoxName.Tag = false;
     20            this.textBoxAddress.Tag = false;
     21            this.textBoxAge.Tag = false;
     22
     23            //订阅事件Subscriptions to events
     24            this.textBoxName.Validating += new CancelEventHandler(this.textBoxEmpty_Validating);
     25            this.textBoxAddress.Validating += new CancelEventHandler(this.textBoxEmpty_Validating);
     26            this.textBoxAge.Validating += new CancelEventHandler(this.textBoxEmpty_Validating);
     27
     28            //当控件中文本发生改变,就激发TextChanged事件。
     29            this.textBoxName.TextChanged +=new EventHandler(textBox_TextChanged);
     30            this.textBoxAddress.TextChanged+=new EventHandler(textBox_TextChanged);
     31            this.textBoxAge.TextChanged += new EventHandler(textBox_TextChanged);
     32        }

     33
     34        private void buttonOK_Click(object sender, EventArgs e)
     35        {
     36            //No testing for invalid values are made ,as that should not be necessary
     37            string output;
     38            
     39            //Concatenate the text values of for TextBoxes.
     40            output = "Name:" + this.textBoxName.Text + "\r\n";
     41            output += "Address:" + this.textBoxAddress.Text + "\r\n";
     42            output += "Occupation:" + (string)(this.checkBoxProgrammer.Checked ?
     43                "Programmer" : "Not a programmer"+ "\r\n";    //此句等价于下面这句
     44            output += "Occupation:" + (string)(this.checkBoxProgrammer.Checked ?
     45                this.checkBoxProgrammer.Text : ("Not a "+this.checkBoxProgrammer.Text)) + "\r\n";
     46            output += "Sex:" + (string)(this.radioButtonFemale.Checked ?
     47                "Female" : "Male"+ "\r\n";    //此句等价于下面这句
     48            output += "Sex:" + (string)(this.radioButtonFemale.Checked ?
     49                this.radioButtonFemale.Text : this.radioButtonMale.Text) + "\r\n";
     50            output += "Age:" + this.textBoxAge.Text;
     51 
     52            //Insert the new text.
     53            this.textBoxOutput.Text = output;
     54        }

     55
     56        private void buttonHelp_Click(object sender, EventArgs e)
     57        {
     58            //Write a short description of each TextBox in the Output TextBox.
     59            string output;
     60
     61            output = "Name=Your name\r\n";
     62            output += "Address=Your address\r\n";
     63            output += "Programmer=Check 'Programmer' if you are a programmer\r\n";
     64            output += "Sex=Choose your sex\r\n";
     65            output += "Age=Your age";
     66
     67            //Insert the new text.
     68            this.textBoxOutput.Text = output;
     69        }

     70
     71        private void textBoxEmpty_Validating(object sender, CancelEventArgs e)
     72        {
     73            //我们知道这个sender是一个对话框,所以我们将他们强制转换为TextBox
     74            TextBox tb = (TextBox)sender;
     75
     76            //如果对话框是空的话我们设置TextBox背景色为红色来象征问题。
     77            //如果控件有valid信息,我们就使用控件的Tag值来指出。
     78            if (tb.Text.Length == 0)
     79            {
     80                tb.BackColor = Color.Red;
     81                tb.Tag = false;
     82                //在这个例子中我们不想取消further processing
     83                //但是如果我们想要这么做的话,我们只需要添加以下一行:
     84                //e.Cancel=true;
     85            }

     86            else
     87            {
     88                this.BackColor = SystemColors.Window;
     89                tb.Tag = true;
     90            }

     91            //Finally ,we call ValidateOK which will set the value of the OK button.
     92            ValidateOK();
     93        }

     94
     95        private void textBoxAge_KeyPress(object sender, KeyPressEventArgs e)
     96        {
     97            if ((e.KeyChar < 48 || e.KeyChar > 57&& e.KeyChar != 8)
     98            {
     99                e.Handled = true;   //Remove the character
    100                //等于true告诉用户不应该对字符进行任何操作
    101            }

    102        }

    103
    104        private void textBox_TextChanged(object sender, EventArgs e)
    105        {
    106            //Cast the sender object to a TextBox
    107            TextBox tb = (TextBox)sender;
    108
    109            //Test if the data is valid and set the tag and back ground color accordingly.
    110            if (tb.Text.Length == 0)
    111            {
    112                tb.Tag = false;
    113                tb.BackColor = Color.Red;
    114            }

    115            else
    116            {
    117                tb.Tag = true;
    118                tb.BackColor = SystemColors.Window;
    119            }

    120
    121            //Call ValidateOK to set the OK button
    122            ValidateOK();
    123        }

    124
    125        private void ValidateOK()
    126        
    127            //Set the OK button to enabled if all the Tags are true
    128            this.buttonOK.Enabled = ((bool)(this.textBoxName.Tag) &&
    129                (bool)(this.textBoxAge.Tag) &&
    130                (bool)(this.textBoxAddress.Tag));
    131        }

    132    }

    133}

    [示例说明]:
    1、开发语言:C#
    2、开发环境:Visual Studio.Net 2005 Team suite
    3、开发模板:C#.net项目->Windows应用程序
  • 相关阅读:
    Web前端开发
    用javascript向一个网页连接接口发送请求,并接收该接口返回的json串
    如何在tomcat启动的时候运行一个Java类
    Linux永久挂载远程网络目录
    C/C++跨平台的的预编译宏
    利用http实现文件的上传和下载
    基于qml创建最简单的图像处理程序(1)-基于qml创建界面
    基于qml创建最简单的android机图像采集程序
    OpenCV相关网站推荐(Informative websites related to OpenCV)
    (GO_GTD_3)基于OpenCV和QT,建立Android图像处理程序
  • 原文地址:https://www.cnblogs.com/volnet/p/574437.html
Copyright © 2020-2023  润新知