一个简易的便签工具:探索窗体之间传值和传引用的方法
2020-11-18 08:34
标签:style blog class code java tar 简单的便签工具:实现了基于文本的便签信息的浏览,删除,添加。可以查看时间,实时更新数据。 这个便签工具完全基于面向对象的编程方式,首先定义Note类,然后使用NoteManager类进行便签的管理。其中的关键就是在NoteManager类中定义list NoteManager的工作原理: public List private int CurrentIndex = 0;//指示当前便签所在位置 public Note GetCurrentNote; //一个只读属性,读取当前索引下的Note对象 public bool AddNewNote(Note note); //方法,想list public int Move(int direc, Label lblTip);
//方法,根据参数是-1还是1,移动CurrentIndex,达到翻页效果 而在Note.cs类中则定义了便签的基本属性: 窗体文件:frmAddNewNote.cs 窗体文件:frmMain.cs 这个小案例中有两点要特别关注: 1.首先是面向对象的思维方式,将每个Note便签都看做一个对象,对象里有各种属性。通过NoteManager对象管理Note对象。达到了指挥者的效果 2.其次便是窗体之间的传值,我这里使用的是构建中间件static class Record类,进行引用的传递 中间件: 使用中间件进行数据的共享很方便。 程序源码: 下载 一个简易的便签工具:探索窗体之间传值和传引用的方法,搜素材,soscw.com 一个简易的便签工具:探索窗体之间传值和传引用的方法 标签:style blog class code java tar 原文地址:http://www.cnblogs.com/hoosway/p/3703928.html//文件:NoteManager.cs
//作者:Me
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 简易便签
{
class NoteManager
{
public List//文件:Note.cs
//作者:Me
namespace 简易便签
{
class Note
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTime Time { get; set; }
}
}using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 简易便签
{
public partial class frmAddNewNote : Form
{
public frmAddNewNote()
{
InitializeComponent();
}
private void btnOK_Click(object sender, EventArgs e)
{
//manager.AddNewNote(new Note { Title=txtTitle.Text, Content=txtContent.Text, Time=DateTime.Now});
NoteManager manager = Record.manager;
manager.AddNewNote(new Note { Title = txtTitle.Text, Content = txtContent.Text, Time = DateTime.Now });
MessageBox.Show("添加成功!");
this.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace 简易便签
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
manger = new NoteManager();
}
private NoteManager manger;
private string[] allLines;
private void Form1_Load(object sender, EventArgs e)
{
skinEngine1.SkinFile = "Silver.ssk";
allLines = File.ReadAllLines("Note.txt", Encoding.Default);
foreach (string item in allLines)
{
string[] eachArray = item.Split(‘|‘);
manger.list.Add(new Note {
Id=Convert.ToInt32(eachArray[0]),
Title=eachArray[1],
Content=eachArray[2],
Time=Convert.ToDateTime(eachArray[3])
});
}
//窗体加载完成之后马上给值
WriteContent();
lblPage.Text = "1/" + manger.list.Count;
}
private void btnPrev_Click(object sender, EventArgs e)
{
//向前
int currentPost = manger.Move(-1, lblTip) + 1;
lblPage.Text = currentPost.ToString() + "/"+ manger.list.Count;
WriteContent();
}
private void btnNext_Click(object sender, EventArgs e)
{
//向后
int currentPost = manger.Move(1, lblTip) + 1;
lblPage.Text = currentPost.ToString() + "/" + manger.list.Count;
WriteContent();
}
private void WriteContent()
{
Note tempNote = manger.GetCurrentNote;
txtTitle.Text = tempNote.Title;
txtContent.Text = tempNote.Content;
lblDateTime.Text = tempNote.Time.ToString();
//取消文本选中
txtTitle.Select(0,0);
//标题获得焦点
txtTitle.Focus();
}
private void ClearAllText()
{
txtTitle.Text = "";
txtContent.Text = "";
lblDateTime.Text = "";
lblTip.Text = "";
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//这里必须和File.ReadAllLines()具有相同的编码,否则肯定乱码
Liststring> strArr = new Liststring>();
foreach (Note item in manger.list)
{
strArr.Add(item.Id+"|"+item.Title+"|"+item.Content+"|"+item.Time);
}
File.WriteAllLines("Note.txt", strArr, Encoding.Default);
}
private void TsBtnAddNew_Click(object sender, EventArgs e)
{
//manger.AddNewNote(new Note { Title=txtTitle.Text, Content=txtContent.Text, Time=DateTime.Now});
//frmAddNewNote fan = new frmAddNewNote();
//fan.SetNoteManage(manger);
//fan.ShowDialog();
frmAddNewNote fa = new frmAddNewNote();
Record.manager = manger;
fa.Show();
}
private void TsBtnClear_Click(object sender, EventArgs e)
{
ClearAllText();
}
private void TsTool_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void TsBtnUpdate_Click(object sender, EventArgs e)
{
manger.AddNewNote(new Note { Title = txtTitle.Text, Content = txtContent.Text, Time = DateTime.Now });
lblTip.Text = "已经更新!";
}
}
}
static class Record
{
public static NoteManager manager;
}