常用控件

常 用 控 件 常用控件

1.常用控件

学习控件的使用:百度+官方文档
1 属性

  • 行为:功能相关的属性
  • 外观:显示相关的属性
  • 杂项:该控件特有的属性

2 事件
在这里插入图片描述

2.文本框 TextBox

TextBox文本框
用于输入单行或多行文本,常用单行输入模式


相关属性:

  • 设计

Name :变量名

  • 外观

Text:文本
Font:字体

  • 行为

Multiline :单行模式/多行模式
PasswordChar:如果设置,则变成密码输入框
ReadOnly:只读模式

在这里插入图片描述

提示:不要全部试一遍,而是等需要的时候再来找


在这里插入图片描述

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 WinForm基础3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void Test_Press(object sender, KeyPressEventArgs e)
        {
            // object sender:发送者,即点击的那个组件
            // KeyPressEventArgs e 所携带的信息

            char ch = e.KeyChar;
            if (ch == '\r')
            {
                string str = BoxName.Text;
                MessageBox.Show("用户输入了:" + str);
            }

        }
    }
}

在这里插入图片描述

相关事件:
KeyPress:按键事件,常用于回车处理

3.复选框 CheckBox

CheckBox复选框

属性:
(外观)Text :文本显示
(外观) Checked:是/否

事件:
(操作) Click :点击动作
(杂项) CheckedChanged :选中状态发生变化


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 winform_基础
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void checkBox1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("checkBox1_Click");
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            MessageBox.Show("checkBox1_CheckedChanged");
        }
    }
}


练习:显示一个TextBox和一个CheckBox。
当选中时,以明文显示;取消选中时,以密码显示。

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 winform_基础
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void checkBox1_Click(object sender, EventArgs e)
        {
           // MessageBox.Show("checkBox1_Click");
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            //MessageBox.Show("checkBox1_CheckedChanged");
            bool isChecked = checkBox1.Checked;
            if (isChecked)
            {
                textBox1.PasswordChar = '\0';
            }
            else
            {
                textBox1.PasswordChar = '*';
            }
        }
    }
}

在这里插入图片描述

在这里插入图片描述


区分两种事件
Click :用户手动点击
ChekedChanged :状态值发生变化,可能是用户点击,也可能是程序代码改变了这个值
例如,

checkBox1.Checked = true;

4.下拉列表 ComboBox

ComboBox下拉列表

添加数据项

  • 在设计器里直接编辑
属性 | 数据 | Items
  • 也可以在构造方法里手工添加
comboBox1.Items.Add("red");

获取选中的项

SelectedItem :选中项的值, null表示未选中
SelectedIndex :选中项的索引,-1表示未选中

事件

事件 | 行为 | SelectedIndexChanged
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 winform_基础
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // 使用Add或Insert可以添加项
            comboBox1.Items.Add("明");
            comboBox1.Items.Add("茗");
        }


        private void Form1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            // SelectedItem:选中项的值,null表示未选中
            // SelectedIndex:选中项的索引,-1表示未选中
            int index = comboBox1.SelectedIndex;
            string sel = (string)comboBox1.SelectedItem;
            Console.WriteLine("选中了:" + sel);

        }
    }
}

在这里插入图片描述

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 winform_基础
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // 使用Add或Insert可以添加项
            comboBox1.Items.Add("明");
            comboBox1.Items.Add("茗");
        }


        private void Form1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            // SelectedItem:选中项的值,null表示未选中
            // SelectedIndex:选中项的索引,-1表示未选中
            int index = comboBox1.SelectedIndex;
            string sel = (string)comboBox1.SelectedItem;
            Console.WriteLine("选中了:" + sel);

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Console.WriteLine("选中了:" + comboBox1.SelectedItem);
        }
    }
}

在这里插入图片描述

ComboBox里的数据项可以是任意类型可以是string,也可以是自定义的类型

5.列表框 ListBox

ListBox列表框

两方面的功能:

  • 展示:展示一些项给用户看
  • 选择:让用户单选/或多选

属性:
(行为) SelectionMode:单选/多选
事件:
(行为)SelectedIndexChanged


1 列表项数据可以是任意类型object
本例中使用的是Student类型
实际在列表时,会调用Student.ToString()来显示

2 界面显示优化,如行高、自定义显示

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

namespace winform_基础
{
    class Student
    {
        public int id;
        public string name;

        public Student(int id, string name)
        {
            this.id = id;
            this.name = name;
        }

        public override string ToString()
        {
            return string.Format("({0}) {1}", id, name);
        }
    }
}

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 winform_基础
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

            listBox1.Items.Add(new Student(1, "major"));
            listBox1.Items.Add(new Student(2, "ming"));
            listBox1.Items.Add(new Student(3, "jun"));
            listBox1.Items.Add(new Student(4, "茗"));
            listBox1.Items.Add(new Student(5, "君"));
          

            listBox1.SetSelected(0, true);
            listBox1.SetSelected(2, true);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 单选模式
            // listBox1.SelectedIndex
            // listBox1.SelectedItem

            // 多选模式
            // listBox1.SelectedIndices
            // listBox1.SelectedItems
            foreach (object o in listBox1.SelectedItems)
            {
                Student stu = (Student)o;
                Console.WriteLine("选中了: " + stu.name);
            }
        }

        private void Form1_Click(object sender, EventArgs e)
        {

        }




    }
}

6.(练习)学生信息编辑

练习:实现一个学生信息的编辑器

  • 学号,姓名,性别,手机号
  • 将数据保存到文件
  • 启动时从文件读取

1 界面布局

  • 添加需要的控件
  • 修改显示文本Text
  • 手工对齐
  • 修改控件名Name

2保存功能
点保存时,将界面的数据保存到文件中

  • 添加NewtonSoft.json支持
  • 添加按钮事件处理
  • 将数据保存为JSON,存到文件中

3 加载功能
当程序启动时,自动读取student.txt中的数据

  • 在构造方法中加载
  • 读取文件,转成JSON
  • 将数据显示到界面
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForm基础22
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void save_click(object sender, EventArgs e)
        {
            Student stu = new Student();
            stu.Id = Convert.ToInt32(idField.Text.Trim());
            stu.Name = nameField.Text.Trim();
            stu.Sex = (sexField.SelectedIndex == 1);
            stu.Phone = phoneField.Text.Trim();

            // JSON
            string jsonstr = JsonConvert.SerializeObject(stu, Formatting.Indented);
            StreamWriter sw = new StreamWriter("test.txt");//这里写上你要保存的路径
            sw.WriteLine(jsonstr);//按行写
            sw.Close();//关闭
            MessageBox.Show("操作成功");
        }
    }
}

在这里插入图片描述


class RWTextFile
    {
        public static string Read(string filePath, Encoding encoding)
        {
            using (StreamReader sr = new StreamReader(filePath, encoding))
            {
                return sr.ReadToEnd();
            }
        }

        public static void Write(string filePath, string content, Encoding encoding)
        {
            using (StreamWriter sw = new StreamWriter(filePath, false, encoding))
            {
                sw.Write(content);
            }
        }

        // 两个静态属性
        public static Encoding UTF8
        {
            get
            {
                return UTF8Encoding.UTF8;
            }
        }
        public static Encoding GBK
        {
            get
            {
                return Encoding.GetEncoding("GBK");
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值