C# WinForm是一种GUI应用程序框架,它允许开发人员使用各种控件来创建丰富的用户界面。以下是一些C# WinForm中常见的界面控件:这些界面控件在C# WinForm应用程序开发中非常常见,开发人员可以借助它们来快速创建各种类型的用户界面。

  • Button:按钮控件,用于添加按钮到用户界面以执行操作。
  • Label:标签控件,用于在用户界面上显示文本。
  • TextBox:文本框控件,允许用户输入文本信息。
  • ComboBox:组合框控件,用于在下拉列表中显示选项。
  • ListBox:列表框控件,用于在列表中显示多个选项。
  • RadioButton:单选按钮控件,可供用户进行单选选择。
  • CheckBox:复选框控件,可供用户进行多选选择。
  • DateTimePicker:日期时间选择控件,允许用户选择日期和时间。
  • ProgressBar:进度条控件,用于显示操作进度。

按钮与编辑框的使用

  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;
  9. using System.Windows.Forms;
  10. namespace WindowsFormsApplication1
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private void button1_Click(object sender, EventArgs e)
  19. {
  20. int start = int.Parse(textBox1.Text);
  21. int end = int.Parse(textBox2.Text);
  22. for (int x = start; x < end; x++)
  23. {
  24. progressBar1.Value += 1;
  25. Thread.Sleep(100);
  26. }
  27. }
  28. }
  29. }

listView

  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. using System.Threading;
  11. namespace WindowsFormsApplication5
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. private void Form1_Load(object sender, EventArgs e)
  20. {
  21. ColumnHeader Uid = new ColumnHeader();
  22. Uid.Text = "UID";
  23. Uid.Width = 100;
  24. Uid.TextAlign = HorizontalAlignment.Left;
  25. this.listView1.Columns.Add(Uid);
  26. ColumnHeader Uname = new ColumnHeader();
  27. Uname.Text = "UNAME";
  28. Uname.Width = 100;
  29. Uname.TextAlign = HorizontalAlignment.Left;
  30. this.listView1.Columns.Add(Uname);
  31. ColumnHeader Uage = new ColumnHeader();
  32. Uage.Text = "UAGE";
  33. Uage.Width = 100;
  34. Uage.TextAlign = HorizontalAlignment.Left;
  35. this.listView1.Columns.Add(Uage);
  36. }
  37. private void button1_Click(object sender, EventArgs e)
  38. {
  39. // tianjia shuju
  40. this.listView1.BeginUpdate(); //数据更新,UI暂时挂起
  41. this.listView1.Items.Clear(); //只移除所有的项。
  42. for (int x = 0; x < 100; x++)
  43. {
  44. ListViewItem lv = new ListViewItem();
  45. lv.ImageIndex = x;
  46. lv.Text = "UUID -> " + x;
  47. lv.SubItems.Add("第2列");
  48. lv.SubItems.Add("第3列" + x + "行");
  49. this.listView1.Items.Add(lv);
  50. }
  51. this.listView1.EndUpdate(); //结束数据处理,UI界面一次性绘制。
  52. }
  53. }
  54. }

MID 窗体设计:

1.首先插入新的子窗体form1,并设置IsMdiContainer = True 属性。

2.zi chuang ti bing she zhi ta men de fu chuang ti

form1.cs

  1. using System.Linq;
  2. using System.Text;
  3. using System.Threading.Tasks;
  4. using System.Windows.Forms;
  5. namespace WindowsFormsApplication3
  6. {
  7. public partial class Form1 : Form
  8. {
  9. public Form1()
  10. {
  11. InitializeComponent();
  12. }
  13. private void Form1_Load(object sender, EventArgs e)
  14. {
  15. Form2 frm2 = new Form2();
  16. frm2.Show();
  17. frm2.MdiParent = this;
  18. // pai lie
  19. LayoutMdi(MdiLayout.TileHorizontal);
  20. }
  21. }
  22. }

ji suan qi

  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. namespace 练习25
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private void Form1_Load(object sender, EventArgs e)
  19. {
  20. comboBox1.SelectedIndex = 0;
  21. }
  22. private void button1_Click(object sender, EventArgs e)
  23. {
  24. try
  25. {
  26. int n1 = Convert.ToInt32(textBox1.Text.Trim());
  27. int n2 = Convert.ToInt32(textBox2.Text.Trim());
  28. string oper = comboBox1.SelectedItem.ToString();
  29. switch (oper)
  30. {
  31. case "+": label1.Text = (n1 + n2).ToString();
  32. break;
  33. case "-": label1.Text = (n1 - n2).ToString();
  34. break;
  35. case "*": label1.Text = (n1 * n2).ToString();
  36. break;
  37. case "/": label1.Text = (n1 / n2).ToString();
  38. break;
  39. default: MessageBox.Show("请选择正确的操作符");
  40. break;
  41. }
  42. }
  43. catch
  44. {
  45. MessageBox.Show("请输入正确的数字");
  46. }
  47. }
  48. }
  49. }

浏览器控件的使用

  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. namespace _03_浏览器控件
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private void button1_Click(object sender, EventArgs e)
  19. {
  20. string text = textBox1.Text;
  21. Uri uri = new Uri("http://"+text);
  22. webBrowser1.Url = uri;
  23. }
  24. }
  25. }

ComboBox 控件

  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. namespace _04ComboBox
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private void button1_Click(object sender, EventArgs e)
  19. {
  20. //通过代码给下拉框添加数据
  21. comboBox2.Items.Add("张三");
  22. comboBox2.Items.Add("李四");
  23. comboBox2.Items.Add("王五");
  24. }
  25. private void button2_Click(object sender, EventArgs e)
  26. {
  27. comboBox2.Items.Clear();
  28. }
  29. }
  30. }

ComboBox 日期时间控件

  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. namespace _05日期选择器
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private void Form1_Load(object sender, EventArgs e)
  19. {
  20. //程序加载的时候 将年份添加到下拉框中
  21. //获得当前的年份
  22. int year = DateTime.Now.Year;
  23. for (int i = year; i >= 1949; i--)
  24. {
  25. cboYear.Items.Add(i + "年");
  26. }
  27. }
  28. /// 当年份发生改变的时候 加载月份
  29. private void cboYear_SelectedIndexChanged(object sender, EventArgs e)
  30. {
  31. //添加之前应该清空之前的内容
  32. cboMonth.Items.Clear();
  33. for (int i = 1; i <= 12; i++)
  34. {
  35. cboMonth.Items.Add(i + "月");
  36. }
  37. }
  38. /// 当月份发生改变的时候 加载天
  39. private void cboMonth_SelectedIndexChanged(object sender, EventArgs e)
  40. {
  41. cboDays.Items.Clear();
  42. int day = 0;//定义一个变量来存储天数
  43. //获得月份 7月 12
  44. string strMonth = cboMonth.SelectedItem.ToString().Split(new char[] { '月' }, StringSplitOptions.RemoveEmptyEntries)[0];
  45. string strYear = cboYear.SelectedItem.ToString().Split(new char[] { '年' }, StringSplitOptions.RemoveEmptyEntries)[0];
  46. // MessageBox.Show(cboMonth.SelectedItem.ToString());
  47. int year = Convert.ToInt32(strYear);
  48. int month = Convert.ToInt32(strMonth);
  49. switch (month)
  50. {
  51. case 1:
  52. case 3:
  53. case 5:
  54. case 7:
  55. case 8:
  56. case 10:
  57. case 12: day = 31;
  58. break;
  59. case 2:
  60. if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
  61. {
  62. day = 29;
  63. }
  64. else
  65. {
  66. day = 28;
  67. }
  68. break;
  69. default: day = 30;
  70. break;
  71. }
  72. for (int i = 1; i <= day; i++)
  73. {
  74. cboDays.Items.Add(i + "日");
  75. }
  76. }
  77. }
  78. }

ListBox 遍历与选中

  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. namespace _06ListBox控件
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private void Form1_Load(object sender, EventArgs e)
  19. {
  20. listBox1.Items.Add(1);
  21. listBox1.Items.Add(200000);
  22. }
  23. // 遍历选中
  24. private void button1_Click(object sender, EventArgs e)
  25. {
  26. for(int x=0;x<listBox1.Items.Count;x++)
  27. {
  28. if (listBox1.SelectedItems.Contains(listBox1.Items[x]))
  29. {
  30. MessageBox.Show(listBox1.Items[x].ToString());
  31. }
  32. }
  33. }
  34. }
  35. }

实现图片预览: 左面ListBox控件,右面是一个pictureBox控件。

  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. using System.IO;
  11. namespace _07实现点击更换照片
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. //用来存储图片文件的全路径
  20. List<string> list = new List<string>();
  21. private void Form1_Load(object sender, EventArgs e)
  22. {
  23. string[] path = Directory.GetFiles(@"C:\", "*.jpg");
  24. for (int i = 0; i < path.Length; i++)
  25. {
  26. string fileName = Path.GetFileName(path[i]);
  27. listBox1.Items.Add(fileName);
  28. //将图片的全路径添加到List泛型集合中
  29. list.Add(path[i]);
  30. //listBox1.Items.Add(path[i]);
  31. }
  32. }
  33. /// 双击播放图片
  34. private void listBox1_DoubleClick(object sender, EventArgs e)
  35. {
  36. pictureBox1.Image = Image.FromFile(list[listBox1.SelectedIndex]);
  37. }
  38. }
  39. }

双击播放音乐:

  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. using System.IO;
  11. using System.Media;
  12. namespace _08双击播放音乐
  13. {
  14. public partial class Form1 : Form
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. }
  20. //存储音乐文件的全路径
  21. List<string> listSongs = new List<string>();
  22. private void Form1_Load(object sender, EventArgs e)
  23. {
  24. string[] path = Directory.GetFiles(@"C:\Music", "*.wav");
  25. for (int i = 0; i < path.Length; i++)
  26. {
  27. string fileName = Path.GetFileName(path[i]);
  28. listBox1.Items.Add(fileName);
  29. //将音乐文件的全路径存到泛型集合中
  30. listSongs.Add(path[i]);
  31. }
  32. }
  33. private void listBox1_DoubleClick(object sender, EventArgs e)
  34. {
  35. SoundPlayer sp = new SoundPlayer();
  36. sp.SoundLocation=listSongs[listBox1.SelectedIndex];
  37. sp.Play();
  38. }
  39. }
  40. }

打开一个txt文件(打开文件对话框): 一个放大了的textbox

  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. using System.IO;
  11. namespace _10打开对话框
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. private void button1_Click(object sender, EventArgs e)
  20. {
  21. //点击弹出对话框
  22. OpenFileDialog ofd = new OpenFileDialog();
  23. //设置对话框的标题
  24. ofd.Title = "请选择要打开的文本";
  25. //设置对话框可以多选
  26. ofd.Multiselect = true;
  27. //设置对话框的初始目录
  28. ofd.InitialDirectory = @"C:\";
  29. //设置对话框的文件类型
  30. ofd.Filter = "文本文件|*.txt|媒体文件|*.wmv|图片文件|*.jpg|所有文件|*.*";
  31. //展示对话框
  32. ofd.ShowDialog();
  33. //获得在打开对话框中选中文件的路径
  34. string path = ofd.FileName;
  35. if (path == "")
  36. {
  37. return;
  38. }
  39. using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
  40. {
  41. byte[] buffer = new byte[1024 * 1024 * 5];
  42. //实际读取到的字节数
  43. int r = fsRead.Read(buffer, 0, buffer.Length);
  44. textBox1.Text = Encoding.Default.GetString(buffer, 0, r);
  45. }
  46. }
  47. }
  48. }

保存文件对话框:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace _11_保存文件对话框
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. private void button1_Click(object sender, EventArgs e)
  20. {
  21. SaveFileDialog sfd = new SaveFileDialog();
  22. sfd.Title = "请选择要保存的路径";
  23. sfd.InitialDirectory = @"C:\";
  24. sfd.Filter = "文本文件|*.txt|所有文件|*.*";
  25. sfd.ShowDialog();
  26. //获得保存文件的路径
  27. string path = sfd.FileName;
  28. if (path == "")
  29. {
  30. return;
  31. }
  32. using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
  33. {
  34. byte[] buffer = Encoding.Default.GetBytes(textBox1.Text);
  35. fsWrite.Write(buffer, 0, buffer.Length);
  36. }
  37. MessageBox.Show("保存成功");
  38. }
  39. }
  40. }

字体颜色对话框

  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. namespace _12_字体和颜色对话框
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. /// 字体对话框
  19. private void button1_Click(object sender, EventArgs e)
  20. {
  21. FontDialog fd = new FontDialog();
  22. fd.ShowDialog();
  23. textBox1.Font = fd.Font;
  24. }
  25. private void button2_Click(object sender, EventArgs e)
  26. {
  27. ColorDialog cd = new ColorDialog();
  28. cd.ShowDialog();
  29. textBox1.ForeColor = cd.Color;
  30. }
  31. private void Form1_Load(object sender, EventArgs e)
  32. {
  33. }
  34. }
  35. }

panel 显示隐藏面板

  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. namespace _13_Panel
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private void button1_Click(object sender, EventArgs e)
  19. {
  20. panel1.Visible = true;
  21. }
  22. private void button2_Click(object sender, EventArgs e)
  23. {
  24. panel1.Visible = false;
  25. }
  26. private void Form1_Load(object sender, EventArgs e)
  27. {
  28. }
  29. }
  30. }

简易记事本

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace _14_记事儿本应用程序
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. private void Form1_Load(object sender, EventArgs e)
  20. {
  21. //加载程序的时候 隐藏panel
  22. panel1.Visible = false;
  23. //取消文本框的自动换行功能
  24. textBox1.WordWrap = false;
  25. }
  26. /// 点击按钮的时候 隐藏panel
  27. private void button1_Click(object sender, EventArgs e)
  28. {
  29. panel1.Visible = false;
  30. }
  31. private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
  32. {
  33. panel1.Visible = true;
  34. }
  35. private void 影藏ToolStripMenuItem_Click(object sender, EventArgs e)
  36. {
  37. panel1.Visible = false;
  38. }
  39. List<string> list = new List<string>();
  40. /// 打开对话框
  41. private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
  42. {
  43. OpenFileDialog ofd = new OpenFileDialog();
  44. ofd.Title = "请选择要打开的文本文件";
  45. ofd.InitialDirectory = @"C:\Users\SpringRain\Desktop";
  46. ofd.Multiselect = true;
  47. ofd.Filter = "文本文件|*.txt|所有文件|*.*";
  48. ofd.ShowDialog();
  49. //获得用户选中的文件的路径
  50. string path = ofd.FileName;
  51. //将文件的全路径存储到泛型集合中
  52. list.Add(path);
  53. //获得了用户打开文件的文件名
  54. string fileName = Path.GetFileName(path);
  55. //将文件名放到ListBox中
  56. listBox1.Items.Add(fileName);
  57. if (path == "")
  58. {
  59. return;
  60. }
  61. using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
  62. {
  63. byte[] buffer = new byte[1024 * 1024 * 5];
  64. int r = fsRead.Read(buffer, 0, buffer.Length);
  65. textBox1.Text = Encoding.Default.GetString(buffer, 0, r);
  66. }
  67. }
  68. /// 保存对话框
  69. private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
  70. {
  71. SaveFileDialog sfd = new SaveFileDialog();
  72. sfd.InitialDirectory = @"C:\Users\SpringRain\Desktop";
  73. sfd.Title = "请选择要保存的文件路径";
  74. sfd.Filter = "文本文件|*.txt|所有文件|*.*";
  75. sfd.ShowDialog();
  76. //获得用户要保存的文件的路径
  77. string path = sfd.FileName;
  78. if (path == "")
  79. {
  80. return;
  81. }
  82. using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
  83. {
  84. byte[] buffer = Encoding.Default.GetBytes(textBox1.Text);
  85. fsWrite.Write(buffer, 0, buffer.Length);
  86. }
  87. MessageBox.Show("保存成功");
  88. }
  89. private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
  90. {
  91. if (自动换行ToolStripMenuItem.Text == "☆自动换行")
  92. {
  93. textBox1.WordWrap = true;
  94. 自动换行ToolStripMenuItem.Text = "★取消自动换行";
  95. }
  96. else if (自动换行ToolStripMenuItem.Text == "★取消自动换行")
  97. {
  98. textBox1.WordWrap = false;
  99. 自动换行ToolStripMenuItem.Text = "☆自动换行";
  100. }
  101. }
  102. private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
  103. {
  104. FontDialog fd = new FontDialog();
  105. fd.ShowDialog();
  106. textBox1.Font = fd.Font;
  107. }
  108. private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
  109. {
  110. ColorDialog cd = new ColorDialog();
  111. cd.ShowDialog();
  112. textBox1.ForeColor = cd.Color;
  113. }
  114. /// 双击打开对应的文件
  115. private void listBox1_DoubleClick(object sender, EventArgs e)
  116. {
  117. //要获得双击的文件所对应的全路径
  118. string path = list[listBox1.SelectedIndex];
  119. using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
  120. {
  121. byte[] buffer = new byte[1024 * 1024 * 5];
  122. int r = fsRead.Read(buffer, 0, buffer.Length);
  123. textBox1.Text = Encoding.Default.GetString(buffer, 0, r);
  124. }
  125. }
  126. }
  127. }

音乐选择框

  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. using System.IO;
  11. using System.Media;
  12. namespace _01复习
  13. {
  14. public partial class Form1 : Form
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. }
  20. //用来存储音乐文件的全路径
  21. List<string> listSongs = new List<string>();
  22. private void button1_Click(object sender, EventArgs e)
  23. {
  24. OpenFileDialog ofd = new OpenFileDialog();
  25. ofd.Title = "请选择音乐文件";
  26. ofd.InitialDirectory = @"C:\Users\SpringRain\Desktop\Music";
  27. ofd.Multiselect = true;
  28. ofd.Filter = "音乐文件|*.wav|所有文件|*.*";
  29. ofd.ShowDialog();
  30. //获得我们在文件夹中选择所有文件的全路径
  31. string[] path = ofd.FileNames;
  32. for (int i = 0; i < path.Length; i++)
  33. {
  34. //将音乐文件的文件名加载到ListBox中
  35. listBox1.Items.Add(Path.GetFileName(path[i]));
  36. //将音乐文件的全路径存储到泛型集合中
  37. listSongs.Add(path[i]);
  38. }
  39. }
  40. /// 实现双击播放
  41. SoundPlayer sp = new SoundPlayer();
  42. private void listBox1_DoubleClick(object sender, EventArgs e)
  43. {
  44. sp.SoundLocation=listSongs[listBox1.SelectedIndex];
  45. sp.Play();
  46. }
  47. /// 点击下一曲
  48. private void button3_Click(object sender, EventArgs e)
  49. {
  50. //获得当前选中歌曲的索引
  51. int index = listBox1.SelectedIndex;
  52. index++;
  53. if (index == listBox1.Items.Count)
  54. {
  55. index = 0;
  56. }
  57. //将改变后的索引重新的赋值给我当前选中项的索引
  58. listBox1.SelectedIndex = index;
  59. sp.SoundLocation = listSongs[index];
  60. sp.Play();
  61. }
  62. /// 点击上一曲
  63. private void button2_Click(object sender, EventArgs e)
  64. {
  65. int index = listBox1.SelectedIndex;
  66. index--;
  67. if (index < 0)
  68. {
  69. index = listBox1.Items.Count-1;
  70. }
  71. //将重新改变后的索引重新的赋值给当前选中项
  72. listBox1.SelectedIndex = index;
  73. sp.SoundLocation = listSongs[index];
  74. sp.Play();
  75. }
  76. private void Form1_Load(object sender, EventArgs e)
  77. {
  78. }
  79. }
  80. }

标签与随机数:

  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;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace _05_摇奖机应用程序
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. bool b = false;
  20. private void button1_Click(object sender, EventArgs e)
  21. {
  22. if (b == false)
  23. {
  24. b = true;
  25. button1.Text = "停止";
  26. Thread th = new Thread(PlayGame);
  27. th.IsBackground = true;
  28. th.Name = "新线程";
  29. // th.
  30. th.Start();
  31. }
  32. else//b==true
  33. {
  34. b = false;
  35. button1.Text = "开始";
  36. }
  37. //PlayGame();
  38. }
  39. private void PlayGame()
  40. {
  41. Random r = new Random();
  42. while (b)
  43. {
  44. label1.Text = r.Next(0, 10).ToString();
  45. label2.Text = r.Next(0, 10).ToString();
  46. label3.Text = r.Next(0, 10).ToString();
  47. }
  48. }
  49. private void Form1_Load(object sender, EventArgs e)
  50. {
  51. Control.CheckForIllegalCrossThreadCalls = false;
  52. }
  53. }
  54. }

Socket - client

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace Client
  15. {
  16. public partial class Form1 : Form
  17. {
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. }
  22. Socket socket;
  23. private void btnStart_Click(object sender, EventArgs e)
  24. {
  25. try
  26. {
  27. socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  28. IPAddress ip = IPAddress.Parse(txtServer.Text);
  29. IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(txtPort.Text));
  30. socket.Connect(point);
  31. ShowMsg("连接成功");
  32. Thread th = new Thread(Rec);
  33. th.IsBackground = true;
  34. th.Start();
  35. }
  36. catch
  37. { }
  38. }
  39. void Rec()
  40. {
  41. while (true)
  42. {
  43. try
  44. {
  45. byte[] buffer = new byte[1024 * 1024 * 3];
  46. int r = socket.Receive(buffer);
  47. if (buffer[0] == 0)
  48. {
  49. string s = Encoding.UTF8.GetString(buffer, 1, r-1);
  50. ShowMsg(s);
  51. }
  52. else if (buffer[0] == 1)
  53. {
  54. SaveFileDialog sfd = new SaveFileDialog();
  55. sfd.Filter = "所有文件|*.*";
  56. sfd.ShowDialog(this);
  57. string path = sfd.FileName;
  58. using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
  59. {
  60. fsWrite.Write(buffer, 1, r - 1);
  61. }
  62. MessageBox.Show("保存成功");
  63. }
  64. else
  65. {
  66. ZD();
  67. }
  68. }
  69. catch
  70. {
  71. }
  72. }
  73. }
  74. void ZD()
  75. {
  76. for (int i = 0; i < 500; i++)
  77. {
  78. this.Location = new Point(200, 200);
  79. this.Location = new Point(210, 210);
  80. }
  81. }
  82. void ShowMsg(string str)
  83. {
  84. txtLog.AppendText(str + "\r\n");
  85. }
  86. /// 给服务器发送消息
  87. private void btnSend_Click(object sender, EventArgs e)
  88. {
  89. byte[] buffer = Encoding.UTF8.GetBytes(txtMsg.Text);
  90. socket.Send(buffer);
  91. }
  92. private void Form1_Load(object sender, EventArgs e)
  93. {
  94. Control.CheckForIllegalCrossThreadCalls = false;
  95. }
  96. }
  97. }

socket-server

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace Server
  15. {
  16. public partial class Form1 : Form
  17. {
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. }
  22. private void btnStart_Click(object sender, EventArgs e)
  23. {
  24. try
  25. {
  26. Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  27. IPAddress ip = IPAddress.Any;//IPAddress.Parse(txtServer.Text);
  28. IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(txtPort.Text));
  29. socketWatch.Bind(point);
  30. ShowMsg("监听成功");
  31. //去厕所蹲坑
  32. socketWatch.Listen(10);
  33. //不停的接收客户端的连接
  34. Thread th = new Thread(Listen);
  35. th.IsBackground = true;
  36. th.Start(socketWatch);
  37. }
  38. catch
  39. { }
  40. }
  41. Dictionary<string, Socket> dicSocket = new Dictionary<string, Socket>();
  42. void Listen(object o)
  43. {
  44. Socket socketWatch = o as Socket;
  45. while (true)
  46. {
  47. //循环的接收客户端的连接
  48. Socket socketSend = socketWatch.Accept();
  49. //将客户端的IP地址存储到下拉框中
  50. cboUsers.Items.Add(socketSend.RemoteEndPoint.ToString());
  51. //将IP地址和这个客户端的Socket放到键值对集合中
  52. dicSocket.Add(socketSend.RemoteEndPoint.ToString(), socketSend);
  53. ShowMsg(socketSend.RemoteEndPoint.ToString() + ":" + "连接成功");
  54. //客户端连接成功后,就应高接收客户端发来的消息
  55. Thread th = new Thread(Rec);
  56. th.IsBackground = true;
  57. th.Start(socketSend);
  58. }
  59. }
  60. void Rec(object o)
  61. {
  62. Socket socketSend = o as Socket;
  63. while (true)
  64. {
  65. try
  66. {
  67. byte[] buffer = new byte[1024 * 1024 * 3];
  68. int r = socketSend.Receive(buffer);
  69. string str = Encoding.UTF8.GetString(buffer, 0, r);
  70. ShowMsg(socketSend.RemoteEndPoint.ToString() + ":" + str);
  71. }
  72. catch
  73. { }
  74. }
  75. }
  76. void ShowMsg(string str)
  77. {
  78. txtLog.AppendText(str + "\r\n");
  79. }
  80. private void Form1_Load(object sender, EventArgs e)
  81. {
  82. Control.CheckForIllegalCrossThreadCalls = false;
  83. }
  84. /// 服务器给客户端发送消息
  85. private void btnSend_Click(object sender, EventArgs e)
  86. {
  87. byte[] buffer = Encoding.UTF8.GetBytes(txtMsg.Text);
  88. //获得客户端的ip
  89. string ip = cboUsers.SelectedItem.ToString();
  90. List<byte> list = new List<byte>();
  91. list.Add(0);
  92. list.AddRange(buffer);
  93. byte[] newBuffer = list.ToArray();
  94. dicSocket[ip].Send(newBuffer);
  95. }
  96. private void btnSelect_Click(object sender, EventArgs e)
  97. {
  98. OpenFileDialog ofd = new OpenFileDialog();
  99. ofd.Filter = "所有文件|*.*";
  100. ofd.ShowDialog();
  101. txtPath.Text = ofd.FileName;
  102. }
  103. private void btnSendFile_Click(object sender, EventArgs e)
  104. {
  105. string path = txtPath.Text;
  106. using (FileStream fsRead = new FileStream(path, FileMode.Open, FileAccess.Read))
  107. {
  108. byte[] buffer = new byte[1024 * 1024 * 3];
  109. int r = fsRead.Read(buffer, 0, buffer.Length);
  110. List<byte> list = new List<byte>();
  111. list.Add(1);
  112. list.AddRange(buffer);
  113. byte[] newBuffer = list.ToArray();
  114. string ip = cboUsers.SelectedItem.ToString();
  115. dicSocket[ip].Send(newBuffer, 0, r+1, SocketFlags.None);
  116. }
  117. }
  118. private void btnZD_Click(object sender, EventArgs e)
  119. {
  120. byte[] buffer = new byte[1];
  121. buffer[0] = 2;
  122. string ip = cboUsers.SelectedItem.ToString();
  123. dicSocket[ip].Send(buffer);
  124. }
  125. private void button1_Click(object sender, EventArgs e)
  126. {
  127. }
  128. }
  129. }

GDI 绘制登录验证码 需要一个pictureBox1控件

  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. namespace 使用GDI绘制验证码
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. /// 点击更换验证码
  19. private void pictureBox1_Click(object sender, EventArgs e)
  20. {
  21. Random r = new Random();
  22. string str = null;
  23. for (int i = 0; i < 5; i++)
  24. {
  25. int rNumber = r.Next(0, 10);
  26. str += rNumber;
  27. }
  28. // MessageBox.Show(str);
  29. //创建GDI对象
  30. Bitmap bmp = new Bitmap(150, 40);
  31. Graphics g = Graphics.FromImage(bmp);
  32. for (int i = 0; i < 5; i++)
  33. {
  34. Point p = new Point(i * 20, 0);
  35. string[] fonts = { "微软雅黑", "宋体", "黑体", "隶书", "仿宋" };
  36. Color[] colors = { Color.Yellow, Color.Blue, Color.Black, Color.Red, Color.Green };
  37. g.DrawString(str[i].ToString(), new Font(fonts[r.Next(0, 5)], 20, FontStyle.Bold), new SolidBrush(colors[r.Next(0, 5)]), p);
  38. }
  39. for (int i = 0; i < 20; i++)
  40. {
  41. Point p1=new Point(r.Next(0,bmp.Width),r.Next(0,bmp.Height));
  42. Point p2=new Point(r.Next(0,bmp.Width),r.Next(0,bmp.Height));
  43. g.DrawLine(new Pen(Brushes.Green), p1, p2);
  44. }
  45. for (int i = 0; i < 500; i++)
  46. {
  47. Point p=new Point(r.Next(0,bmp.Width),r.Next(0,bmp.Height));
  48. bmp.SetPixel(p.X, p.Y, Color.Black);
  49. }
  50. //将图片镶嵌到PictureBox中
  51. pictureBox1.Image = bmp;
  52. }
  53. private void Form1_Load(object sender, EventArgs e)
  54. {
  55. }
  56. }
  57. }

GDI 图形绘制:

  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. namespace 使用GDI绘制简单的图形
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private void Form1_Load(object sender, EventArgs e)
  19. {
  20. //一根笔 颜色 一张纸 两点 绘制直线的对象
  21. }
  22. private void button1_Click(object sender, EventArgs e)
  23. {
  24. //创建GDI对象
  25. Graphics g = this.CreateGraphics();// new Graphics();
  26. //创建画笔对象
  27. Pen pen = new Pen(Brushes.Red);
  28. //创建两个点
  29. Point p1 = new Point(30, 50);
  30. Point p2 = new Point(250, 250);
  31. g.DrawLine(pen, p1, p2);
  32. }
  33. int i = 0;
  34. private void Form1_Paint(object sender, PaintEventArgs e)
  35. {
  36. i++;
  37. label1.Text = i.ToString();
  38. Graphics g = this.CreateGraphics();// new Graphics();
  39. //创建画笔对象
  40. Pen pen = new Pen(Brushes.Red);
  41. //创建两个点
  42. Point p1 = new Point(30, 50);
  43. Point p2 = new Point(250, 250);
  44. g.DrawLine(pen, p1, p2);
  45. }
  46. private void button2_Click(object sender, EventArgs e)
  47. {
  48. Graphics g = this.CreateGraphics();
  49. Pen pen=new Pen(Brushes.Yellow);
  50. Size size=new System.Drawing.Size(80,80);
  51. Rectangle rec=new Rectangle(new Point(50,50),size);
  52. g.DrawRectangle(pen,rec);
  53. }
  54. private void button3_Click(object sender, EventArgs e)
  55. {
  56. Graphics g = this.CreateGraphics();
  57. Pen pen=new Pen(Brushes.Blue);
  58. Size size=new System.Drawing.Size(180,180);
  59. Rectangle rec=new Rectangle(new Point(150,150),size);
  60. g.DrawPie(pen, rec, 60, 60);
  61. }
  62. private void button4_Click(object sender, EventArgs e)
  63. {
  64. Graphics g = this.CreateGraphics();
  65. g.DrawString("百度网盘下载最快", new Font("宋体", 20, FontStyle.Underline), Brushes.Black, new Point(300, 300));
  66. }
  67. }
  68. }

窗体传值:

form1.cs 只有一个 label1 和一个button

  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. namespace 窗体传值
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private void button1_Click(object sender, EventArgs e)
  19. {
  20. Form2 frm2 = new Form2(ShowMsg);
  21. frm2.Show();
  22. }
  23. void ShowMsg(string s)
  24. {
  25. label1.Text = s;
  26. }
  27. }
  28. }

form2.cs 一个textbox1 和一个button

  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. namespace 窗体传值
  11. {
  12. public delegate void DelStr(string s);
  13. public partial class Form2 : Form
  14. {
  15. public DelStr _del;
  16. // 子窗口中的del赋值给this._del 父窗体,完成的窗体进程数据传输。
  17. public Form2(DelStr del)
  18. {
  19. this._del = del;
  20. InitializeComponent();
  21. }
  22. private void button1_Click(object sender, EventArgs e)
  23. {
  24. // 给父窗体赋值
  25. _del(textBox1.Text);
  26. }
  27. }
  28. }

创建xml

  1. using System.Xml;
  2. 通过代码来创建XML文档
  3. XmlDocument doc = new XmlDocument();
  4. 创建第一个行描述信息,并且添加到doc文档中
  5. XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
  6. doc.AppendChild(dec);
  7. 创建根节点
  8. XmlElement books = doc.CreateElement("Books");
  9. 将根节点添加到文档中
  10. doc.AppendChild(books);
  11. 给根节点Books创建子节点
  12. XmlElement book1 = doc.CreateElement("Book");
  13. book添加到根节点
  14. books.AppendChild(book1);
  15. Book1添加子节点
  16. XmlElement name1 = doc.CreateElement("Name");
  17. name1.InnerText = "你好";
  18. book1.AppendChild(name1);

写入一个XML

  1. 1、创建一个XML文档对象
  2. XmlDocument doc = new XmlDocument();
  3. 2、创建第一行描述信息
  4. XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
  5. 3、将创建的第一行数据添加到文档中
  6. doc.AppendChild(dec);
  7. 4、给文档添加根节点
  8. XmlElement books = doc.CreateElement("Books");
  9. 5、将根节点添加给文档对象
  10. doc.AppendChild(books);
  11. 6、给根节点添加子节点
  12. XmlElement book1 = doc.CreateElement("Book");
  13. 将子节点book1添加到根节点下
  14. books.AppendChild(book1);
  15. 7、给book1添加子节点
  16. XmlElement bookName1 = doc.CreateElement("BookName");
  17. 8、设置标签中显示的文本
  18. bookName1.InnerText = "水浒传";
  19. book1.AppendChild(bookName1);
  20. XmlElement author1 = doc.CreateElement("Author");
  21. author1.InnerText = "<authorName>匿名</authorName>";
  22. book1.AppendChild(author1);
  23. XmlElement price1 = doc.CreateElement("Price");
  24. price1.InnerXml = "<authorName>匿名</authorName>";
  25. book1.AppendChild(price1);
  26. XmlElement des1 = doc.CreateElement("Des");
  27. des1.InnerXml = "好看";
  28. book1.AppendChild(des1);
  29. Console.WriteLine("保存成功");
  30. doc.Save("Book.xml");
  31. Console.ReadKey();

追加xml

  1. 追加XML文档
  2. XmlDocument doc = new XmlDocument();
  3. XmlElement books;
  4. if (File.Exists("Books.xml"))
  5. {
  6. 如果文件存在 加载XML
  7. doc.Load("Books.xml");
  8. 获得文件的根节点
  9. books = doc.DocumentElement;
  10. }
  11. else
  12. {
  13. 如果文件不存在
  14. 创建第一行
  15. XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
  16. doc.AppendChild(dec);
  17. 创建跟节点
  18. books = doc.CreateElement("Books");
  19. doc.AppendChild(books);
  20. }
  21. 5、给根节点Books创建子节点
  22. XmlElement book1 = doc.CreateElement("Book");
  23. book添加到根节点
  24. books.AppendChild(book1);
  25. 6、给Book1添加子节点
  26. XmlElement name1 = doc.CreateElement("Name");
  27. name1.InnerText = "c#开发大全";
  28. book1.AppendChild(name1);
  29. XmlElement price1 = doc.CreateElement("Price");
  30. price1.InnerText = "110";
  31. book1.AppendChild(price1);
  32. XmlElement des1 = doc.CreateElement("Des");
  33. des1.InnerText = "看不懂";
  34. book1.AppendChild(des1);
  35. doc.Save("Books.xml");
  36. Console.WriteLine("保存成功");
  37. Console.ReadKey();

读取XML

  1. 加载要读取的XML
  2. XmlDocument doc = new XmlDocument();
  3. doc.Load("Books.xml");
  4. 获得根节点
  5. XmlElement books = doc.DocumentElement;
  6. 获得子节点 返回节点的集合
  7. XmlNodeList xnl = books.ChildNodes;
  8. foreach (XmlNode item in xnl)
  9. {
  10. Console.WriteLine(item.InnerText);
  11. }
  12. Console.ReadKey();
  13. 读取带属性的XML文档
  14. XmlDocument doc = new XmlDocument();
  15. doc.Load("Order.xml");
  16. Xpath
  17. XmlDocument doc = new XmlDocument();
  18. doc.Load("Order.xml");
  19. XmlNodeList xnl = doc.SelectNodes("/Order/Items/OrderItem");
  20. foreach (XmlNode node in xnl)
  21. {
  22. Console.WriteLine(node.Attributes["Name"].Value);
  23. Console.WriteLine(node.Attributes["Count"].Value);
  24. }
  25. Console.ReadKey();
  26. 改变属性的值
  27. XmlDocument doc = new XmlDocument();
  28. doc.Load("Order.xml");
  29. XmlNode xn = doc.SelectSingleNode("/Order/Items/OrderItem[@Name='190']");
  30. xn.Attributes["Count"].Value = "200";
  31. xn.Attributes["Name"].Value = "颜世伟";
  32. doc.Save("Order.xml");
  33. Console.WriteLine("保存成功");
  34. XmlDocument doc = new XmlDocument();
  35. doc.Load("Order.xml");
  36. XmlNode xn = doc.SelectSingleNode("/Order/Items");
  37. xn.RemoveAll();
  38. doc.Save("Order.xml");
  39. Console.WriteLine("删除成功");
  40. Console.ReadKey();
  41. 获得文档的根节点
  42. xmlelement order = doc.documentelement;
  43. xmlnodelist xnl = order.childnodes;
  44. foreach (xmlnode item in xnl)
  45. {
  46. 如果不是items continue
  47. if (item[])
  48. {
  49. continue;
  50. }
  51. console.writeline(item.attributes["name"].value);
  52. console.writeline(item.attributes["count"].value);
  53. }
  54. Console.ReadKey();

增删改查:

  1. XMLDocument
  2. #region 对xml文档实现追加的需求
  3. XmlDocument doc = new XmlDocument();
  4. 首先判断xml文档是否存在 如果存在 则追加 否则创建一个
  5. if (File.Exists("Student.xml"))
  6. {
  7. 加载进来
  8. doc.Load("Student.xml");
  9. 追加
  10. 获得根节点 给根节点添加子节点
  11. XmlElement person = doc.DocumentElement;
  12. XmlElement student = doc.CreateElement("Student");
  13. student.SetAttribute("studentID", "10");
  14. person.AppendChild(student);
  15. XmlElement name = doc.CreateElement("Name");
  16. name.InnerXml = "我是新来哒";
  17. student.AppendChild(name);
  18. XmlElement age = doc.CreateElement("Age");
  19. age.InnerXml = "18";
  20. student.AppendChild(age);
  21. XmlElement gender = doc.CreateElement("Gender");
  22. gender.InnerXml = "女";
  23. student.AppendChild(gender);
  24. }
  25. else
  26. {
  27. 不存在
  28. XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
  29. doc.AppendChild(dec);
  30. XmlElement person = doc.CreateElement("Person");
  31. doc.AppendChild(person);
  32. XmlElement student = doc.CreateElement("Student");
  33. student.SetAttribute("studentID", "110");
  34. person.AppendChild(student);
  35. XmlElement name = doc.CreateElement("Name");
  36. name.InnerXml = "张三三李思思";
  37. student.AppendChild(name);
  38. XmlElement age = doc.CreateElement("Age");
  39. age.InnerXml = "28";
  40. student.AppendChild(age);
  41. XmlElement gender = doc.CreateElement("Gender");
  42. gender.InnerXml = "男";
  43. student.AppendChild(gender);
  44. }
  45. doc.Save("Student.xml");
  46. Console.WriteLine("保存成功");
  47. #endregion
  48. #region 读取XML文档
  49. XmlDocument doc = new XmlDocument();
  50. doc.Load("OrDER.xml");
  51. 还是 先获得根节点
  52. XmlElement order = doc.DocumentElement;
  53. 获得根节点下面的所有子节点
  54. XmlNodeList xnl = order.ChildNodes;
  55. foreach (XmlNode item in xnl)
  56. {
  57. Console.WriteLine(item.InnerText);
  58. }
  59. XmlElement items = order["Items"];
  60. XmlNodeList xnl2 = items.ChildNodes;
  61. foreach (XmlNode item in xnl2)
  62. {
  63. Console.WriteLine(item.Attributes["Name"].Value);
  64. Console.WriteLine(item.Attributes["Count"].Value);
  65. if (item.Attributes["Name"].Value == "手套")
  66. {
  67. item.Attributes["Count"].Value = "新来哒";
  68. }
  69. }
  70. doc.Save("OrDER.xml");
  71. #endregion
  72. #region 使用XPath的方式来读取XML文件
  73. XmlDocument doc = new XmlDocument();
  74. doc.Load("order.xml");
  75. 获得根节点
  76. XmlElement order = doc.DocumentElement;
  77. XmlNode xn = order.SelectSingleNode("/Order/Items/OrderItem[@Name='雨衣']");
  78. Console.WriteLine(xn.Attributes["Name"].Value);
  79. xn.Attributes["Count"].Value = "woshi new";
  80. doc.Save("order.xml");
  81. Console.WriteLine("保存成功");
  82. #endregion
  83. XmlDocument doc = new XmlDocument();
  84. doc.Load("order.xml");
  85. doc.RemoveAll();不行 根节点不允许删除
  86. XmlElement order = doc.DocumentElement;
  87. order.RemoveAll();移除根节点下的所有子节点
  88. XmlNode xn = order.SelectSingleNode("/Order/Items/OrderItem[@Name='雨衣']");
  89. orderItem去删除属性
  90. XmlNode orderItem = order.SelectSingleNode("/Order/Items/OrderItem");
  91. 获得Items节点
  92. XmlNode items = order["Items"];order.SelectSingleNode("/Order/Items");
  93. items.RemoveChild(xn);移除当前节点
  94. orderItem.RemoveAttributeNode(xn.Attributes["Count"]);
  95. xn.Attributes.RemoveNamedItem("Count");
  96. doc.Save("order.xml");
  97. Console.WriteLine("删除成功");
  98. Console.ReadKey();

C# WinForm 界面控件的更多相关文章

  1. WinForm开发-界面控件到实体,实体到界面控件自动绑定

    在WinForm开发中,我们是不是为绑定界面控件的数据而每个控件每个控件的赋值?在保存修改时是不是也是每个控件每个控件的赋值到实体中?字段一多,那简直就是噩梦.有没有像Web中那样方便的方法直接就自动 ...

  2. WinForm 简易仿360界面控件

    因为经常要做一些1.2千行的小工具,WinForm自带的TabCtrl又不美观,所以想做成360的样子,在网上找来找去,都只有散乱的代码,没有可以通用的结构,于是自己写了一个简易的通用控件. 控件主要 ...

  3. WinForm 清空界面控件值的小技巧

    原文:WinForm 清空界面控件值的小技巧 在WinForm里面有时候需要清空自己输入内容或是选择的选项,以便重新操作流程,那么一般你是怎么清空界面各个控件值的呢?如果窗体里面控件,尤其是TextB ...

  4. 在DevExpress程序中使用Winform分页控件直接录入数据并保存

    一般情况下,我们都倾向于使用一个组织比较好的独立界面来录入或者展示相关的数据,这样处理比较规范,也方便显示比较复杂的数据.不过在一些情况下,我们也可能需要直接在GridView表格上直接录入或者修改数 ...

  5. C#多线程操作界面控件的解决方案(转)

    C#中利用委托实现多线程跨线程操作 - 张小鱼 2010-10-22 08:38 在使用VS2005的时候,如果你从非创建这个控件的线程中访问这个控件或者操作这个控件的话就会抛出这个异常.这是微软为了 ...

  6. 基于MVC4+EasyUI的Web开发框架形成之旅--界面控件的使用

    在前面介绍了两篇关于我的基于MVC4+EasyUI技术的Web开发框架的随笔,本篇继续介绍其中界面部分的一些使用知识,包括控件的赋值.取值.清空,以及相关的使用. 我们知道,一般Web界面包括的界面控 ...

  7. winform基本控件----按钮

    这次来引用一个我们上课时候老师给的一个实验内容,来说一下winform程序设计中的按钮控件的使用.下面是我们老师给的实验内容. 实验目的: 掌握Winform的开发环境. 掌握窗体的创建和基本方法. ...

  8. 转--基于MVC4+EasyUI的Web开发框架形成之旅--界面控件的使用

    原文  http://www.cnblogs.com/wuhuacong/p/3317223.html 基于MVC4+EasyUI的Web开发框架形成之旅--界面控件的使用 在前面介绍了两篇关于我的基 ...

  9. C# WinForm实现控件拖动实例介绍

    主要是设计控件的MouseDown.MouseLeave.MouseMove事件.一步步来吧:1.定义一个枚举类型,描述光标状态 private enum EnumMousePointPosition ...

  10. Winform DevExpress控件库(一) DevExpress控件库的安装与新建第一个DevExpress项目

    前言:因为这段时间要接触到DevExpress控件库,而我本身甚至对winform的控件都了解甚少,所以处在学习中,写下博客主要是为了方便后期的回顾,当然也可以给一些新人第一次接触时做为学习的参考,以 ...

随机推荐

  1. ACM:快读读入技巧

    快速读入:当数据输入较大时,比scanf快 inline int read(){ int s=0,w=1; char ch=getchar(); while(ch<'0'||ch>'9') ...

  2. KYOCERA Programming Contest 2021(AtCoder Beginner Contest 200)(AB水题,C思维,D搜索,E DP)

    补题链接:Here A - Century 整除 \(200\) 并且判断能否整除完全 B - 200th ABC-200 按题意即可 C - Ringo's Favorite Numbers 2 求 ...

  3. 负载均衡--rpc服务端

    1. dubbo负载均衡的作用? 其出发点,自然也就是普通的负载均衡器的出发点了. 将负载均衡功能实现在rpc客户端侧,以便能够随时适应外部的环境变化,更好地发挥硬件作用. 而且客户端的负载均衡天然地 ...

  4. 嵌入式软件工程师笔试面试指南-ARM体系与架构

    哈喽,大家好.我终于回来了!19号刚提交完大论文,就被抓去出差了,折腾了整整一周,26号晚上,才回到学校.鸽了好久都没更新干货了.今天更新一篇关于Arm的笔试面试题目,文章内容已同步更新在github ...

  5. plsql打开报错:Control 'dxDockBrowserPanel' has no parent window问题解决

    一.现象: 使用plsql登陆oracle数据库时,登陆信息没有报错,但是最后一步报错,重启电脑依然没有解决 一直报:" Control 'dxDockBrowserPanel' has n ...

  6. FinalShell上传文件失败

    1.问题 上传文件失败,如图所示,即使切换至root用户 2.解决方式 这里在建立SSH连接时,就必须使用root用户,而若使用普通用户,即使在其中切换至root用户,也无法上传. 所以重新建立一个r ...

  7. 【SHELL】在指定格式的文件中查找字符串

    在指定格式的文件中查找字符串 grep -nr "string" --include=*.{c,cpp,h} 在排除指定格式的文件中查找字符串 grep -nr "str ...

  8. 【FreeRTOS】堆内存管理

    动态内存分配及其与FreeRTOS的相关性 为了使FreeRTOS更易用,内核对象(如任务.队列.信号量.事件组)不在编译期静态分配,而是在运行时动态分配,FreeRTOS在内核对象创建时分配RAM, ...

  9. 【TouchGFX】AnalogClock 小部件使用小记

  10. phpcms - 获取单网页 , 例如关于我们

       {pc:get sql="select * from phpcms_page where catid=2" num="1"}         {loop  ...