C#电视节目单展示案例
?
需求:將文件中的電視節目單信息展示到列表中
步驟一:搭建電視節目單實體類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TVListProgram
{
??? /// <summary>
??? /// 電視節目單實體類
??? /// </summary>
??? public class TVList
??? {
??????? /// <summary>
??????? /// 電視節目時間
??????? /// </summary>
??????? public string TVTime { get; set; }
??????? /// <summary>
??????? /// 電視節目頻道
??????? /// </summary>
??????? public string Channel { get; set; }
??????? /// <summary>
??????? /// 電視節目名稱
??????? /// </summary>
??????? public string TVName { get; set; }
????? ?
??? }
}
步驟二:搭建數據操作類。注意數據操作類的命名一般是在實體類后面加上Service
?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace TVListProgram
{
??? /// <summary>
??? /// 電視節目單數據操作類
??? /// </summary>
? public? class TVListService
??? {
??????? /// <summary>
??????? /// 將文件對象封裝到List集合中并返回
??????? /// </summary>
??????? /// <param name="path">節目單文件路徑</param>
??????? /// <param name="tvDate">節目單日期</param>
??????? /// <returns>返回電視節目單和節目單日期</returns>
??????? public List<TVList> LoadTVList(string path,out string tvDate)
??????? {
??????????? //創建文件流和文件讀取器
??????????? FileStream fs = new FileStream(path, FileMode.Open);
??????????? StreamReader sr = new StreamReader(fs,Encoding.Default);
??????????? tvDate = sr.ReadLine(); //讀取文本第一行的電視節目日期
??????????? sr.ReadLine();//讀取空行
??????????? //循環讀取電視節目列表到最后一行終止
??????????? string content = string.Empty;
??????????? List<TVList> list = new List<TVList>();
??????????? string[] array = null; //保存一行分割后的內容
??????????? while (true)
??????????? {
??????????????? content = sr.ReadLine();
??????????????? if (content == null || content.Length == 0)//讀取到空的內容處,跳出循環
??????????????? {
??????????????????? break;
??????????????? }
??????????????? else
??????????????? {
??????????????????? array = content.Split(Convert.ToChar("#"));
??????????????????? list.Add(new TVList()
??????????????????? {
??????????????????????? TVTime =array[0],
??????????????????????? Channel=array[1],
??????????????????????? TVName=array[2]
??????????????????? });
??????????????? }
??????????? }
???????? ?
??????????? //關閉文件流,讀取器
??????????? fs.Close();
??????????? sr.Close();
??????????? return list;
??????? }
??? }
}
步驟三:加載按鈕編寫窗體程序
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 TVListProgram
{
??? public partial class FrmTVList : Form
??? {
??????? public FrmTVList()
??????? {
??????????? InitializeComponent();
??????????? this.dgvTVList.AutoGenerateColumns = false;
??????? }
??????? TVListService objTVListService = new TVListService();
??????? private void dgvTVList_CellContentClick(object sender, DataGridViewCellEventArgs e)
??????? {
??????? }
??????? private void btnLoadTVList_Click(object sender, EventArgs e)
??????? {
??????????? OpenFileDialog ofd = new OpenFileDialog();
??????????? DialogResult result = ofd.ShowDialog();
??????????? if (result == DialogResult.OK)
??????????? {
??????????????? string tvDate = string.Empty;
?????????????? List<TVList> list= objTVListService.LoadTVList(ofd.FileName,out tvDate);
???????????? ?
??????????????? this.dgvTVList.DataSource = list;
??????????????? this.lblTVDate.Text = tvDate;
??????????? }
??????? }
??? }
}
?
轉載于:https://www.cnblogs.com/cuig/p/8854029.html
總結
以上是生活随笔為你收集整理的C#电视节目单展示案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java加密种类
- 下一篇: [转]为什么Java中的HashMap默