当前位置: 首页 > news >正文

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("输入错误");
            }
        }
    }
}

相关文章:

  • 每天学一个 Linux 命令(31):md5sum
  • Linux安全模块:SELinux与AppArmor深度解析
  • ✨ Apifox:这玩意儿是接口界的“瑞士军刀”吧![特殊字符][特殊字符]
  • XYNU2024信安杯-REVERSE(复现)
  • kafka与flume的整合、spark-streaming
  • 量子加密通信技术及其应用:构建无条件安全的通信网络
  • 【合新通信】浸没式液冷光模块与冷媒兼容性测试技术报告
  • 【滑动窗口+哈希表/数组记录】Leetcode 3. 无重复字符的最长子串
  • 搜索二叉树-key的搜索模型
  • nc工具!Netcat:TCP/IP瑞士军刀!全参数详细教程!Kali Linux教程!
  • prometheus通过Endpoints自定义grafana的dashboard模块
  • 时序数据库IoTDB在航空航天领域的解决方案
  • 对Mac文字双击或三击鼠标左键没有任何反应
  • Mac 「brew」快速安装MySQL
  • 河道流量监测,雷达流量计赋能水安全智慧守护
  • 4.26 世界知识产权日|CRMEB与您共筑知识产权保护铠甲
  • 【playwright】 page.wait_for_timeout() 和time.sleep()区别
  • 【华为HCIP | 华为数通工程师】821—多选解析—第十七页
  • Spring 与 ActiveMQ 的深度集成实践(一)
  • 告别进度失控:用燃尽图补上甘特图的监控盲区
  • 大众、学术和政治三重框架下的“汉末之变”
  • 申活观察|咖香涌动北外滩,带来哪些消费新想象?
  • 国家卫健委对近日肖某引发舆情问题开展调查
  • 乌美签署矿产协议
  • 锦江酒店:第一季度营业收入约29.42亿元,境内酒店出租率同比增长
  • 澎湃读报丨解放日报9个版聚焦:上海,加快建成具有全球影响力的科技创新高地