A young programmer's C # project,
A small project is to be written today. We should prepare for writing projects. We need to perform project analysis. You need to know your needs and plan your projects as needed. We should use what we have learned to do the best program. Do your best to fulfill the project requirements. Simplified code!
The project we want to write today is called the online TV genie. The first thing we need to do is to parse the XML file we wrote in advance and display it in the TreeView space ., Select a Treeview node to display its corresponding information to DateGridView!
I have several requirements for details!Here are some eggs !!!
This is the form!
This is what it looks like to start
All classes look like this.
Code and comments for each class
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Chap _ network TV genie {// <summary> // TV channel parent class, abstract class /// </summary> public abstract class ChannelBase {// This is a bunch of automatic attributes. It is easy to receive Xml file information! Public string ChannelName {get; set;} public string Path {get; set;} public string ChannelType {get; set;} public List <TvProgram> list {get; set ;} public abstract void Fetch (); // a single-column generic set with the Data Type List <TvProgram> private List <TvProgram> ProgramList = new List <TvProgram> (); public List <TvProgram> ProgramList1 {get {return ProgramList;} set {ProgramList = value ;}}}}
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Chap _ network TV genie {public class ChannelFactory {// <summary> /// factory class /// </summary> /// <param name = "type"> </param> /// <returns> </returns> public static ChannelBase CreateChannel (string type) {ChannelBase channel = null; switch (type) {case "TypeA": channel = new TypeAChannel (); break; case "TypeB": channel = new TypeBChannel (); break; default: break;} return channel ;}}}
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. xml; namespace Chap _ network TV genie {// <summary> // program management class // </summary> class ChannelManager {// private List of Single-Column generic sets <ChannelBase> list = new List <ChannelBase> (); public List <ChannelBase> List {get {return list;} set {list = value ;}}// Method for parsing XML files public void ResolveChannels () {// 1.1 I will store parsed Data to a generic set // List <custom class> XmlDocument doc = new XmlDocument (); doc. load ("FullChannels. xml "); // find the root node XmlNode root = doc. documentElement; foreach (XmlNode item in root. childNodes) {// The Channel ChannelBase channel = Factory that an item represents. createFactory (item ["channelType"]. innerText); channel. channelType = item ["channelType"]. innerText; channel. channelName = item ["tvChannel"]. innerText; channel. path = item ["path"]. innerText; // belongs to the list in a collection. add (channel );}}}}
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Chap _ network TV genie {// <summary> /// simple Factory class /// </summary> class Factory {// a method with a static return value of the ChannelBase type public static ChannelBase CreateFactory (string type) {ChannelBase types = null; switch (type) {case "TypeA": types = new TypeAChannel (); break; case "TypeB": types = new TypeBChannel (); break ;} return types ;}}}
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Chap _ network TV genie {// <summary> // TV channel class // </summary> public class TvProgram {// program name public string ProgramName {get; set ;}// time range public string Meridien {get; set ;}// program Path public string Path {get; set ;}// time public DateTime PlayTime {get; set ;}}}
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. xml; namespace Chap _ network TV genie {// <summary> /// class A channel class /// </summary> class TypeAChannel: channelBase {// override the abstract method of the parent class, parse the XML file and bind it to the generic set public override void Fetch () {XmlDocument doc = new XmlDocument (); doc. load ("Beijing TV. xml "); XmlNode root = doc. documentElement; // find the root node foreach (XmlNode item in root. childNodes) // traverse the subnode {if (item. name. equals ("tvProgramTable") // determine whether the node Name is "tvProgramTable" {foreach (XmlNode items in item. childNodes) // traverse the subnode {TvProgram Tp = new TvProgram (); Tp. playTime = Convert. toDateTime (items ["playTime"]. innerText); Tp. meridien = items ["meridien"]. innerText; Tp. path = items ["path"]. innerText; Tp. programName = items ["programName"]. innerText; ProgramList1.Add (Tp); // bind to collection }}}}}}
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. xml; namespace Chap _ network TV genie {class TypeBChannel: ChannelBase {// Method for parsing XML files public override void Fetch () {// 1.1 I will store parsed Data to a generic set // List <custom class> XmlDocument doc = new XmlDocument (); doc. load ("Phoenix TV. xml "); // find the root node XmlNode root = doc. documentElement; foreach (XmlNode item in root. childNodes) // traverse the subnode {if (item. name. equals ("ProgramList") // determines whether the node Name is "tvProgramTable" {foreach (XmlNode items in item. childNodes) // traverse the subnode {TvProgram Tp = new TvProgram (); Tp. playTime = Convert. toDateTime (items ["playTime"]. innerText); Tp. path = items ["path"]. innerText; Tp. programName = items ["name"]. innerText; ProgramList1.Add (Tp); // bind to collection }}}}}}
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 System. xml; namespace Chap _ network TV genie {public partial class FrmMain: Form {public FrmMain () {InitializeComponent ();} bool flag = false; private void FrmMain_Load (object sender, eventArgs e) {Insert (); flag = true ;}
// Public void Insert () {TreeNode minenode = new TreeNode (); minenode. text = "my TV station"; // bind tvList. nodes. add (minenode); // The root node TreeNode root = new TreeNode (); root. text = "all TV stations"; // bind tvList. nodes. add (root); ChannelManager manager = new ChannelManager (); manager. resolveChannels (); List <ChannelBase> list = manager. list; foreach (ChannelBase item in list) {TreeNode tn = new TreeNode (); tn. text = item. channelName; tn. tag = item; root. nodes. add (tn) ;}} private void tvList_AfterSelect (object sender, TreeViewEventArgs e) {if (flag ){
// Right-click the menu to display if (tvList. selectedNode. level = 0) {ctms. items [0]. visible = false; ctms. items [1]. visible = false;} else if (tvList. selectedNode. parent. text = "my TV station") {ctms. items [0]. visible = true; ctms. items [1]. visible = false;} else if (tvList. selectedNode. parent. text = "all TV stations") {ctms. items [0]. visible = false; ctms. items [1]. visible = true;} TreeNode selectNode = tvList. selectedNode; if (selectNode. tag = null) {return;} ChannelBase channel = (ChannelBase) selectNode. tag; // fill the data channel in the LIst set. fetch (); List <TvProgram> list = channel. programList1; clear (list) ;}} public void clear (List <TvProgram> list) {dgvList. autoGenerateColumns = false; dgvList. dataSource = new BindingList <TvProgram> (list);} private void dgvList_CellContentClick (object sender, DataGridViewCellEventArgs e) {} private void joins my TV ToolStripMenuItem_Click (object sender, EventArgs e ){
// Right-click my TV station ChannelBase channel = (ChannelBase) tvList. selectedNode. tag; TreeNode node = new TreeNode (); node. text = channel. channelName; node. tag = channel; tvList. nodes [0]. nodes. add (node);} private void Delete ToolStripMenuItem_Click (object sender, EventArgs e) {// Delete the selected node XmlDocument doc = new XmlDocument (); doc. load ("FullChannels. xml "); // find the root node () XmlNode root = doc. documentElement; foreach (XmlNode item in root. childNodes) {if (item ["tvChannel"]. innerText = tvList. selectedNode. text) {tvList. selectedNode. remove (); break ;}}}}}
I am an egg !!!
All the code is above. Let's summarize the ideas! First, we resolve the XML file of the TV channel to a single-column generic set, and then bind the data of the set to the TreeView control and bind the Tag value to the ChannelBase object; then, bind DateGridView through the Tag value of the selected node to the generic set parsed by the TV program!