0505.Net基础班第二十二天(委托、XML和播放器项目)

01索引器

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace _01索引器
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Person p = new Person();
14             p[0] = "张三";
15             p[1] = "李四";
16             p[1, 1] = "新数组";
17             p[2, 1] = "新数组";
18             //p["张三"] = 15;
19             //p["李四"] = 20;
20           //  p[]
21             //p[0] = 1;
22             //p[1] = 2;
23             //Console.WriteLine(p[0]);
24             //Console.WriteLine(p[1]);
25             Console.ReadKey();
26         }
27     }
28
29
30     public class Person
31     {
32         //int[] nums = new int[10];
33         //public int this[int index]
34         //{
35         //    get { return nums[index]; }
36         //    set { nums[index] = value; }
37         //}
38         //string[] names = new string[10];
39         ////public string this[int index]
40         ////{
41         ////    get { return names[index]; }
42         ////    set { names[index] = value; }
43         ////}
44
45         string[] names = new string[10];
46         string[] newNames = new string[20];
47         public string this[int index]
48         {
49             get { return names[index]; }
50             set { names[index] = value; }
51         }
52
53         public string this[int index,int n]
54         {
55             get { return newNames[index]; }
56             set { newNames[index] = value; }
57         }
58       ////  List<string> list = new List<string>();
59       //  Dictionary<string, int> dic = new Dictionary<string, int>();
60       //  public int this[string index]
61       //  {
62       //      get { return dic[index]; }
63       //      set { dic[index] = value; }
64       //  }
65
66
67
68
69
70     }
71 }

2、单例模式

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10
11 namespace _2_单例模式
12 {
13     public partial class Form1 : Form
14     {
15         public Form1()
16         {
17             InitializeComponent();
18         }
19
20         private void button1_Click(object sender, EventArgs e)
21         {
22             Form2 frm2 = Form2.GetSingle();//new Form2();
23             frm2.Show();
24         }
25     }
26 }

 1 namespace _2_单例模式
 2 {
 3     partial class Form1
 4     {
 5         /// <summary>
 6         /// 必需的设计器变量。
 7         /// </summary>
 8         private System.ComponentModel.IContainer components = null;
 9
10         /// <summary>
11         /// 清理所有正在使用的资源。
12         /// </summary>
13         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14         protected override void Dispose(bool disposing)
15         {
16             if (disposing && (components != null))
17             {
18                 components.Dispose();
19             }
20             base.Dispose(disposing);
21         }
22
23         #region Windows 窗体设计器生成的代码
24
25         /// <summary>
26         /// 设计器支持所需的方法 - 不要
27         /// 使用代码编辑器修改此方法的内容。
28         /// </summary>
29         private void InitializeComponent()
30         {
31             this.button1 = new System.Windows.Forms.Button();
32             this.SuspendLayout();
33             //
34             // button1
35             //
36             this.button1.Location = new System.Drawing.Point(357, 93);
37             this.button1.Name = "button1";
38             this.button1.Size = new System.Drawing.Size(75, 23);
39             this.button1.TabIndex = 0;
40             this.button1.Text = "button1";
41             this.button1.UseVisualStyleBackColor = true;
42             this.button1.Click += new System.EventHandler(this.button1_Click);
43             //
44             // Form1
45             //
46             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
47             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
48             this.ClientSize = new System.Drawing.Size(646, 408);
49             this.Controls.Add(this.button1);
50             this.Name = "Form1";
51             this.Text = "Form1";
52             this.ResumeLayout(false);
53
54         }
55
56         #endregion
57
58         private System.Windows.Forms.Button button1;
59     }
60 }

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10
11 namespace _2_单例模式
12 {
13     public partial class Form2 : Form
14     {
15         //全局唯一的单例
16         public static Form2 FrmSingle=null;
17
18         private Form2()
19         {
20             InitializeComponent();
21         }
22
23         public static Form2 GetSingle()
24         {
25             if (FrmSingle == null)
26             {
27                 FrmSingle = new Form2();
28             }
29             return FrmSingle;
30         }
31     }
32 }

 1 namespace _2_单例模式
 2 {
 3     partial class Form2
 4     {
 5         /// <summary>
 6         /// Required designer variable.
 7         /// </summary>
 8         private System.ComponentModel.IContainer components = null;
 9
10         /// <summary>
11         /// Clean up any resources being used.
12         /// </summary>
13         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
14         protected override void Dispose(bool disposing)
15         {
16             if (disposing && (components != null))
17             {
18                 components.Dispose();
19             }
20             base.Dispose(disposing);
21         }
22
23         #region Windows Form Designer generated code
24
25         /// <summary>
26         /// Required method for Designer support - do not modify
27         /// the contents of this method with the code editor.
28         /// </summary>
29         private void InitializeComponent()
30         {
31             this.SuspendLayout();
32             //
33             // Form2
34             //
35             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
36             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
37             this.ClientSize = new System.Drawing.Size(494, 360);
38             this.Name = "Form2";
39             this.Text = "Form2";
40             this.ResumeLayout(false);
41
42         }
43
44         #endregion
45     }
46 }

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 using System.Windows.Forms;
 6
 7 namespace _2_单例模式
 8 {
 9     static class Program
10     {
11         /// <summary>
12         /// 应用程序的主入口点。
13         /// </summary>
14         [STAThread]
15         static void Main()
16         {
17             Application.EnableVisualStyles();
18             Application.SetCompatibleTextRenderingDefault(false);
19             Application.Run(new Form1());
20         }
21     }
22 }

03创建XML

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Xml;
 7 namespace _03创建XML
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //通过代码来创建XML文档
14             //1、引用命名空间
15             //2、创建XML文档对象
16             XmlDocument doc = new XmlDocument();
17             //3、创建第一个行描述信息,并且添加到doc文档中
18             XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
19             doc.AppendChild(dec);
20             //4、创建根节点
21             XmlElement books = doc.CreateElement("Books");
22             //将根节点添加到文档中
23             doc.AppendChild(books);
24
25             //5、给根节点Books创建子节点
26             XmlElement book1 = doc.CreateElement("Book");
27             //将book添加到根节点
28             books.AppendChild(book1);
29
30
31             //6、给Book1添加子节点
32             XmlElement name1 = doc.CreateElement("Name");
33             name1.InnerText = "金瓶";
34             book1.AppendChild(name1);
35
36             XmlElement price1 = doc.CreateElement("Price");
37             price1.InnerText = "10";
38             book1.AppendChild(price1);
39
40             XmlElement des1 = doc.CreateElement("Des");
41             des1.InnerText = "好看";
42             book1.AppendChild(des1);
43
44             XmlElement book2 = doc.CreateElement("Book");
45             books.AppendChild(book2);
46
47
48             XmlElement name2 = doc.CreateElement("Name");
49             name2.InnerText = "金瓶";
50             book2.AppendChild(name2);
51
52             XmlElement price2= doc.CreateElement("Price");
53             price2.InnerText = "10";
54             book2.AppendChild(price2);
55
56             XmlElement des2 = doc.CreateElement("Des");
57             des2.InnerText = "好看";
58             book2.AppendChild(des2);
59
60             doc.Save("Books.xml");
61             Console.WriteLine("保存成功");
62             Console.ReadKey();
63         }
64     }
65 }

1 <?xml version="1.0" encoding="utf-8" ?>
2
3 <Books/>

4、创建带属性的XML文档

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Xml;
 7 namespace _4_创建带属性的XML文档
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             XmlDocument doc = new XmlDocument();
14             XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8","yes");
15             doc.AppendChild(dec);
16
17             XmlElement order = doc.CreateElement("Order");
18             doc.AppendChild(order);
19
20             XmlElement customerName = doc.CreateElement("CustomerName");
21             customerName.InnerXml = "<p>我是一个p标签</p>";
22             order.AppendChild(customerName);
23
24             XmlElement customerNumber = doc.CreateElement("CustomerNumber");
25             customerNumber.InnerText = "<p>我是一个p标签</p>";
26             order.AppendChild(customerNumber);
27
28
29             XmlElement items = doc.CreateElement("Items");
30             order.AppendChild(items);
31
32             XmlElement orderItem1 = doc.CreateElement("OrderItem");
33             //给节点添加属性
34             orderItem1.SetAttribute("Name", "充气娃娃");
35             orderItem1.SetAttribute("Count", "10");
36             items.AppendChild(orderItem1);
37
38             XmlElement orderItem2 = doc.CreateElement("OrderItem");
39             //给节点添加属性
40             orderItem2.SetAttribute("Name", "充气娃娃");
41             orderItem2.SetAttribute("Count", "10");
42             items.AppendChild(orderItem2);
43
44             XmlElement orderItem3 = doc.CreateElement("OrderItem");
45             //给节点添加属性
46             orderItem3.SetAttribute("Name", "充气娃娃");
47             orderItem3.SetAttribute("Count", "10");
48             items.AppendChild(orderItem3);
49
50             doc.Save("Order.xml");
51             Console.WriteLine("保存成功");
52             Console.ReadKey();
53
54
55         }
56     }
57 }

5、追击XML

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Xml;
 7 using System.IO;
 8 namespace _5_追击XML
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             //追加XML文档
15             XmlDocument doc = new XmlDocument();
16             XmlElement books;
17             if (File.Exists("Books.xml"))
18             {
19                 //如果文件存在 加载XML
20                 doc.Load("Books.xml");
21                 //获得文件的根节点
22                 books = doc.DocumentElement;
23             }
24             else
25             {
26                 //如果文件不存在
27                 //创建第一行
28                 XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
29                 doc.AppendChild(dec);
30                 //创建跟节点
31                 books = doc.CreateElement("Books");
32                 doc.AppendChild(books);
33             }
34             //5、给根节点Books创建子节点
35             XmlElement book1 = doc.CreateElement("Book");
36             //将book添加到根节点
37             books.AppendChild(book1);
38
39
40             //6、给Book1添加子节点
41             XmlElement name1 = doc.CreateElement("Name");
42             name1.InnerText = "c#开发大全";
43             book1.AppendChild(name1);
44
45             XmlElement price1 = doc.CreateElement("Price");
46             price1.InnerText = "110";
47             book1.AppendChild(price1);
48
49             XmlElement des1 = doc.CreateElement("Des");
50             des1.InnerText = "看不懂";
51             book1.AppendChild(des1);
52
53
54             doc.Save("Books.xml");
55             Console.WriteLine("保存成功");
56             Console.ReadKey();
57
58         }
59     }
60 }

6、读取XML文档

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Xml;
 7 namespace _6_读取XML文档
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //XmlDocument doc = new XmlDocument();
14             ////加载要读取的XML
15             //doc.Load("Books.xml");
16
17             ////获得根节点
18             //XmlElement books = doc.DocumentElement;
19
20             ////获得子节点 返回节点的集合
21             //XmlNodeList xnl = books.ChildNodes;
22
23             //foreach (XmlNode item in xnl)
24             //{
25             //    Console.WriteLine(item.InnerText);
26             //}
27             //Console.ReadKey();
28
29
30             //读取带属性的XML文档
31
32             //XmlDocument doc = new XmlDocument();
33             //doc.Load("Order.xml");
34             //Xpath
35
36             //XmlDocument doc = new XmlDocument();
37             //doc.Load("Order.xml");
38             //XmlNodeList xnl = doc.SelectNodes("/Order/Items/OrderItem");
39
40             //foreach (XmlNode node in xnl)
41             //{
42             //    Console.WriteLine(node.Attributes["Name"].Value);
43             //    Console.WriteLine(node.Attributes["Count"].Value);
44             //}
45             //Console.ReadKey();
46             //改变属性的值
47             //XmlDocument doc = new XmlDocument();
48             //doc.Load("Order.xml");
49             //XmlNode xn = doc.SelectSingleNode("/Order/Items/OrderItem[@Name=‘190‘]");
50             //xn.Attributes["Count"].Value = "200";
51             //xn.Attributes["Name"].Value = "颜世伟";
52             //doc.Save("Order.xml");
53             //Console.WriteLine("保存成功");
54
55
56
57
58             XmlDocument doc = new XmlDocument();
59
60             doc.Load("Order.xml");
61
62             XmlNode xn = doc.SelectSingleNode("/Order/Items");
63
64             xn.RemoveAll();
65             doc.Save("Order.xml");
66             Console.WriteLine("删除成功");
67             Console.ReadKey();
68
69             ////获得文档的根节点
70             //XmlElement order = doc.DocumentElement;
71             //XmlNodeList xnl = order.ChildNodes;
72             //foreach (XmlNode item in xnl)
73             //{
74             //    ////如果不是Items 就continue
75             //    //if (item[])
76             //    //{
77             //    //    continue;
78             //    //}
79             //    Console.WriteLine(item.Attributes["Name"].Value);
80             //    Console.WriteLine(item.Attributes["Count"].Value);
81             //}
82             Console.ReadKey();
83         }
84     }
85 }

7、为什么要使用委托

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace _7_为什么要使用委托
 8 {
 9     public delegate string DelProStr(string name);
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             //三个需求
15             //1、将一个字符串数组中每个元素都转换成大写
16             //2、将一个字符串数组中每个元素都转换成小写
17             //3、将一个字符串数组中每个元素两边都加上 双引号
18             string[] names = { "abCDefG", "HIJKlmnOP", "QRsTuvW", "XyZ" };
19             //ProStToUpper(names);
20             //ProStrToLower(names);
21             // ProStrSYH(names);
22
23             ProStr(names, delegate(string name)
24             {
25                 return "\"" + name + "\"";
26             });
27             for (int i = 0; i < names.Length; i++)
28             {
29                 Console.WriteLine(names[i]);
30             }
31             Console.ReadKey();
32         }
33
34
35         public static void ProStr(string[] name, DelProStr del)
36         {
37             for (int i = 0; i < name.Length; i++)
38             {
39                 name[i] = del(name[i]);
40             }
41         }
42
43         //public static string StrToUpper(string name)
44         //{
45         //    return name.ToUpper();
46         //}
47
48         //public static string StrToLower(string name)
49         //{
50         //    return name.ToLower();
51         //}
52
53         //public static string StrSYH(string name)
54         //{
55         //    return "\"" + name + "\"";
56         //}
57
58
59         //public static void ProStToUpper(string[] name)
60         //{
61         //    for (int i = 0; i < name.Length; i++)
62         //    {
63         //        name[i] = name[i].ToUpper();
64         //    }
65         //}
66         //public static void ProStrToLower(string[] name)
67         //{
68         //    for (int i = 0; i < name.Length; i++)
69         //    {
70         //        name[i] = name[i].ToLower();
71         //    }
72         //}
73         //public static void ProStrSYH(string[] names)
74         //{
75         //    for (int i = 0; i < names.Length; i++)
76         //    {
77         //        names[i] = "\"" + names[i] + "\"";
78         //    }
79         //}
80
81
82     }
83 }

8、委托概念

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading;
 6 using System.Threading.Tasks;
 7
 8 namespace _8_委托概念
 9 {
10     //声明一个委托指向一个函数
11     public delegate void DelSayHi(string name);
12     class Program
13     {
14         static void Main(string[] args)
15         {
16             //DelSayHi del = SayHiEnglish;//new DelSayHi(SayHiEnglish);
17             //del("张三");
18             //Console.ReadKey();
19
20             //Test("张三", SayHiChinese);
21             //Test("李四", SayHiEnglish);
22             //Console.ReadKey();
23         }
24
25         public static void Test(string name,DelSayHi del)
26         {
27             //调用
28             del(name);
29         }
30
31         public static void SayHiChinese(string name)
32         {
33             Console.WriteLine("吃了么?" + name);
34         }
35         public static void SayHiEnglish(string name)
36         {
37             Console.WriteLine("Nice to meet you" + name);
38         }
39     }
40 }

9、匿名函数

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace _9_匿名函数
 8 {
 9     public delegate void DelSayHi(string name);
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             //SayHi("张三", SayHiChinese);
15             //DelSayHi del = delegate(string name)
16             //{
17             //    Console.WriteLine("你好" + name);
18             //};
19             //del("张三");
20
21
22             //lamda表达式  => goes to
23             DelSayHi del = (string name) => { Console.WriteLine("你好" + name); };
24             del("张三");
25             Console.ReadKey();
26         }
27
28         //public static void SayHi(string name,DelSayHi del)
29         //{
30         //    del(name);
31         //}
32
33         //public static void SayHiChinese(string name)
34         //{
35         //    Console.WriteLine("你好"+name);
36         //}
37         //public static void SayHiEnglish(string name)
38         //{
39         //    Console.WriteLine("Hello"+name);
40         //}
41     }
42 }

10求数组的最大值

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace _10求数组的最大值
 8 {
 9     public delegate int DelCompare(object o1, object o2);
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             object[] o = {"abc","fdsfdsds","fdsfdsfdsfdsfdsfds","fdsfds"};
15             //object result = GetMax(o, Compare2);
16             //object result = GetMax(o, delegate(object o1, object o2) {
17             //    string s1 = (string)o1;
18             //    string s2 = (string)o2;
19             //    return s1.Length - s2.Length;
20             //});
21
22             object result = GetMax(o, (object o1,object o2) => {
23                 string s1 = (string)o1;
24                 string s2 = (string)o2;
25                 return s1.Length - s2.Length;
26             });
27             Console.WriteLine(result);
28             Console.ReadKey();
29         }
30
31         public static object GetMax(object[] nums, DelCompare del)
32         {
33             object max = nums[0];
34             for (int i = 0; i < nums.Length; i++)
35             {
36                 //要传一个比较的方法
37                 if (del(max, nums[i]) < 0)
38                 {
39                     max = nums[i];
40                 }
41             }
42             return max;
43         }
44
45
46         //public static int Compare1(object o1, object o2)
47         //{
48         //    int n1 = (int)o1;
49         //    int n2 = (int)o2;
50         //    return n1 - n2;
51         //}
52
53         //public static int Compare2(object o1, object o2)
54         //{
55         //    string s1 = (string)o1;
56         //    string s2 = (string)o2;
57         //    return s1.Length - s2.Length;
58         //}
59
60         //public static string GetMax(string[] names)
61         //{
62         //    string max = names[0];
63         //    for (int i = 0; i < names.Length; i++)
64         //    {
65         //        if (max.Length < names[i].Length)
66         //        {
67         //            max = names[i];
68         //        }
69         //    }
70         //    return max;
71         //}
72     }
73 }

11、泛型委托

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace _11_泛型委托
 8 {
 9     public delegate int DelCompare<T>(T t1, T t2);
10     // public delegate int DelCompare(object o1, object o2);
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             int[] nums = { 1, 2, 3, 4, 5 };
16             int max = GetMax<int>(nums, Compare1);
17             Console.WriteLine(max);
18
19             string[] names = { "abcdefg", "fdsfds", "fdsfdsfdsfdsfdsfdsfdsfsd" };
20             string max1 = GetMax<string>(names, (string s1, string s2) =>
21             {
22                 return s1.Length - s2.Length;
23             });
24             Console.WriteLine(max1);
25             Console.ReadKey();
26         }
27         public static T GetMax<T>(T[] nums, DelCompare<T> del)
28         {
29             T max = nums[0];
30             for (int i = 0; i < nums.Length; i++)
31             {
32                 //要传一个比较的方法
33                 if (del(max, nums[i]) < 0)
34                 {
35                     max = nums[i];
36                 }
37             }
38             return max;
39         }
40
41
42         public static int Compare1(int n1, int n2)
43         {
44             return n1 - n2;
45         }
46     }
47 }

12、lamda表达式

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace _12_lamda表达式
 8 {
 9
10     public delegate void DelOne();
11     public delegate void DelTwo(string name);
12     public delegate string DelThree(string name);
13     class Program
14     {
15         static void Main(string[] args)
16         {
17             DelOne del = () => { };// delegate() { };
18
19             DelTwo del2 = (string name) => { };//delegate(string name) { };
20
21             DelThree del3 = (string name) => { return name; };//delegate(string name) { return name; };
22
23
24             List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
25             list.RemoveAll(n => n > 4);
26             foreach (var item in list)
27             {
28                 Console.WriteLine(item);
29             }
30             Console.ReadKey();
31         }
32     }
33 }

13、使用委托来进行窗体传值

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace _13_使用委托来进行窗体传值
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13         }
14     }
15 }

14、窗体传值

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10
11 namespace _14_窗体传值
12 {
13     public partial class Form1 : Form
14     {
15         public Form1()
16         {
17             InitializeComponent();
18         }
19
20         private void button1_Click(object sender, EventArgs e)
21         {
22             Form2 frm2 = new Form2(ShowMsg);
23             frm2.Show();
24         }
25
26
27         void ShowMsg(string str)
28         {
29             label1.Text = str;
30         }
31     }
32 }

 1 namespace _14_窗体传值
 2 {
 3     partial class Form1
 4     {
 5         /// <summary>
 6         /// 必需的设计器变量。
 7         /// </summary>
 8         private System.ComponentModel.IContainer components = null;
 9
10         /// <summary>
11         /// 清理所有正在使用的资源。
12         /// </summary>
13         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14         protected override void Dispose(bool disposing)
15         {
16             if (disposing && (components != null))
17             {
18                 components.Dispose();
19             }
20             base.Dispose(disposing);
21         }
22
23         #region Windows 窗体设计器生成的代码
24
25         /// <summary>
26         /// 设计器支持所需的方法 - 不要
27         /// 使用代码编辑器修改此方法的内容。
28         /// </summary>
29         private void InitializeComponent()
30         {
31             this.button1 = new System.Windows.Forms.Button();
32             this.label1 = new System.Windows.Forms.Label();
33             this.SuspendLayout();
34             //
35             // button1
36             //
37             this.button1.Location = new System.Drawing.Point(431, 85);
38             this.button1.Name = "button1";
39             this.button1.Size = new System.Drawing.Size(75, 23);
40             this.button1.TabIndex = 0;
41             this.button1.Text = "button1";
42             this.button1.UseVisualStyleBackColor = true;
43             this.button1.Click += new System.EventHandler(this.button1_Click);
44             //
45             // label1
46             //
47             this.label1.AutoSize = true;
48             this.label1.Location = new System.Drawing.Point(137, 95);
49             this.label1.Name = "label1";
50             this.label1.Size = new System.Drawing.Size(41, 12);
51             this.label1.TabIndex = 1;
52             this.label1.Text = "label1";
53             //
54             // Form1
55             //
56             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
57             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
58             this.ClientSize = new System.Drawing.Size(628, 400);
59             this.Controls.Add(this.label1);
60             this.Controls.Add(this.button1);
61             this.Name = "Form1";
62             this.Text = "Form1";
63             this.ResumeLayout(false);
64             this.PerformLayout();
65
66         }
67
68         #endregion
69
70         private System.Windows.Forms.Button button1;
71         private System.Windows.Forms.Label label1;
72     }
73 }

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10
11 namespace _14_窗体传值
12 {
13
14     //声明一个委托
15     public delegate void DelTest(string str);
16     public partial class Form2 : Form
17     {
18         public DelTest _del;
19         public Form2(DelTest del)
20         {
21             this._del = del;
22             InitializeComponent();
23         }
24
25         private void button1_Click(object sender, EventArgs e)
26         {
27             _del(textBox1.Text);
28         }
29     }
30 }

 1 namespace _14_窗体传值
 2 {
 3     partial class Form2
 4     {
 5         /// <summary>
 6         /// Required designer variable.
 7         /// </summary>
 8         private System.ComponentModel.IContainer components = null;
 9
10         /// <summary>
11         /// Clean up any resources being used.
12         /// </summary>
13         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
14         protected override void Dispose(bool disposing)
15         {
16             if (disposing && (components != null))
17             {
18                 components.Dispose();
19             }
20             base.Dispose(disposing);
21         }
22
23         #region Windows Form Designer generated code
24
25         /// <summary>
26         /// Required method for Designer support - do not modify
27         /// the contents of this method with the code editor.
28         /// </summary>
29         private void InitializeComponent()
30         {
31             this.button1 = new System.Windows.Forms.Button();
32             this.textBox1 = new System.Windows.Forms.TextBox();
33             this.SuspendLayout();
34             //
35             // button1
36             //
37             this.button1.Location = new System.Drawing.Point(346, 140);
38             this.button1.Name = "button1";
39             this.button1.Size = new System.Drawing.Size(75, 23);
40             this.button1.TabIndex = 0;
41             this.button1.Text = "button1";
42             this.button1.UseVisualStyleBackColor = true;
43             this.button1.Click += new System.EventHandler(this.button1_Click);
44             //
45             // textBox1
46             //
47             this.textBox1.Location = new System.Drawing.Point(189, 140);
48             this.textBox1.Name = "textBox1";
49             this.textBox1.Size = new System.Drawing.Size(100, 21);
50             this.textBox1.TabIndex = 1;
51             //
52             // Form2
53             //
54             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
55             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
56             this.ClientSize = new System.Drawing.Size(485, 388);
57             this.Controls.Add(this.textBox1);
58             this.Controls.Add(this.button1);
59             this.Name = "Form2";
60             this.Text = "Form2";
61             this.ResumeLayout(false);
62             this.PerformLayout();
63
64         }
65
66         #endregion
67
68         private System.Windows.Forms.Button button1;
69         private System.Windows.Forms.TextBox textBox1;
70     }
71 }

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 using System.Windows.Forms;
 6
 7 namespace _14_窗体传值
 8 {
 9     static class Program
10     {
11         /// <summary>
12         /// 应用程序的主入口点。
13         /// </summary>
14         [STAThread]
15         static void Main()
16         {
17             Application.EnableVisualStyles();
18             Application.SetCompatibleTextRenderingDefault(false);
19             Application.Run(new Form1());
20         }
21     }
22 }

15多播委托

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace _15多播委托
 8 {
 9     public delegate void DelTest();
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             DelTest del = T1;
15             del += T2;
16             del += T3;
17             del+= T4;
18             del -= T3;
19             del -= T1;
20             del();
21             Console.ReadKey();
22         }
23
24         public static void T1()
25         {
26             Console.WriteLine("我是T1");
27         }
28         public static void T2()
29         {
30             Console.WriteLine("我是T2");
31         }
32
33         public static void T3()
34         {
35             Console.WriteLine("我是T3");
36         }
37         public static void T4()
38         {
39             Console.WriteLine("我是T4");
40         }
41     }
42 }

时间: 08-08

0505.Net基础班第二十二天(委托、XML和播放器项目)的相关文章

0505.Net基础班第二天(基础语法)

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _13_转移符 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 // Console.WriteLine("今天天气好晴\n朗处处好

0505.Net基础班第十二天(面向对象多态)

1.绝对路径和相对路径 绝对路径:通过给定的这个路径直接能在我的电脑中找到这个文件. 相对路径:文件相对于应用程序的路径. 结论: 我们在开发中应该去尽量的使用相对路径. 2.装箱.拆箱 装箱:就是将值类型转换为引用类型. 拆箱:将引用类型转换为值类型. 看两种类型是否发生了装箱或者拆箱,要看,这两种类型是否存在继承关系. 3.将创建文件流对象的过程写在using当中,会自动的帮助我们释放流所占用的资源. 4.实现多态的手段 1).虚方法 步骤: 1.将父类的方法标记为虚方法 ,使用关键字 vi

0505.Net基础班第二十一天(基础加强总复习)

1.取消播放器的自动播放功能 2.播放或者暂停按钮 3.下一曲.上一曲 4.多选删除 5.静音和放音 6.选择列表中的音乐文件,单击播放按钮直接播放 7.自动进行下一曲 15秒  44秒 当我和世界不一样 44.--47 那就让我不一样 lblInfomation.Text = musicPlayer.currentMedia.duration.ToString() + "\r\n" + musicPlayer.currentMedia.durationString + "\

委托实现音乐播放器 | 委托实现音乐播放器的缺点 | 事件实现音乐播放器

1,使用委托实现一个音乐播放器的功能呢.(模拟事件) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace 模拟事件 { /// <summary> /// 音乐播放器类 /// </summary> public class MusicP

0505.Net 基础班第十七天(GDI绘图)

基础知识复习+练习(带*为选做) 以下练习题做的时候如果遇到不会做的,请跳过本题继续向后做. 编写一段程序,运行时向用户提问“你考了多少分?(0~100)”,接受输入后判断其等级并显示出来.判断依据如下:等级={优 (90~100分):良 (80~89分):中 (60~69分):差 (0~59分):} 编程输出九九乘法表. 定义长度50的数组,随机给数组赋值,并可以让用户输入一个数字n,按一行n个数输出数组  int[]  array = new int[50];     Random r =

基础班第二天

<style type="text/css"> body{ width:1000px; border:2px solid #ccc; margin:20px auto; } img{margin-top:0; border:2px solid #ccc; } h2{ width:1000px; margin:10px auto; } p{ text-indent:2em; color:#313131; } a{ text-decoration:none; } div{ di

0505.Net基础班第十三天(面向对象多态)

1.c#中的访问修饰符 public :公开的公共的 private:私有的,只能在当前类的内部访问 protected:受保护的,只能在当前类的内部以及该类的子类中访问. internal:只能在当前项目中访问.在同一个项目中,internal和public的权限是一样. protected internal:protected+internal 1).能够修饰类的访问修饰符只有两个:public.internal. 2).可访问性不一致. 子类的访问权限不能高于父类的访问权限,会暴漏父类的成

0505.Net基础班第八天(飞行棋)

1.画游戏头 2.初始化地图(加载地图所需要的资源) 将整数数组中的数字编程控制台中显示的特殊字符串的这个过程 就是初始化地图 3.画地图 4.玩游戏 游戏规则: 如果玩家A踩到了玩家B  玩家B退6格  踩到了地雷 退6格 踩到了时空隧道 进10格 踩到了幸运轮盘 1交换位置  2 轰炸对方 使对方退6格 踩到了暂停  暂停一回合  踩到了方块  神马都不干 Map[50] if(map[40]==1) {  Console.WriteLine("◎"); } //初始化地图   M

0505.Net基础班第六天(复杂数据类型)

1.变量类型 int double string char bool decimal 变量的使用规则:先声明再赋值最后使用 int number; number=10;  number=20; Console.WriteLine(number); 2.Camel  Pascal 3.运算符 赋值运算符:= 复合赋值运算符:+= -= *= /= %=  sum+=age;  sum=sum+age; 算数运算符: + - * / % ++ -- 关系运算符: > < >= <= =