WINFORM学习手册——TextBox、Lable、Button

一、打开上一篇建立的项目:

image

二、在MainForm.cs里面输入以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFrom
{
    public class MainForm:Form//继承Form
    {
        private TextBox MyTextBox;//创建TextBox
        private Label MyLabel;//创建label
        private Button MyButton;//创建Button
        public MainForm()
        {
            this.Text = "测试窗体";//标明窗体的Title
            MyTextBox = new TextBox();//实例化TextBox
            MyTextBox.Left = 30;//设置控件离form左边的距离
            MyTextBox.Top = 30;//设置控件离form上边的距离
            MyTextBox.Width = 200;//设置控件的宽度
            this.Controls.Add(MyTextBox);//将控件加入到form中
            MyLabel = new Label();//实例化Label
            MyLabel.Left = 30;//设置控件离form左边的距离
            MyLabel.Top = 80;//设置控件离form上边的距离
            MyLabel.Width = 200;//设置控件的宽度
            MyLabel.Text = "显示TextBox输入的内容";//设置label显示内容
            this.Controls.Add(MyLabel);//将控件加入到form中
            MyButton = new Button();//实例化Button
            MyButton.Left = 30;//设置控件离form左边的距离
            MyButton.Top = 130;//设置控件离form上边的距离
            MyButton.Width = 200;//设置控件的宽度
            MyButton.Text = "显示";//按钮显示名称
            MyButton.Click += ShowMessage;//按钮单机事件
            this.Controls.Add(MyButton);//将控件加入到form中
        }
        /// <summary>
        /// 显示用户输入的信息
        /// </summary>
        /// <param name="sender">出发事件的对象</param>
        /// <param name="e">事件信息</param>
        private void ShowMessage(object sender, EventArgs e)
        {
            MyLabel.Text = MyTextBox.Text;//将用户输入的信息显示在label控件上
        }
    }
}

三、执行:

image

posted @ 2014-01-05 12:54  争世不悔  阅读(744)  评论(0编辑  收藏  举报