Winform(1.Winform控件学习)
使用的控件有:Button,Label,TextBox
button:表示一个按钮,用户点击按钮触发事件 click事件最常用
label:标签,用于显示文本 Name属性:变量名称
textBox:输入框
Form1代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _1.Winform控件学习
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//控件除了拖拽,也可以自定义(窍门:查看属性列表)
TextBox userNameTextBox = new TextBox
{
Location = new Point(350, 40),
Size = new Size(200, 20),
PasswordChar = '*',
};
//Controls代表当前窗体的控制集合
this.Controls.Add(userNameTextBox);
}
//按钮点击时执行的事件
//1.事件绑定者
//2.事件的基类
private void button1_Click(object sender, EventArgs e)
{
//MessageBox:消息提示框
//show:弹出的文本
MessageBox.Show("这是一个简单的消息提示框");
}
private void button2_Click(object sender, EventArgs e)
{
//带有标题
MessageBox.Show("带有标题的提示框","早上好");
}
private void button3_Click(object sender, EventArgs e)
{
//制定按钮和图标:选项和图标都是枚举
//DialogResult:代表获取用户的点击
DialogResult result = MessageBox.Show("这是一个带有按钮和图标的对话框","早上好",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
//具体的判断根据Show中的枚举来配对
if (result ==DialogResult.Yes)
{
//获取到当前label标签,添加内容
this.resultLabel.Text = "用户确定了,点击了yes";
}
else
{
this.resultLabel.Text = "用户取消了,点击了No";
}
}
private void button4_Click(object sender, EventArgs e)
{
//指定默认按钮:用户按下回车触发的按钮
//MessageBoxDefaultButton.Button2:默认为第二个按钮(否)
//MessageBoxDefaultButton.Button1:默认为第一个按钮(是)
MessageBox.Show("这是一个带有默认按钮的对话框","我是标题",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
}
private void button5_Click(object sender, EventArgs e)
{
//案例
MessageBox.Show("欢迎来到winform教程","欢迎大家");
DialogResult result = MessageBox.Show("确定要学习吗?", "态度选择", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
MessageBox.Show("您选择了是,程序会继续运行", "提醒");
}
else
{
MessageBox.Show("您选择了否,程序将退出", "提醒");
//关闭程序
this.Close();
}
}
private void button6_Click(object sender, EventArgs e)
{
string userInput = AccountTxt.Text;
if (userInput =="admin")
{
MessageBox.Show("欢迎");
}
else
{
MessageBox.Show("输入错误");
}
}
}
}