即将毕业,从c/c++转向c#,linux转Windows,先提前学习工作中会用到的的基础知识。
1.DateTimePicker控件
该日期控件用于选择日期和时间,而不是连续的时间段,也可以直接输入日期和时间。
该控件的Format属性用于获取或设置日期及时间格式。该属性值是DateTimePickerFormat的枚举值。枚举值如下:
枚举值 | 说明 |
---|---|
Custom | 自定义格式显示日期时间值 |
Long | 以用户操作系统设置的长日期格式显示日期时间值 |
Short | 以用户操作系统设置的短日期格式显示日期时间值 |
Time | 以用户操作系统设置的日期格式显示日期时间值 |
若自定义时间日期格式,需要用到customformat属性,以下是属性值的格式字符串:
d
一个或两位数的日期。
dd
两位数的日期。 位数字的日期值的前面带有为 0。
ddd
三个字符一天的星期几的缩写。
dddd
完整的星期名称中。
h
12 小时格式的一个或两位数小时数。
hh
12 小时格式的两位数小时数。 一位数字值的前面带有为 0。
H
24 小时格式的一个或两位数小时数。
HH
24 小时格式的两位数小时数。 一位数字值的前面带有为 0。
m
一个或两个数字的分钟数。
mm
两个数字的分钟数。 一位数字值的前面带有为 0。
M
一个或两位数月份值。
MM
两位数表示的月份数。 一位数字值的前面带有为 0。
MMM
三个字符的月份的缩写。
MMMM
完整的月份名。
s
一个或两位数秒数。
ss
两位数秒数。 一位数字值的前面带有为 0。
t
一个字母 a.m./p.m.。 缩写 (上午 显示为"A")。
tt
两个字母 a.m./p.m.。 缩写 (上午 将显示如"是")。
y
一位数字的年份 (2001年显示为"1")。
yy
年 (2001年显示为"01") 中的最后两位数字。
yyyy
完整的年份 (2001年显示为"2001")。
示例:设置两个datetimepicker控件,用来分别显示Time格式和custom格式,再用text和value属性获取datetimepicke中的值。
private void Form1_Load(object sender, EventArgs e)
{
dateTimePicker1.Format = DateTimePickerFormat.Time;//设置控件format属性值为time格式
dateTimePicker1.ShowUpDown = true;//可以用按钮获取时间值
dateTimePicker2.Format = DateTimePickerFormat.Custom;//设置控件format属性值为Custom格式
dateTimePicker2.CustomFormat = "yyyy年MMM d日 ddd,H点 m分";//自定义时间格式
textBox1.Text = dateTimePicker2.Text;//获取datetimepicker的值
//分别获取datetimepicker的年月日
textBox2.Text = dateTimePicker1.Value.Year.ToString();
textBox3.Text = dateTimePicker1.Value.Month.ToString();
textBox4.Text = dateTimePicker1.Value.Date.ToString();
//这种方法可以直接获取当前系统的时间日期
// textBox2.Text = dateTimePicker1.Value.ToShortDateString();
//textBox2.Text = dateTimePicker1.Value.ToShortDateString();
}
2.MonthCalendar控件
该月历控件提供了一个直观的图形界面,可以让用户查看和设置日期。
示例:用MonthCalendar控件显示多个月份,更改外观,粗体显示特定日期,选择日期范围(用两个textBox显示)。
private void Form1_Load(object sender, EventArgs e)
{
monthCalendar1.TitleBackColor = System.Drawing.Color.Blue;//设置日历背景颜色
monthCalendar1.TrailingForeColor = System.Drawing.Color.Red;//设置日历其他日期为红色,然而没啥用
monthCalendar1.TitleForeColor = System.Drawing.Color.Yellow;//设置标题上的文字为黄色,也没啥用
monthCalendar1.ShowWeekNumbers = true;//显示周数
monthCalendar1.CalendarDimensions = new Size(2, 2);//设置多月份显示
DateTime special = new DateTime(2020, 6, 15);//实例化datetime类
monthCalendar1.AddBoldedDate(special);//使datetime实例化的对象日期粗体显示
monthCalendar1.UpdateBoldedDates();//重绘粗体格式日期
}
private void 日期范围(object sender, DateRangeEventArgs e)
{
//鼠标点击一个日期,然后再shift+点击另一个日期,得到一个日期范围(只能显示一周时间)
textBox1.Text = monthCalendar1.SelectionStart.ToString();
textBox2.Text = monthCalendar1.SelectionEnd.ToString();
}
多月份显示:
单月份显示:
3.ErrorProvider控件
该控件可以在不打扰用户的情况下显示有错误发生。当用户在窗体的输入或显示数据集内的错误时,会用到该控件。通过设置SetError方法来指定控件的错误方法。判断文本框是否输入正确,一般是在Validating事件中进行判断。
示例:用三个textbox控件来输入数据,如果输入不符预期,则判断错误,如果全部输入正确,则弹出消息框反馈。
public int a, b, c;
private void 文本框3(object sender, CancelEventArgs e)
{
if (textBox1.Text == "")
{
errorProvider1.SetError(textBox3, "不能为空");
}
else
{
errorProvider1.SetError(textBox3, "");//清空错误提示
c = 1;
}
}
private void 文本框2(object sender, CancelEventArgs e)
{
if (textBox2.Text == "")
{
errorProvider1.SetError(textBox2, "不能为空");
}
if (Regex.IsMatch(textBox2.Text.Trim(), "^\\d+$"))//如果文本框输入的字符串与数字字符串匹配,则返回true
{
errorProvider1.SetError(textBox2, "");
b = 1;
}
else if (!Regex.IsMatch(textBox2.Text.Trim(), "^\\d+$"))
{
errorProvider1.SetError(textBox2, "请输入一个数字!");
}
}
private void 文本框1(object sender, CancelEventArgs e)
{
if (textBox1.Text == "")
{
errorProvider1.SetError(textBox1, "不能为空");
}
else
{
errorProvider1.SetError(textBox1, "");
a = 1;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (a + b + c == 3)
{
MessageBox.Show("输入数据完毕","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
4.Timer控件
该控件可以定期引发事件,若启用该控件,则每个时间间隔发一个Tick事件,在该事件中添加要执行的代码,时间间隔有interval属性决定,单位为ms。
示例:制作一个系统时钟,每隔一秒获取当前时间并显示
private void TIck(object sender, EventArgs e)
{
textBox1.Text = DateTime.Now.ToString();//获取当前时间
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;//设置定时为1秒
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "启动")
{
timer1.Enabled = true;//timer开启
button1.Text = "停止";
}
else
{
timer1.Enabled = false;//timer关闭
button1.Text = "启动";
}
}
5。ProgressBar
该控件其实就是一个进度条。但是只能水平方向显示,如果想改变控件的显示样式,可以用ProgressBarRenderer类来实现,如纵向进度条,或在进度条上显示文本。
示例:实现一个进度条。
private void button2_Click(object sender, EventArgs e)
{
button2.Enabled = false;//在进度条加载过程中,将button2设置为不可点击状态
progressBar1.Maximum = 100000;//设置进度条最大值为100000
progressBar1.Minimum = 0;//设置进度条最小值为0
progressBar1.Step = 1;//每次进度条加1
for (int i = 0; i < 100000; i++)
{
progressBar1.PerformStep();//按照step=1的形式递增
textBox2.Text = "进度值为" + progressBar1.Value.ToString();
}
}