大家好,我是皮皮猫吖!
每文一言:祝大家新年快乐,身体健康!
新年新气象:
今天是新年的第一天。春节更文,这是人干的事情么?为了让大家学习到更多知识,春节来更文。希望大家可以在此篇文章中学到想学的知识。
希望大家在新的一年里,多读书,多看报,多敲代码,少睡觉!照顾好自己的身体,身体最重要!
本篇文章:
主要是C#创建并运行窗体项目、C#窗体项目中简单控件的使用、窗体项目中实现定时器。希望本篇文章对大家有所帮助。
正文如下:
一、Viusal Studio创建C#窗体项目及简单控件使用
1)创建一个C#窗体项目:
① 新建项目
- 选择模板,是系统提供的模板,可以自己做自己的模板
② 设置文件保存位置
③ 创建成功页面
2)C#简单控件学习及调试:
① 控件学习:
- buton控件
② 相关调试
③ 发布项目配置【我的visual studio找不到,不知道为什么】
3)C#点击按钮,文本输出
- 实现效果:点击注册,文本内容填充;点击登录,文本内容注销
- C#编程,主要是学习form1.cs视图化和编程控件响应代码
- 效果展示
- 代码:
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 WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//按钮1的点击函数
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
radioButton2.Checked = false;
textBox2.Text = "";
}
//按钮2的点击函数
private void button2_Click(object sender, EventArgs e)
{
//在视图中控件的name属性值,可视为该控件的对象名
textBox1.Text = "xm";
radioButton2.Checked = true;
textBox2.Text = "123456";
}
}
}
二、C#实现窗体定时器
1)定时器界面展示:
2)定时器所需控件
- label:文本
- button:按钮
- combobox:下拉框
- item:属性设置内容
- progressBar:进度条
3)代码
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 WindowsFormsApp3
{
public partial class Form1 : Form
{
//定义一个计数器变量
int count;
int time;
public Form1()
{
InitializeComponent();
}
//窗口弹出之后进行的函数
private void Form1_Load(object sender, EventArgs e)
{
int i;
for(i = 1; i < 100; i++)
{
//下拉框的内容填充
//任意格式转换成字符类型的,使用Tostring()方法
//也可以在视图化的里面进行更改,函数中的填充会覆盖试图中的填充
//空格会防止出现1位数据报错
comboBox1.Items.Add(i.ToString()+" 秒");
}
comboBox1.Text = "1 秒";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
count = 0;
//获得下拉框的内容
String str = comboBox1.Text;
//从下拉框中拿到整数:获取字符串中的前两个字符
//汉字在这里如果查找的话算1个
time = Convert.ToInt16(str.Substring(0, 2));
//进度条设置最大值
progressBar1.Maximum = time;
//开始计时
timer1.Start();
}
//定时器函数
private void timer1_Tick(object sender, EventArgs e)
{
//定时器+1
count++;
//设置进度条:进度条更新
progressBar1.Value = count;
//更新标签的内容
label3.Text = (time - count).ToString() + "秒";
if (count == time)
{
//关闭定时器
timer1.Stop();
//设置提示音(可以修改)
System.Media.SystemSounds.Asterisk.Play();
//弹出框提示
//内容在前,标题在后
MessageBox.Show("时间到了!!!", "提示");
}
}
}
}
- 【注意】:
- ① 弹出提示框需要设置在定时器结束之前
- ② 初始化多选框需要在代码中实现
4)效果展示
希望本篇文章对大家有所帮助,后续会继续分享C#相关知识【C#实现简单串口助手、C#实现网络编程…】C#继续学习中…
如果文章内容有错误的地方,请在留言处留下你的见解,方便大家共同学习。谢谢。
作者:皮皮猫吖