设计器样式:
控件属性:
checkBox1:
checkedListBox1:
comboBox1:
dateTimePicker1:
tabControl1:
运行后的效果
类文件: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 winform控件的学习
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SetStaticValue_comboBox1();//默认下拉框中不显示为文本
//SetDynamicValue_comboBox1();//默认选中索引为0的文本显示在下拉框中
}
private void Form1_Load(object sender, EventArgs e)
{
UseControl_DateTimerPicker();
}
//静态传值
void SetStaticValue_comboBox1()
{
comboBox1.Items.Add("C语言");
comboBox1.Items.Add("C#");
comboBox1.Items.Add("C++");
comboBox1.SelectedIndex = 0;
}
//动态传值:String 类写入list集合中
void SetDynamicValue_comboBox1()
{
List<string> list = new List<string>()
{
"C语言",
"C#",
"C++"
};
comboBox1.DataSource = list;
}
动态传值;将Class类写入到List集合中
//public void SetDataSource<T>(List<T> list,string str)
//{
// comboBox1.DataSource = list;
// comboBox1.DisplayMember = str;//要显示在下拉框中的属性名
//}
private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
MessageBox.Show("检测到下拉框的下拉内容关闭后触发该事件");
}
void UseControl_DateTimerPicker()
{
DateTime time = dateTimePicker1.Value;//默认24小时制
MessageBox.Show(time.ToString());
}
//设置tabControl控件的按钮颜色
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
SolidBrush lightblue = new SolidBrush(Color.LightBlue);//创建一个淡蓝色画刷
SolidBrush black = new SolidBrush(Color.Black);//创建一个黑色画刷
StringFormat stringFormat = new StringFormat();//创建一个文本字符串对象
stringFormat.Alignment = StringAlignment.Center;//文本居中
for (int i = 0; i < tabControl1.TabPages.Count; i++)
{
Rectangle Rec = tabControl1.GetTabRect(i);//返回选项卡控件中指定选项卡的边框
e.Graphics.FillRectangle(lightblue, Rec);//用画刷将选项卡内部边框填满
e.Graphics.DrawString(tabControl1.TabPages[i].Text, new Font("宋体", 20), black, Rec, stringFormat);//绘制指定文本字符串
}
}
}
}
Form1.Designer.cs的代码:
namespace winform控件的学习
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.tabControl1.SuspendLayout();
this.SuspendLayout();
//
// checkBox1
//
this.checkBox1.AutoCheck = false;
this.checkBox1.AutoSize = true;
this.checkBox1.Checked = true;
this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBox1.Location = new System.Drawing.Point(12, 12);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(78, 16);
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "checkBox1";
this.checkBox1.UseVisualStyleBackColor = true;
//
// checkedListBox1
//
this.checkedListBox1.BackColor = System.Drawing.Color.LightBlue;
this.checkedListBox1.Font = new System.Drawing.Font("微软雅黑", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.checkedListBox1.FormattingEnabled = true;
this.checkedListBox1.Items.AddRange(new object[] {
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18"});
this.checkedListBox1.Location = new System.Drawing.Point(12, 34);
this.checkedListBox1.MultiColumn = true;
this.checkedListBox1.Name = "checkedListBox1";
this.checkedListBox1.Size = new System.Drawing.Size(365, 208);
this.checkedListBox1.TabIndex = 1;
//
// comboBox1
//
this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox1.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(12, 265);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(179, 28);
this.comboBox1.TabIndex = 2;
this.comboBox1.DropDownClosed += new System.EventHandler(this.comboBox1_DropDownClosed);
//
// dateTimePicker1
//
this.dateTimePicker1.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
this.dateTimePicker1.Location = new System.Drawing.Point(12, 315);
this.dateTimePicker1.Name = "dateTimePicker1";
this.dateTimePicker1.ShowCheckBox = true;
this.dateTimePicker1.Size = new System.Drawing.Size(200, 30);
this.dateTimePicker1.TabIndex = 3;
//
// tabControl1
//
this.tabControl1.Alignment = System.Windows.Forms.TabAlignment.Left;
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;
this.tabControl1.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.tabControl1.ItemSize = new System.Drawing.Size(40, 120);
this.tabControl1.Location = new System.Drawing.Point(12, 353);
this.tabControl1.Multiline = true;
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(254, 85);
this.tabControl1.SizeMode = System.Windows.Forms.TabSizeMode.Fixed;
this.tabControl1.TabIndex = 4;
this.tabControl1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tabControl1_DrawItem);
//
// tabPage1
//
this.tabPage1.BackColor = System.Drawing.Color.Chocolate;
this.tabPage1.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.tabPage1.Location = new System.Drawing.Point(124, 4);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(126, 77);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "1111111";
//
// tabPage2
//
this.tabPage2.BackColor = System.Drawing.Color.OrangeRed;
this.tabPage2.Location = new System.Drawing.Point(124, 4);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(126, 77);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "2222222";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.tabControl1);
this.Controls.Add(this.dateTimePicker1);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.checkedListBox1);
this.Controls.Add(this.checkBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.tabControl1.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.CheckedListBox checkedListBox1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.DateTimePicker dateTimePicker1;
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
}
}