定时器
namespace AlarmClock
{
public partial class Main : Form
{
// 定时器刚启动时的秒数
private int secondStart = 0;
public Main()
{
InitializeComponent();
}
//定时器事件
private void timer1_Tick(object sender, EventArgs e)
{
// 需求:每间隔1秒钟,判断是否已经到休息时间,如果已经到休息时间,
// 需要打开新窗体提醒休息一会。
int minute = (int)numericUpDown1.Value;
int second = minute * 60;
secondStart++;
if (secondStart >= second) // 休息时间到
{
// 到达休息时间时,把定时器停止。timer1_Tick事件不再执行。
button2_Click(sender, e);// 隐藏Main窗体
Hide();// 拿到Tip窗体的实例,参数1:工作分钟数,参数2:Main窗体
Tip tipWindow = new Tip(minute, this);
// 打开窗体
tipWindow.ShowDialog();
}
}//启动闹钟
public void button1_Click(object sender, EventArgs e)
{
secondStart = 0; // 每次启动闹钟时,把走过的秒数重置。
button2.Enabled = true; // 停止闹钟按钮能用
timer1.Enabled = true; // 让定时器能用。
timer1.Start(); // 定时器启动,前提定时必须能用Enabled=true;
button1.Enabled = false;
}
//停止闹钟
private void button2_Click(object sender, EventArgs e)
{
button1.Enabled = true;
timer1.Stop(); // 定时器停止,前提定时必须能用Enabled=true;
button2.Enabled = false;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace AlarmClock
{
public partial class Tip : Form
{
// 10秒后,关闭此窗体
int secondClose = 10;
// 工作的的分钟数
int minute = 0;
// 音频的路径
static string path = Application.StartupPath + "/alarm.wav";
// 播放器
SoundPlayer sp = new SoundPlayer(path);
// 存储一下Main窗体
Main? mainForm = null;
public Tip(int min, Main main)
{
InitializeComponent();
minute = min; // 把Main窗体中的工作分钟数存储起来,将来在其他事件中使用。
mainForm = main; // 把Main窗体存储起来,将来在其他事件中使用。
}
//tip定时器事件
private void timer1_Tick(object sender, EventArgs e)
{
secondClose--;
label2.Text = $"{secondClose}秒后自动关闭";
if (secondClose <= 0)
{
timer1.Stop(); // 定时器停止
sp.Stop(); // 音频停止
Close(); // Tip窗口关闭
// 把Main窗体再显示出来
// mainForm?.Show();
if (mainForm != null)
{
mainForm.Show();
}
}
}
//TIP窗体加载时
private void Tip_Load(object sender, EventArgs e)
{
label1.Text = $"您已经工作{minute}分钟了,休息一会!";
label2.Text = $"{secondClose}秒后自动关闭";
timer1.Start();// 播放音频
sp.Play();
}
}
}
MVC
案例 登录注册-mvc版本
MODEL
1.AccontInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace WindowsFormsApp_mvc.Model
{
internal class AccontInfo
{//账号
public string Accont { get ; set ;}
//密码
public string Password { get ; set ;}
//获取本地所有账号信息
public static List<AccontInfo> GetAccontInfo(string filePath)
{
return JsonManage.JsonDeserialize<List<AccontInfo>>(FlieManage.ReadFile(filePath)) as List<AccontInfo>;
}//存储账号信息
public static void SaveAccontInfo(string account,string password)
{///获取本地所有账号信息
List<AccontInfo> list = GetAccontInfo(FliePathManage.UserInfoFilePath);
if (list == null)
{ //创建一个账号信息列表
list = new List<AccontInfo>();
}
else
{
//添加账号信息
list.Add(new AccontInfo()
{
Accont = account,
Password = password
});// 写入文件
FlieManage.WriteFile(FliePathManage.UserInfoFilePath, JsonManage.JsonSerialize(list));
}
}
}
}
2.FliePathManage.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace WindowsFormsApp_mvc
{
internal static class FliePathManage
{
// 用户信息文件路径
public static string UserInfoFilePath = @"UserInfo.txt";
}
}
3.JsonManage
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LitJson;
namespace WindowsFormsApp_mvc.Model
{
internal class JsonManage
{// Json 序列化
public static string JsonSerialize(object obj)
{
string jsonStr;
try
{
jsonStr = JsonMapper.ToJson(obj);
}
catch (Exception e)
{
Console.WriteLine("Json 序列化:" +e.Message);
return null;
}return jsonStr;
}
// Json 反序列化
public static object JsonDeserialize<T>(string json)
{
object jsonObj;
try
{
jsonObj = JsonMapper.ToObject<T>(json);}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}return jsonObj;
}}
}
4.FlieManage.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace WindowsFormsApp_mvc.Model
{
internal class FlieManage
{
//读取文件
public static string ReadFile(string filePath)
{
string content = "";
try
{
System.IO.StreamReader sr = new System.IO.StreamReader(filePath);
content = sr.ReadToEnd();
sr.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
return content;
}
//写入文件
public static bool WriteFile(string filePath, string content)
{
try
{
File.WriteAllText(filePath, content);
}
catch (Exception ex)
{
Console.WriteLine("写入文件"+ex.Message);
return false;
}return true;
}
}}
VIew-LoginForm
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;
using WindowsFormsAppLessonDay7.Model;
using System.IO;
namespace WindowsFormsAppLessonDay7
{
public partial class Form1 : Form
{// 两个控件 用于在control中使用
public TextBox AccontTextBox => textBox1;
public TextBox PasswordTextBox => textBox2;
// 登录成功事件 用于绑定control中方法
public event Action LoginSuccessEvent;public Form1()
{
InitializeComponent();
}
// 登录按钮点击事件
private void button1_Click(object sender, EventArgs e)
{
if (LoginSuccessEvent != null)
{
LoginSuccessEvent();
}}
// 登录成功后显示消息
public void ShowLoginDialog(string mes)
{
MessageBox.Show(mes);}
}
}
Control-LoginControl
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;
using WindowsFormsApp_mvc.Control;
using WindowsFormsApp_mvc.View;namespace WindowsFormsApp_mvc
{
public partial class LoginForm : Form
{
public event Action<string,string> LoginEvent;
public LoginForm()
{
InitializeComponent();
}private void button1_Click(object sender, EventArgs e)
{
if (LoginEvent != null)
{
LoginEvent(textBox1.Text,textBox2.Text);
}
}
// 登录状态显示
public void LoginState(string state)
{
MessageBox.Show(state);
}private void button2_Click(object sender, EventArgs e)
{
RegisteForm form = new RegisteForm();
RegisteContorl registeContorl = new RegisteContorl(form);
form.ShowDialog();
}
}
}
View -RegisteForm
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 WindowsFormsApp_mvc.View
{
public partial class RegisteForm : Form
{
// 注册事件
public event Action<string,string> registeEvent;
public RegisteForm()
{
InitializeComponent();
}// 注册按钮点击事件
private void button1_Click(object sender, EventArgs e)
{
if (registeEvent != null)
{
registeEvent(textBox1.Text,textBox2.Text);
}
}
}
}
Contorl-RegisteContorl
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WindowsFormsApp_mvc.Model;
using WindowsFormsApp_mvc.View;namespace WindowsFormsApp_mvc.Control
{
internal class RegisteContorl
{
//构造函数
public RegisteContorl(RegisteForm form)
{
form.registeEvent += Registe;
}
//注册逻辑
public void Registe(string account, string password)
{
Console.WriteLine("注册逻辑执行");
//保存用户信息
AccontInfo.SaveAccontInfo(account, password);}
}
}
入口类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp_mvc.Control;namespace WindowsFormsApp_mvc
{
internal static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);//登录界面
LoginForm loginForm = new LoginForm();
//登录控制
LoginControl loginControl = new LoginControl(loginForm);
//启动登录界面
Application.Run(loginForm);
}
}
}
SunnyUI
文档预览 - Gitee.com
1.通过 NuGet导入SunnyUI框架
2. 添加SunnyUI 控件 到工具
3.使用sunnyUI-Demo 学习基本控件的使用方式
使用sunnyUI-Demo 源码,可以再源码中选中自己喜欢的控件,比起工具箱更加的直观,选择好控件我们选择复制到我们自己的项目中即可。
比如 文字滚动条 复制到自己的项目中 对内容进行修改即可