WinForm常规控件的使用(一)

WinForm常规控件的使用(一)

常规控件的通用属性

常规控件一般都包含由如下表中的通用属性。

属性名 作用
Name 控件的名称,区别不同控件唯一标志
Text 控件上显示的文本
Font 控件里字体的大小、字号、字体、下划线、加粗等
ForeColor 控件里字体的颜色,默认黑色
BackColor 控件背景颜色
Location 控件在窗体的位置
Size 控件的大小
Visible 是否隐藏控件
Enabled 控件是否可用
Anchor 控件的大小随窗体大小4个方向变化而变化
Dock 控件在窗体停靠位置
Tag 可以用来灵活存取需要的对象数据

几种最常用的控件

WinForm窗体设计最常用的控件如下表所示,其中常用属性为通用属性以外的常用属性。

控件名 作用 常用属性 常用事件
Label 专门用来在窗体上写文本使用,通常与文本框成对使用
LinkLabel 一般用于响应鼠标单击事件访问超链接标签对应的网址 Clicked
TextBox 给用户提供输入信息的控件 MaxLength:在文本框中最多输入的文本的字符个数
WordWrap:文本框中的文本是否自动换行
PasswordChar:将文本框中出现的字符使用指定的字符替换,通常会使用“*”字符
Multiline:指定文本框是否为多行文本框
ReadOnly:指定文本框中的文本是否可以更改
TextChanged
Button 用于提交页面内容或者确认某种操作 Clicked
RadioButton 多个 RadioButton 控件为一组,这一组内的RadioButton 控件只能有一个被选中 Checked:单选按钮默认是否被选中
TextAlign:文本对齐方向
Clicked
Checked
CheckBox 与单选按钮相对应的,用于选择多个选项的操作 Checked:单选按钮默认是否被选中
TextAlign:文本对齐方向
Clicked
Checked
ComboBox 用于选择所需的选项,使用组合框可以有效地避免非法值的输入 DropDownStyle:获取或设置组合框的外观,如果值为 Simple,同时显示文本框和列表框,并且文本框可以编辑;如果值为 DropDown,则只显示文本框,通过鼠标或键盘的单击事件展开文本框,并且文本框可以编辑;如果值为 DropDownList,显示效果与 DropDown 值一样,但文本框不可编辑。默认情况下为 DropDown

Items:获取或设置组合框中的值
Sorted: 指定是否对组合框列表中的项进行排序
MaxDropDownltems:获取或设置组合框中最多显示的项数
SelectedlndexChanged

最常用控件使用实例

前台设计代码:

namespace WinFormGernalControls
{
    partial class Order
    {
        /// 
        /// 必需的设计器变量。
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// 清理所有正在使用的资源。
        /// 
        /// 如果应释放托管资源,为 true;否则为 false。
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// 
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// 
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Order));
            this.lblMeal = new System.Windows.Forms.Label();
            this.lblSide = new System.Windows.Forms.Label();
            this.lblDrink = new System.Windows.Forms.Label();
            this.lblAddress = new System.Windows.Forms.Label();
            this.lblTelephone = new System.Windows.Forms.Label();
            this.cbxMeal = new System.Windows.Forms.ComboBox();
            this.rbtnChips = new System.Windows.Forms.RadioButton();
            this.rbtnPotato = new System.Windows.Forms.RadioButton();
            this.rbtnChicken = new System.Windows.Forms.RadioButton();
            this.lblTitle = new System.Windows.Forms.Label();
            this.chkCoco = new System.Windows.Forms.CheckBox();
            this.chkMilk = new System.Windows.Forms.CheckBox();
            this.chkCoffee = new System.Windows.Forms.CheckBox();
            this.txtAddress = new System.Windows.Forms.TextBox();
            this.txtTel = new System.Windows.Forms.TextBox();
            this.btnCommit = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // lblMeal
            // 
            this.lblMeal.AutoSize = true;
            this.lblMeal.BackColor = System.Drawing.Color.Transparent;
            this.lblMeal.ForeColor = System.Drawing.Color.White;
            this.lblMeal.Location = new System.Drawing.Point(70, 76);
            this.lblMeal.Name = "lblMeal";
            this.lblMeal.Size = new System.Drawing.Size(29, 12);
            this.lblMeal.TabIndex = 0;
            this.lblMeal.Text = "Meal";
            // 
            // lblSide
            // 
            this.lblSide.AutoSize = true;
            this.lblSide.BackColor = System.Drawing.Color.Transparent;
            this.lblSide.ForeColor = System.Drawing.Color.White;
            this.lblSide.Location = new System.Drawing.Point(70, 114);
            this.lblSide.Name = "lblSide";
            this.lblSide.Size = new System.Drawing.Size(59, 12);
            this.lblSide.TabIndex = 0;
            this.lblSide.Text = "Side Item";
            // 
            // lblDrink
            // 
            this.lblDrink.AutoSize = true;
            this.lblDrink.BackColor = System.Drawing.Color.Transparent;
            this.lblDrink.ForeColor = System.Drawing.Color.White;
            this.lblDrink.Location = new System.Drawing.Point(70, 143);
            this.lblDrink.Name = "lblDrink";
            this.lblDrink.Size = new System.Drawing.Size(35, 12);
            this.lblDrink.TabIndex = 0;
            this.lblDrink.Text = "Drink";
            // 
            // lblAddress
            // 
            this.lblAddress.AutoSize = true;
            this.lblAddress.BackColor = System.Drawing.Color.Transparent;
            this.lblAddress.ForeColor = System.Drawing.Color.White;
            this.lblAddress.Location = new System.Drawing.Point(70, 174);
            this.lblAddress.Name = "lblAddress";
            this.lblAddress.Size = new System.Drawing.Size(47, 12);
            this.lblAddress.TabIndex = 0;
            this.lblAddress.Text = "Address";
            // 
            // lblTelephone
            // 
            this.lblTelephone.AutoSize = true;
            this.lblTelephone.BackColor = System.Drawing.Color.Transparent;
            this.lblTelephone.ForeColor = System.Drawing.Color.White;
            this.lblTelephone.Location = new System.Drawing.Point(70, 241);
            this.lblTelephone.Name = "lblTelephone";
            this.lblTelephone.Size = new System.Drawing.Size(59, 12);
            this.lblTelephone.TabIndex = 0;
            this.lblTelephone.Text = "Telephone";
            // 
            // cbxMeal
            // 
            this.cbxMeal.FormattingEnabled = true;
            this.cbxMeal.Items.AddRange(new object[] {
            "New Orleans Roasted Burger",
            "Zinger Burger",
            "Dragon Twister",
            "Mini Burger"});
            this.cbxMeal.Location = new System.Drawing.Point(157, 73);
            this.cbxMeal.Name = "cbxMeal";
            this.cbxMeal.Size = new System.Drawing.Size(327, 20);
            this.cbxMeal.TabIndex = 1;
            // 
            // rbtnChips
            // 
            this.rbtnChips.AutoSize = true;
            this.rbtnChips.BackColor = System.Drawing.Color.Transparent;
            this.rbtnChips.Checked = true;
            this.rbtnChips.ForeColor = System.Drawing.Color.White;
            this.rbtnChips.Location = new System.Drawing.Point(157, 112);
            this.rbtnChips.Name = "rbtnChips";
            this.rbtnChips.Size = new System.Drawing.Size(95, 16);
            this.rbtnChips.TabIndex = 2;
            this.rbtnChips.TabStop = true;
            this.rbtnChips.Text = "French fries";
            this.rbtnChips.UseVisualStyleBackColor = false;
            // 
            // rbtnPotato
            // 
            this.rbtnPotato.AutoSize = true;
            this.rbtnPotato.BackColor = System.Drawing.Color.Transparent;
            this.rbtnPotato.ForeColor = System.Drawing.Color.White;
            this.rbtnPotato.Location = new System.Drawing.Point(258, 112);
            this.rbtnPotato.Name = "rbtnPotato";
            this.rbtnPotato.Size = new System.Drawing.Size(101, 16);
            this.rbtnPotato.TabIndex = 2;
            this.rbtnPotato.Text = "Mashed Potato";
            this.rbtnPotato.UseVisualStyleBackColor = false;
            // 
            // rbtnChicken
            // 
            this.rbtnChicken.AutoSize = true;
            this.rbtnChicken.BackColor = System.Drawing.Color.Transparent;
            this.rbtnChicken.ForeColor = System.Drawing.Color.White;
            this.rbtnChicken.Location = new System.Drawing.Point(365, 112);
            this.rbtnChicken.Name = "rbtnChicken";
            this.rbtnChicken.Size = new System.Drawing.Size(119, 16);
            this.rbtnChicken.TabIndex = 2;
            this.rbtnChicken.Text = "Pop Corn Chicken";
            this.rbtnChicken.UseVisualStyleBackColor = false;
            // 
            // lblTitle
            // 
            this.lblTitle.AutoSize = true;
            this.lblTitle.BackColor = System.Drawing.Color.Transparent;
            this.lblTitle.Font = new System.Drawing.Font("宋体", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.lblTitle.ForeColor = System.Drawing.Color.Yellow;
            this.lblTitle.Location = new System.Drawing.Point(162, 22);
            this.lblTitle.Name = "lblTitle";
            this.lblTitle.Size = new System.Drawing.Size(253, 29);
            this.lblTitle.TabIndex = 0;
            this.lblTitle.Text = "KFC Smart Order";
            // 
            // chkCoco
            // 
            this.chkCoco.AutoSize = true;
            this.chkCoco.BackColor = System.Drawing.Color.Transparent;
            this.chkCoco.ForeColor = System.Drawing.Color.White;
            this.chkCoco.Location = new System.Drawing.Point(157, 142);
            this.chkCoco.Name = "chkCoco";
            this.chkCoco.Size = new System.Drawing.Size(48, 16);
            this.chkCoco.TabIndex = 3;
            this.chkCoco.Text = "Coke";
            this.chkCoco.UseVisualStyleBackColor = false;
            // 
            // chkMilk
            // 
            this.chkMilk.AutoSize = true;
            this.chkMilk.BackColor = System.Drawing.Color.Transparent;
            this.chkMilk.ForeColor = System.Drawing.Color.White;
            this.chkMilk.Location = new System.Drawing.Point(278, 141);
            this.chkMilk.Name = "chkMilk";
            this.chkMilk.Size = new System.Drawing.Size(72, 16);
            this.chkMilk.TabIndex = 3;
            this.chkMilk.Text = "Hot milk";
            this.chkMilk.UseVisualStyleBackColor = false;
            // 
            // chkCoffee
            // 
            this.chkCoffee.AutoSize = true;
            this.chkCoffee.BackColor = System.Drawing.Color.Transparent;
            this.chkCoffee.ForeColor = System.Drawing.Color.White;
            this.chkCoffee.Location = new System.Drawing.Point(423, 142);
            this.chkCoffee.Name = "chkCoffee";
            this.chkCoffee.Size = new System.Drawing.Size(60, 16);
            this.chkCoffee.TabIndex = 3;
            this.chkCoffee.Text = "Coffee";
            this.chkCoffee.UseVisualStyleBackColor = false;
            // 
            // txtAddress
            // 
            this.txtAddress.Location = new System.Drawing.Point(157, 174);
            this.txtAddress.Multiline = true;
            this.txtAddress.Name = "txtAddress";
            this.txtAddress.Size = new System.Drawing.Size(327, 49);
            this.txtAddress.TabIndex = 4;
            // 
            // txtTel
            // 
            this.txtTel.Location = new System.Drawing.Point(157, 241);
            this.txtTel.Name = "txtTel";
            this.txtTel.Size = new System.Drawing.Size(327, 21);
            this.txtTel.TabIndex = 5;
            // 
            // btnCommit
            // 
            this.btnCommit.BackColor = System.Drawing.Color.White;
            this.btnCommit.ForeColor = System.Drawing.Color.Black;
            this.btnCommit.Location = new System.Drawing.Point(236, 312);
            this.btnCommit.Name = "btnCommit";
            this.btnCommit.Size = new System.Drawing.Size(75, 23);
            this.btnCommit.TabIndex = 6;
            this.btnCommit.Text = "Commit";
            this.btnCommit.UseVisualStyleBackColor = false;
            this.btnCommit.Click += new System.EventHandler(this.btnCommit_Click);
            // 
            // Order
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.ClientSize = new System.Drawing.Size(551, 388);
            this.Controls.Add(this.btnCommit);
            this.Controls.Add(this.txtTel);
            this.Controls.Add(this.txtAddress);
            this.Controls.Add(this.chkCoffee);
            this.Controls.Add(this.chkMilk);
            this.Controls.Add(this.chkCoco);
            this.Controls.Add(this.rbtnChicken);
            this.Controls.Add(this.rbtnPotato);
            this.Controls.Add(this.rbtnChips);
            this.Controls.Add(this.cbxMeal);
            this.Controls.Add(this.lblTelephone);
            this.Controls.Add(this.lblAddress);
            this.Controls.Add(this.lblDrink);
            this.Controls.Add(this.lblSide);
            this.Controls.Add(this.lblTitle);
            this.Controls.Add(this.lblMeal);
            this.ForeColor = System.Drawing.Color.White;
            this.HelpButton = true;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "Order";
            this.ShowIcon = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "KFC Smart Order";
            this.HelpButtonClicked += new System.ComponentModel.CancelEventHandler(this.Order_HelpButtonClicked);
            this.Load += new System.EventHandler(this.Order_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label lblMeal;
        private System.Windows.Forms.Label lblSide;
        private System.Windows.Forms.Label lblDrink;
        private System.Windows.Forms.Label lblAddress;
        private System.Windows.Forms.Label lblTelephone;
        private System.Windows.Forms.ComboBox cbxMeal;
        private System.Windows.Forms.RadioButton rbtnChips;
        private System.Windows.Forms.RadioButton rbtnPotato;
        private System.Windows.Forms.RadioButton rbtnChicken;
        private System.Windows.Forms.Label lblTitle;
        private System.Windows.Forms.CheckBox chkCoco;
        private System.Windows.Forms.CheckBox chkMilk;
        private System.Windows.Forms.CheckBox chkCoffee;
        private System.Windows.Forms.TextBox txtAddress;
        private System.Windows.Forms.TextBox txtTel;
        private System.Windows.Forms.Button btnCommit;
    }
}


后台代码:

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 WinFormGernalControls
{
    public partial class Order : Form
    {
        public Order()
        {
            InitializeComponent();
        }

        private void Order_HelpButtonClicked(object sender, CancelEventArgs e)
        {

        }

        private void btnCommit_Click(object sender, EventArgs e)
        {
            //meal
            string zs = cbxMeal.SelectedItem.ToString();
            //side item
            string pc = "";
            if (rbtnPotato.Checked)
                pc = rbtnPotato.Text;
            else if (rbtnChicken.Checked)
                pc = rbtnChicken.Text;
            else
                pc = rbtnChips.Text;
            //drink
            string yp = "";
            if (chkCoco.Checked)
                yp += chkCoco.Text;
            if (chkMilk.Checked)
            {
                if (yp != "")
                {
                    yp += "、";
                }
                yp += chkMilk.Text;
            }
            if (chkCoffee.Checked)
            {
                if (yp != "")
                {
                    yp += "、";
                }
                yp += chkCoffee.Text;
            }
            //Address
            string dz = txtAddress.Text;
            //Telephone
            string tel = txtTel.Text;
            MessageBox.Show("Meal:" + zs + "\rSide Item:" + pc + "\rDrink:" + yp + "\rAddress:" + dz + "\rTelephone:" + tel);
        }

        private void Order_Load(object sender, EventArgs e)
        {
            cbxMeal.SelectedIndex = 0;
        }
    }
}

运行效果图:
WinForm常规控件的使用(一)_第1张图片

你可能感兴趣的:(C#,#WinForm,Controls)