<1>委托的基本运用
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
委托的基本运用namespace 委托 { public delegate void funcDelegate(); class Program { static void Main(string[] args) { Wo w = new Wo(); w.func(); funcDelegate laozhai; laozhai = w.func; laozhai(); Console.ReadKey(); } } class Wo { public void func() { Console.WriteLine("旁边是213"); } }
<2>委托变量的传值处理
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
代码namespace 委托变量的传值处理 { public delegate void FucDelegate(); class Program { static void Main(string[] args) { while(true) { FucDelegate Myfunc=null; Console.WriteLine("请输入Y来确定方法的执行"); string m = Console.ReadLine(); if(m=="Y") { Myfunc = Say; } Fuc(Myfunc); Console.WriteLine(); Console.ReadKey(); } } public static void Fuc(FucDelegate function) { if (function != null) { function(); } } public static void Say() { Console.WriteLine("我出来啦"); } } }
<3>抓取图片
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
代码namespace 抓取图片 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { WebClient wc = new WebClient(); string html = wc.DownloadString(textBox1.Text.Trim()); // 2、正则提取 List<string> list = new List<string>(); MatchCollection ms = Regex.Matches(html, @"<\s?img[^>]+src=""([^""]+)"""); foreach (Match m in ms) { if (m.Success) { list.Add(m.Groups[1].Value.Trim()); } } // 与网页地址拼接得到图片的具体地址 string url = textBox1.Text.Trim(); for (int i = 0; i < list.Count; i++) { string temp = list[i]; temp = url + "/" + temp; list[i] = temp; } // 下载 string dir = textBox2.Text.Trim(); //这里http://192.168.1.142:8090要把最后的斜杠去掉。 if(!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } for (int i = 0; i < list.Count; i++) { string name = Regex.Match(list[i], @".+/(.+)").Groups[1].Value; wc.DownloadFile(list[i], Path.Combine(dir, name)); } MessageBox.Show("提取成功"); } private void Form1_Load(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { textBox2.Text = fbd.SelectedPath; } } } }
<4>抓取招聘信息
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
代码namespace 抓取招聘信息 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // 1、下载HTML代码 WebClient wc = new WebClient(); wc.Encoding = Encoding.Default; string html = wc.DownloadString(textBox1.Text.Trim()); // 2、正则提取 string regex = @"class=""jobname"" target=""_blank"">(.+)</a>"; MatchCollection ms = Regex.Matches(html, regex); StringBuilder sb = new StringBuilder(); foreach (Match m in ms) { if (m.Success) { sb.AppendFormat("{0}\t\r\n", m.Groups[1].Value); } } textBox2.Text = sb.ToString(); } private void Form1_Load(object sender, EventArgs e) { } } }
<5>自定义排序Form
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
代码namespace 自定义排序Form { public delegate bool CompairDelegate(string s1, string s2); public partial class Form1 : Form { public Form1() { InitializeComponent(); } List<string> list = new List<string>(); StringBuilder sb = new StringBuilder(); CompairDelegate MyFunc; Random r1 = new Random(); Random r2 = new Random(); public string[] nums; public int count; string str; private void button4_Click(object sender, EventArgs e) { sb.Clear(); count = r1.Next(5, 10); for (int j = 0; j < count; j++) { int count1 = r1.Next(5, 10); for (int i = 0; i < count1; i++) { char temp1 = Convert.ToChar(r2.Next(48, 57)); char temp2 = Convert.ToChar(r2.Next(65, 90)); char temp3 = Convert.ToChar(r2.Next(97, 122)); char[] temps = { temp1, temp2, temp3 }; sb.Append(temps); } list.Add(sb.ToString()); sb.Clear(); } nums = list.ToArray(); for (int i = 0; i < list.Count; i++) { sb.AppendFormat("{0}\t\r\n", list[i]); } textBox1.Text = sb.ToString(); } private void button1_Click(object sender, EventArgs e) { str = "1"; Myswitch(str); } private void button2_Click(object sender, EventArgs e) { str = "2"; Myswitch(str); } private void Form1_Load(object sender, EventArgs e) { } public void Myswitch(string str) { switch (str) { case "1": MyFunc = new CompairDelegate(MyCompair1); break; case "2": MyFunc = new CompairDelegate(MyCompair2); break; case "3": MyFunc = MyCompair3; break; default: MyFunc = MyCompair1; break; } Compair(nums, MyFunc); sb.Clear(); for (int i = 0; i < list.Count; i++) { sb.AppendFormat("{0}\t\r\n", nums[i]); } textBox1.Text = sb.ToString(); } public static void Compair(string[] nums, CompairDelegate Func) { for (int i = 0; i < nums.Length - 1; i++) { for (int j = 0; j < nums.Length - i - 1; j++) { // 传入什么样的方法,就做什么样的比较 if (Func(nums[j], nums[j + 1])) { string temp = nums[j]; nums[j] = nums[j + 1]; nums[j + 1] = temp; } } } } public static bool MyCompair1(string s1, string s2) { return string.Compare(s1, s2) > 0; } public static bool MyCompair2(string s1, string s2) { return s1.Length > s2.Length; } public static bool MyCompair3(string s1, string s2) { // 提取其中的数字,如果没有数字,默认为0 // 然后比较数字 // s1 > s2 则返回true int num1 = 0; int num2 = 0; Match m1 = Regex.Match(s1, @"-?\d+"); Match m2 = Regex.Match(s2, @"-?\d+"); if (m1.Success) { num1 = Convert.ToInt32(m1.Value); } if (m2.Success) { num2 = Convert.ToInt32(m2.Value); } return num1 > num2 ? true : false; } private void button3_Click(object sender, EventArgs e) { str = "3"; Myswitch(str); } } }
<6>组替换
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
代码namespace 组替换 { class Program { static void Main(string[] args) { //将一段文本中的MM/DD/YYYY格式的日期转换为YYYY-MM-DD格式 ,比如“我的生日是05/21/2010耶”转换为“我的生日是2010-05-21耶” string str1 = "我的生日是05/21/2010耶"; str1 = Regex.Replace(str1, @"(\d+)/(\d+)/(\d+)", "$3-$1-$2"); Console.WriteLine(str1); // 练习2:给一段文本中匹配到的url添加超链接 //比如把http://www.test.com替换为<a href="http://www.test.com"> http://www.test.com</a>。 string str2 = " http://www.test.com "; str2 = Regex.Replace(str2, @" (.+?) ",@"<a href=""$1""> $1</a>"); Console.WriteLine(str2); Console.ReadKey(); } } }
<7>委托链的应用
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
代码namespace 委托链 { public delegate void FucDelegate(); class Program { static void Main(string[] args) { MyControl m = new MyControl(); m.Close(); Console.ReadKey(); } } class JIQI1 { public void Close() { Console.WriteLine("机器1关闭"); } } class JIQI2 { public void Close() { Console.WriteLine("机器2关闭"); } } class JIQI3 { public void Close() { Console.WriteLine("机器3关闭"); } } class JIQI4 { public void Close() { Console.WriteLine("机器4关闭"); } } class MyControl { JIQI1 j1 = new JIQI1(); JIQI2 j2 = new JIQI2(); JIQI3 j3 = new JIQI3(); JIQI4 j4 = new JIQI4(); public FucDelegate Func; public MyControl() //在委托链上增加这些方法来控制。 { Func = j1.Close; Func += j2.Close; Func += j3.Close; Func += j4.Close; } public void Close() { Func(); //实现委托 } } }