ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

C# WinForm 常用控件及 SunnyUI 常用控件详解

C# WinForm 常用控件及 SunnyUI 常用控件详解 本文系统梳理 C# WinForm 原生常用控件与第三方 UI 库 SunnyUI 的常用控件涵盖控件说明、关键属性、典型用法及代码示例可直接复制使用。一、C# WinForm 常用控件1.1 文本显示与输入类Label标签用于显示不可编辑的文本常作为提示信息。Label label1 new Label(); label1.Text 用户名; label1.Location new Point(20, 20); label1.AutoSize true; this.Controls.Add(label1);常用属性Text、Font、ForeColor、TextAlign、AutoSizeTextBox文本框用于输入和编辑单行或多行文本。TextBox txtName new TextBox(); txtName.Location new Point(80, 20); txtName.Width 150; txtName.MaxLength 20; // 最大输入长度 txtName.PasswordChar *; // 密码模式 txtName.Multiline true; // 多行模式 txtName.ScrollBars ScrollBars.Vertical; this.Controls.Add(txtName);常用属性Text、Multiline、PasswordChar、ReadOnly、MaxLength、ScrollBars常用事件TextChanged、KeyPress、KeyDownRichTextBox富文本框支持格式化文本字体、颜色、段落等的编辑控件。RichTextBox rtb new RichTextBox(); rtb.Location new Point(20, 60); rtb.Size new Size(300, 150); rtb.SelectionFont new Font(宋体, 12, FontStyle.Bold); rtb.SelectionColor Color.Red; rtb.AppendText(这是一段富文本); this.Controls.Add(rtb);1.2 按钮与选择类Button按钮最常用的交互控件触发点击事件。Button btnOk new Button(); btnOk.Text 确定; btnOk.Location new Point(20, 220); btnOk.Size new Size(80, 30); btnOk.Click (s, e) { MessageBox.Show(点击了确定按钮); }; this.Controls.Add(btnOk);常用属性Text、Image、FlatStyle、Enabled、DialogResultCheckBox复选框允许多选的布尔选择控件。CheckBox chkRead new CheckBox(); chkRead.Text 已阅读协议; chkRead.Location new Point(20, 260); chkRead.CheckedChanged (s, e) { MessageBox.Show(选中状态 chkRead.Checked); }; this.Controls.Add(chkRead);常用属性Checked、CheckState、Text、ThreeStateRadioButton单选按钮同一容器内互斥的单选控件。RadioButton rdoMale new RadioButton(); rdoMale.Text 男; rdoMale.Location new Point(20, 290); rdoMale.Checked true; RadioButton rdoFemale new RadioButton(); rdoFemale.Text 女; rdoFemale.Location new Point(80, 290); this.Controls.Add(rdoMale); this.Controls.Add(rdoFemale);1.3 列表与选择类ComboBox下拉框下拉选择列表支持可编辑模式。ComboBox cboCity new ComboBox(); cboCity.Location new Point(20, 320); cboCity.Width 150; cboCity.DropDownStyle ComboBoxStyle.DropDownList; // 不可编辑 cboCity.Items.AddRange(new object[] { 北京, 上海, 广州, 深圳 }); cboCity.SelectedIndex 0; this.Controls.Add(cboCity); // 获取选中值 string city cboCity.SelectedItem.ToString();ListBox列表框显示可滚动的项列表支持单选或多选。ListBox lstHobby new ListBox(); lstHobby.Location new Point(20, 350); lstHobby.Size new Size(150, 80); lstHobby.SelectionMode SelectionMode.MultiSimple; // 多选 lstHobby.Items.AddRange(new object[] { 阅读, 运动, 音乐, 旅行 }); this.Controls.Add(lstHobby);ListView列表视图以多种视图大图标、小图标、列表、详细信息显示项集合。ListView lv new ListView(); lv.Location new Point(20, 440); lv.Size new Size(300, 120); lv.View View.Details; // 详细信息视图 lv.FullRowSelect true; // 整行选中 lv.GridLines true; // 显示网格线 lv.Columns.Add(ID, 50); lv.Columns.Add(姓名, 100); lv.Columns.Add(年龄, 80); lv.Items.Add(new ListViewItem(new[] { 1, 张三, 20 })); lv.Items.Add(new ListViewItem(new[] { 2, 李四, 22 })); this.Controls.Add(lv);TreeView树视图以树形结构显示层级数据。TreeView tv new TreeView(); tv.Location new Point(20, 570); tv.Size new Size(200, 120); TreeNode root new TreeNode(计算机); root.Nodes.Add(CPU); root.Nodes.Add(内存); root.Nodes.Add(硬盘); tv.Nodes.Add(root); tv.ExpandAll(); this.Controls.Add(tv);1.4 容器类Panel面板用于分组管理其他控件的容器。Panel panel1 new Panel(); panel1.Location new Point(20, 10); panel1.Size new Size(300, 200); panel1.BorderStyle BorderStyle.FixedSingle; panel1.BackColor Color.LightGray; this.Controls.Add(panel1);GroupBox分组框带标题的容器常用于逻辑分组。GroupBox gb new GroupBox(); gb.Text 用户信息; gb.Location new Point(20, 10); gb.Size new Size(280, 150); this.Controls.Add(gb);TabControl选项卡控件多页签容器每页可放置不同控件。TabControl tab new TabControl(); tab.Location new Point(20, 10); tab.Size new Size(350, 200); TabPage page1 new TabPage(基本信息); TabPage page2 new TabPage(详细设置); tab.TabPages.Add(page1); tab.TabPages.Add(page2); page1.Controls.Add(new Label { Text 姓名, Location new Point(10, 10) }); this.Controls.Add(tab);SplitContainer分割容器可拖动分隔条的两栏容器。SplitContainer sc new SplitContainer(); sc.Dock DockStyle.Fill; sc.Orientation Orientation.Vertical; sc.Panel1.BackColor Color.LightBlue; sc.Panel2.BackColor Color.LightGreen; this.Controls.Add(sc);1.5 菜单与工具栏类MenuStrip菜单栏窗体顶部的主菜单。MenuStrip menu new MenuStrip(); ToolStripMenuItem fileMenu new ToolStripMenuItem(文件(F)); fileMenu.DropDownItems.Add(新建); fileMenu.DropDownItems.Add(打开); fileMenu.DropDownItems.Add(-); // 分隔线 fileMenu.DropDownItems.Add(退出); menu.Items.Add(fileMenu); this.MainMenuStrip menu; this.Controls.Add(menu);ToolStrip工具栏快捷操作工具栏。ToolStrip ts new ToolStrip(); ts.GripStyle ToolStripGripStyle.Hidden; ts.Items.Add(新建); ts.Items.Add(打开); ts.Items.Add(保存); this.Controls.Add(ts);StatusStrip状态栏窗体底部状态栏。StatusStrip ss new StatusStrip(); ToolStripStatusLabel lblStatus new ToolStripStatusLabel(); lblStatus.Text 就绪; ss.Items.Add(lblStatus); this.Controls.Add(ss);ContextMenuStrip右键菜单控件的上下文快捷菜单。ContextMenuStrip cms new ContextMenuStrip(); cms.Items.Add(复制); cms.Items.Add(粘贴); cms.Items.Add(删除); txtName.ContextMenuStrip cms; // 绑定到文本框1.6 数据显示类DataGridView数据表格以表格形式显示和编辑数据WinForm 中最强大的数据控件。DataGridView dgv new DataGridView(); dgv.Dock DockStyle.Fill; dgv.AllowUserToAddRows false; // 禁止用户添加行 dgv.ReadOnly true; // 只读 dgv.SelectionMode DataGridViewSelectionMode.FullRowSelect; dgv.AutoSizeColumnsMode DataGridViewAutoSizeColumnsMode.Fill; // 绑定 DataTable DataTable dt new DataTable(); dt.Columns.Add(ID, typeof(int)); dt.Columns.Add(姓名, typeof(string)); dt.Columns.Add(分数, typeof(double)); dt.Rows.Add(1, 张三, 95.5); dt.Rows.Add(2, 李四, 88.0); dgv.DataSource dt; this.Controls.Add(dgv);常用事件CellClick、CellDoubleClick、CellValueChanged、RowPostPaint1.7 对话框类表格控件用途关键属性 / 方法OpenFileDialog打开文件Filter、FileName、ShowDialog()SaveFileDialog保存文件Filter、FileName、ShowDialog()FolderBrowserDialog选择文件夹SelectedPath、ShowDialog()ColorDialog选择颜色Color、ShowDialog()FontDialog选择字体Font、ShowDialog()// 打开文件对话框示例 OpenFileDialog ofd new OpenFileDialog(); ofd.Filter 文本文件|*.txt|所有文件|*.*; ofd.Title 请选择文件; if (ofd.ShowDialog() DialogResult.OK) { string content File.ReadAllText(ofd.FileName); MessageBox.Show(content); }1.8 其他常用控件PictureBox图片框PictureBox pb new PictureBox(); pb.Location new Point(20, 20); pb.Size new Size(200, 150); pb.SizeMode PictureBoxSizeMode.Zoom; pb.Image Image.FromFile(C:\test.jpg); this.Controls.Add(pb);Timer定时器Timer timer new Timer(); timer.Interval 1000; // 1秒 timer.Tick (s, e) { lblTime.Text DateTime.Now.ToString(yyyy-MM-dd HH:mm:ss); }; timer.Start();ProgressBar进度条ProgressBar pb new ProgressBar(); pb.Location new Point(20, 20); pb.Size new Size(200, 20); pb.Minimum 0; pb.Maximum 100; pb.Value 50; this.Controls.Add(pb);DateTimePicker日期选择器DateTimePicker dtp new DateTimePicker(); dtp.Location new Point(20, 20); dtp.Format DateTimePickerFormat.Custom; dtp.CustomFormat yyyy-MM-dd HH:mm:ss; dtp.Value DateTime.Now; this.Controls.Add(dtp);ToolTip提示气泡ToolTip tip new ToolTip(); tip.SetToolTip(btnOk, 点击此按钮提交数据); tip.AutoPopDelay 5000; tip.InitialDelay 500;二、SunnyUI 常用控件SunnyUI 是基于 .NET Framework 4.0 和 .NET Core 3.0 的 WinForm UI 库提供了一套美观、易用的控件。使用前需通过 NuGet 安装SunnyUI。2.1 窗体与页面UIForm美化窗体继承自Form自带圆角、阴影、标题栏美化。只需让窗体继承UIForm即可。public partial class MainForm : UIForm { public MainForm() { InitializeComponent(); // 设置主题色 this.Style UIStyle.Blue; } }常用属性Style主题风格、RectColor边框颜色、TitleColor标题栏颜色UIPage页面控件配合UIFrame或UITabControl使用实现多页面切换。public partial class PageHome : UIPage { public PageHome() { InitializeComponent(); this.Text 首页; } }2.2 按钮类UIButton美化按钮支持多种样式、圆角、悬停效果。UIButton btn new UIButton(); btn.Text 提交; btn.Location new Point(20, 20); btn.Size new Size(100, 35); btn.Style UIStyle.Blue; btn.Radius 5; // 圆角 btn.Click (s, e) { ShowSuccessTip(操作成功); }; this.Controls.Add(btn);常用属性Style、Radius、FillColor、ForeColor、HoverColor、PressColor2.3 文本输入类UILabel美化标签UILabel lbl new UILabel(); lbl.Text 用户名; lbl.Location new Point(20, 20); lbl.ForeColor Color.FromArgb(64, 64, 64); this.Controls.Add(lbl);UITextBox美化文本框支持圆角、水印提示、图标。UITextBox txt new UITextBox(); txt.Location new Point(80, 20); txt.Size new Size(180, 30); txt.Radius 5; txt.Watermark 请输入用户名; // 水印提示 txt.MaxLength 20; this.Controls.Add(txt);常用属性Watermark、Radius、MaxLength、PasswordChar、Multiline2.4 选择与开关类UICheckBox美化复选框UICheckBox chk new UICheckBox(); chk.Text 记住密码; chk.Location new Point(20, 60); chk.Checked true; this.Controls.Add(chk);UIRadioButton美化单选按钮UIRadioButton rdo new UIRadioButton(); rdo.Text 男; rdo.Location new Point(20, 90); this.Controls.Add(rdo);UISwitch开关控件类似手机开关的布尔选择控件SunnyUI 特色控件。UISwitch sw new UISwitch(); sw.Location new Point(20, 120); sw.Checked true; sw.ValueChanged (s, e) { MessageBox.Show(开关状态 sw.Checked); }; this.Controls.Add(sw);常用属性Checked、ActiveColor开启颜色、InActiveColor关闭颜色2.5 下拉与列表类UIComboBox美化下拉框UIComboBox cbo new UIComboBox(); cbo.Location new Point(20, 150); cbo.Size new Size(180, 30); cbo.Radius 5; cbo.DropDownStyle UIDropDownStyle.DropDownList; cbo.Items.AddRange(new object[] { 选项一, 选项二, 选项三 }); cbo.SelectedIndex 0; this.Controls.Add(cbo);2.6 数据表格类UIDataGridView美化数据表格在原生DataGridView基础上美化了表头、行样式。UIDataGridView dgv new UIDataGridView(); dgv.Dock DockStyle.Fill; dgv.AllowUserToAddRows false; dgv.ReadOnly true; dgv.SelectionMode DataGridViewSelectionMode.FullRowSelect; dgv.AutoSizeColumnsMode DataGridViewAutoSizeColumnsMode.Fill; DataTable dt new DataTable(); dt.Columns.Add(编号, typeof(int)); dt.Columns.Add(名称, typeof(string)); dt.Columns.Add(数量, typeof(int)); dt.Rows.Add(1, 商品A, 100); dt.Rows.Add(2, 商品B, 200); dgv.DataSource dt; this.Controls.Add(dgv);2.7 进度与状态类UIProcessBar进度条支持多种样式可显示百分比文字。UIProcessBar pb new UIProcessBar(); pb.Location new Point(20, 20); pb.Size new Size(250, 25); pb.Value 65; pb.ShowText true; // 显示百分比文字 pb.Style UIStyle.Blue; this.Controls.Add(pb);常用属性Value、Maximum、Minimum、ShowText、Style、RadiusUIBarChart柱状图SunnyUI 内置图表控件无需第三方库。UIBarChart chart new UIBarChart(); chart.Dock DockStyle.Fill; chart.Title 月度销售统计; chart.Option new UIBarOption(); chart.Option.XAxis.Data.AddRange(new[] { 1月, 2月, 3月, 4月, 5月, 6月 }); UISeries series new UISeries(销售额); series.AddData(120, 200, 150, 80, 70, 110); chart.Option.Series.Add(series); chart.Refresh(); this.Controls.Add(chart);UILineChart折线图UILineChart lineChart new UILineChart(); lineChart.Dock DockStyle.Fill; lineChart.Title 趋势分析; lineChart.Option new UILineOption(); lineChart.Option.XAxis.Data.AddRange(new[] { 周一, 周二, 周三, 周四, 周五 }); UISeries s new UISeries(访问量); s.AddData(100, 200, 150, 80, 70); lineChart.Option.Series.Add(s); lineChart.Refresh(); this.Controls.Add(lineChart);2.8 导航与菜单类UINavBar导航栏左侧或顶部导航菜单常用于管理系统。UINavBar nav new UINavBar(); nav.Dock DockStyle.Left; nav.Width 180; nav.Items.Add(new UINavItem { Text 首页, PageName PageHome }); nav.Items.Add(new UINavItem { Text 用户管理, PageName PageUser }); nav.Items.Add(new UINavItem { Text 系统设置, PageName PageSetting }); nav.MenuItemClick (s, e) { MessageBox.Show(点击了 e.Item.Text); }; this.Controls.Add(nav);UITabControl美化选项卡UITabControl tab new UITabControl(); tab.Dock DockStyle.Fill; tab.Style UIStyle.Blue; TabPage page1 new TabPage(页面一); TabPage page2 new TabPage(页面二); tab.TabPages.Add(page1); tab.TabPages.Add(page2); this.Controls.Add(tab);2.9 消息提示与对话框ShowSuccessTip / ShowErrorTip提示气泡SunnyUI 内置的右上角消息提示。// 成功提示 this.ShowSuccessTip(保存成功); // 错误提示 this.ShowErrorTip(操作失败请重试); // 信息提示 this.ShowInfoTip(请注意查看); // 警告提示 this.ShowWarningTip(数据将被删除);UIMessageBox美化消息框替代原生MessageBox。UIMessageBox.Show(这是一条提示信息, 标题, UIStyle.Blue); // 带确认按钮 if (UIMessageBox.ShowDialog(确定要删除吗, 确认, UIStyle.Red) DialogResult.OK) { // 执行删除 }2.10 其他特色控件UIDatePicker日期选择器UIDatePicker dtp new UIDatePicker(); dtp.Location new Point(20, 20); dtp.Size new Size(180, 30); dtp.Value DateTime.Now; this.Controls.Add(dtp);UICircularProgress圆形进度条UICircularProgress cp new UICircularProgress(); cp.Location new Point(20, 20); cp.Size new Size(100, 100); cp.Value 75; cp.ShowText true; this.Controls.Add(cp);UILedLabelLED 数字标签UILedLabel led new UILedLabel(); led.Location new Point(20, 20); led.Size new Size(150, 50); led.Text 8888; led.LedColor Color.Red; this.Controls.Add(led);三、SunnyUI 主题风格SunnyUI 内置多种主题风格通过Style属性统一设置// 全局设置主题 UIStyles.SetStyle(UIStyle.Dark); // 单个控件设置 btn.Style UIStyle.Blue;表格风格枚举说明UIStyle.Blue蓝色主题默认UIStyle.Green绿色主题UIStyle.Orange橙色主题UIStyle.Red红色主题UIStyle.Purple紫色主题UIStyle.Dark深色主题UIStyle.Light浅色主题四、总结表格分类WinForm 原生控件SunnyUI 对应控件窗体FormUIForm按钮ButtonUIButton标签LabelUILabel文本框TextBoxUITextBox复选框CheckBoxUICheckBox单选框RadioButtonUIRadioButton下拉框ComboBoxUIComboBox数据表格DataGridViewUIDataGridView进度条ProgressBarUIProcessBar选项卡TabControlUITabControl开关无UISwitch图表无UIBarChart/UILineChart圆形进度无UICircularProgressLED 数字无UILedLabelWinForm 原生控件覆盖面广、稳定可靠适合基础业务开发SunnyUI 在原生控件基础上提供了更美观的视觉效果和更丰富的特色控件开关、图表、圆形进度条等能显著提升桌面应用的 UI 质感。两者可混合使用根据实际需求灵活选择。
返回列表