一个简易的便签工具:探索窗体之间传值和传引用的方法

2020-11-18 08:34

阅读:530

标签:style   blog   class   code   java   tar   

简单的便签工具:实现了基于文本的便签信息的浏览,删除,添加。可以查看时间,实时更新数据。

这个便签工具完全基于面向对象的编程方式,首先定义Note类,然后使用NoteManager类进行便签的管理。其中的关键就是在NoteManager类中定义list泛型集合。原理上和ArrayList基本是一致的。

NoteManager的工作原理:

  public List list;//定义一个泛型集合,并在构造函数中初始化。

  private int CurrentIndex = 0;//指示当前便签所在位置

  public Note GetCurrentNote; //一个只读属性,读取当前索引下的Note对象

  public bool AddNewNote(Note note); //方法,想list中添加新的Note对象

  public int Move(int direc, Label lblTip); //方法,根据参数是-1还是1,移动CurrentIndex,达到翻页效果

soscw.com,搜素材
//文件:NoteManager.cs
//作者:Me
using
System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace 简易便签 { class NoteManager { public List list; private int CurrentIndex = 0; public Note GetCurrentNote { get { return list[CurrentIndex]; } } //新增一个Note public bool AddNewNote(Note note) { //设置id note.Id = list.Count+1; list.Add(note); return true; } public NoteManager() { list = new List(); } //diec只能是-1或者1 // 0 1 2 3 4 length=5 //返回当前的CurrentIndex public int Move(int direc, Label lblTip) { //每移动一次,清空lblTip lblTip.Text = ""; if (direc == -1) { CurrentIndex --; if (CurrentIndex == -1) { CurrentIndex = 0; lblTip.Text = "已是第一张!"; } } if (direc == 1) { CurrentIndex ++; if (CurrentIndex == list.Count) { CurrentIndex = list.Count - 1; lblTip.Text = "已是最后一张!"; } } return CurrentIndex; } } }
soscw.com,搜素材

 

而在Note.cs类中则定义了便签的基本属性:

soscw.com,搜素材
//文件: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; } } }
soscw.com,搜素材

窗体文件:frmAddNewNote.cs

soscw.com,搜素材
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();
        }
    }
}
soscw.com,搜素材

窗体文件:frmMain.cs

soscw.com,搜素材
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 = "已经更新!";
                

    
        }


    }
}
soscw.com,搜素材

 

这个小案例中有两点要特别关注:

1.首先是面向对象的思维方式,将每个Note便签都看做一个对象,对象里有各种属性。通过NoteManager对象管理Note对象。达到了指挥者的效果

2.其次便是窗体之间的传值,我这里使用的是构建中间件static class Record类,进行引用的传递

  中间件:

    static class Record
    {
        public static NoteManager manager;
    }

  使用中间件进行数据的共享很方便。

程序源码: 下载

一个简易的便签工具:探索窗体之间传值和传引用的方法,搜素材,soscw.com

一个简易的便签工具:探索窗体之间传值和传引用的方法

标签:style   blog   class   code   java   tar   

原文地址:http://www.cnblogs.com/hoosway/p/3703928.html


评论


亲,登录后才可以留言!