Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 11k
Category:

ADO-ODBC

Development Platform:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using Microsoft.Win32;
  8. namespace ch4_4
  9. {
  10. /// <summary>
  11. /// Form1 的摘要说明。
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15. private System.Windows.Forms.GroupBox groupBox1;
  16. private System.Windows.Forms.Label label1;
  17. private System.Windows.Forms.RadioButton radioButton1;
  18. private System.Windows.Forms.RadioButton radioButton2;
  19. private System.Windows.Forms.RadioButton radioButton3;
  20. private System.Windows.Forms.Button button1;
  21. private System.Windows.Forms.Button button2;
  22. private System.Windows.Forms.Label label2;
  23. private System.Windows.Forms.ComboBox comboBox1;
  24. private System.Windows.Forms.Button button3;
  25. /// <summary>
  26. /// 必需的设计器变量。
  27. /// </summary>
  28. private System.ComponentModel.Container components = null;
  29. public Form1()
  30. {
  31. //
  32. // Windows 窗体设计器支持所必需的
  33. //
  34. InitializeComponent();
  35.             InitialComboBox();
  36. InitialForm1();
  37. //
  38. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  39. //
  40. }
  41.          
  42. private void InitialComboBox()
  43. {
  44. //将FormBorderStyle枚举值读入到comboBox中去
  45. for (int i=0;i<7;i++)
  46. {
  47. this.comboBox1.Items.Add(((FormBorderStyle)i).ToString());
  48. }
  49. this.comboBox1.SelectedIndex=1;
  50. }
  51. private void InitialForm1()
  52. {
  53. //获取或者新建Form1主键
  54. RegistryKey  hLocalMachine=Registry.LocalMachine;
  55. RegistryKey  software=hLocalMachine.OpenSubKey("SOFTWARE",true);
  56. RegistryKey  newNode=software.CreateSubKey("Form1");
  57. //如果主键下面包含的子键数目不为0,则读取子键
  58. if (newNode.SubKeyCount!=0)
  59. {
  60. RegistryKey  formColor=newNode.CreateSubKey("formColor");
  61. //获取color键值
  62. string color=formColor.GetValue("color").ToString();
  63. RegistryKey  borderStyle=newNode.CreateSubKey("borderStyle");
  64. //获取borderStyle键值
  65. string style=borderStyle.GetValue("borderStyle").ToString();
  66. //根据color键值设定radioButton的checked属性
  67. if (color=="red") 
  68. {
  69. this.BackColor=Color.Red;
  70. this.radioButton1.Checked=true;
  71. }
  72. if (color=="yellow") 
  73. {
  74. this.BackColor=Color.Yellow;
  75. this.radioButton2.Checked=true;
  76. }
  77. //将string转化为int
  78. int i=Int32.Parse(style);
  79. //根据color的键值设定FormBorderStyle属性
  80. FormBorderStyle fStyle=(FormBorderStyle)i;
  81. this.FormBorderStyle=fStyle;
  82. this.comboBox1.SelectedIndex=i;     
  83. }
  84. //如果主键下面包含的子键数目为0,则进行默认初始化
  85. else
  86. {
  87. this.radioButton3.Checked=true;
  88. this.comboBox1.SelectedIndex=1;
  89. }
  90. }
  91. /// <summary>
  92. /// 清理所有正在使用的资源。
  93. /// </summary>
  94. protected override void Dispose( bool disposing )
  95. {
  96. if( disposing )
  97. {
  98. if (components != null) 
  99. {
  100. components.Dispose();
  101. }
  102. }
  103. base.Dispose( disposing );
  104. }
  105. #region Windows Form Designer generated code
  106. /// <summary>
  107. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  108. /// 此方法的内容。
  109. /// </summary>
  110. private void InitializeComponent()
  111. {
  112. this.groupBox1 = new System.Windows.Forms.GroupBox();
  113. this.comboBox1 = new System.Windows.Forms.ComboBox();
  114. this.label2 = new System.Windows.Forms.Label();
  115. this.radioButton3 = new System.Windows.Forms.RadioButton();
  116. this.radioButton2 = new System.Windows.Forms.RadioButton();
  117. this.radioButton1 = new System.Windows.Forms.RadioButton();
  118. this.label1 = new System.Windows.Forms.Label();
  119. this.button1 = new System.Windows.Forms.Button();
  120. this.button2 = new System.Windows.Forms.Button();
  121. this.button3 = new System.Windows.Forms.Button();
  122. this.groupBox1.SuspendLayout();
  123. this.SuspendLayout();
  124. // 
  125. // groupBox1
  126. // 
  127. this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
  128. this.comboBox1,
  129. this.label2,
  130. this.radioButton3,
  131. this.radioButton2,
  132. this.radioButton1,
  133. this.label1});
  134. this.groupBox1.Location = new System.Drawing.Point(8, 16);
  135. this.groupBox1.Name = "groupBox1";
  136. this.groupBox1.Size = new System.Drawing.Size(280, 120);
  137. this.groupBox1.TabIndex = 0;
  138. this.groupBox1.TabStop = false;
  139. this.groupBox1.Text = "设置";
  140. // 
  141. // comboBox1
  142. // 
  143. this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  144. this.comboBox1.Location = new System.Drawing.Point(136, 72);
  145. this.comboBox1.Name = "comboBox1";
  146. this.comboBox1.Size = new System.Drawing.Size(120, 20);
  147. this.comboBox1.TabIndex = 5;
  148. this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
  149. // 
  150. // label2
  151. // 
  152. this.label2.Location = new System.Drawing.Point(16, 72);
  153. this.label2.Name = "label2";
  154. this.label2.TabIndex = 4;
  155. this.label2.Text = "窗体边框样式";
  156. // 
  157. // radioButton3
  158. // 
  159. this.radioButton3.Location = new System.Drawing.Point(216, 24);
  160. this.radioButton3.Name = "radioButton3";
  161. this.radioButton3.Size = new System.Drawing.Size(32, 18);
  162. this.radioButton3.TabIndex = 3;
  163. this.radioButton3.Text = "无";
  164. this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged);
  165. // 
  166. // radioButton2
  167. // 
  168. this.radioButton2.Location = new System.Drawing.Point(168, 24);
  169. this.radioButton2.Name = "radioButton2";
  170. this.radioButton2.Size = new System.Drawing.Size(32, 18);
  171. this.radioButton2.TabIndex = 2;
  172. this.radioButton2.Text = "黄";
  173. this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
  174. // 
  175. // radioButton1
  176. // 
  177. this.radioButton1.Location = new System.Drawing.Point(112, 24);
  178. this.radioButton1.Name = "radioButton1";
  179. this.radioButton1.Size = new System.Drawing.Size(48, 18);
  180. this.radioButton1.TabIndex = 1;
  181. this.radioButton1.Text = "红";
  182. this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
  183. // 
  184. // label1
  185. // 
  186. this.label1.Location = new System.Drawing.Point(16, 24);
  187. this.label1.Name = "label1";
  188. this.label1.Size = new System.Drawing.Size(80, 18);
  189. this.label1.TabIndex = 0;
  190. this.label1.Text = "窗体背景颜色";
  191. // 
  192. // button1
  193. // 
  194. this.button1.Location = new System.Drawing.Point(8, 160);
  195. this.button1.Name = "button1";
  196. this.button1.Size = new System.Drawing.Size(80, 23);
  197. this.button1.TabIndex = 1;
  198. this.button1.Text = "写入注册表";
  199. this.button1.Click += new System.EventHandler(this.button1_Click);
  200. // 
  201. // button2
  202. // 
  203. this.button2.Location = new System.Drawing.Point(104, 160);
  204. this.button2.Name = "button2";
  205. this.button2.Size = new System.Drawing.Size(80, 23);
  206. this.button2.TabIndex = 2;
  207. this.button2.Text = "恢复注册表";
  208. this.button2.Click += new System.EventHandler(this.button2_Click);
  209. // 
  210. // button3
  211. // 
  212. this.button3.Location = new System.Drawing.Point(200, 160);
  213. this.button3.Name = "button3";
  214. this.button3.Size = new System.Drawing.Size(80, 23);
  215. this.button3.TabIndex = 3;
  216. this.button3.Text = "退出";
  217. this.button3.Click += new System.EventHandler(this.button3_Click);
  218. // 
  219. // Form1
  220. // 
  221. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  222. this.ClientSize = new System.Drawing.Size(296, 199);
  223. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  224.   this.button3,
  225.   this.button2,
  226.   this.button1,
  227.   this.groupBox1});
  228. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  229. this.Name = "Form1";
  230. this.Text = "注册信息的创建和删除";
  231. this.groupBox1.ResumeLayout(false);
  232. this.ResumeLayout(false);
  233. }
  234. #endregion
  235. /// <summary>
  236. /// 应用程序的主入口点。
  237. /// </summary>
  238. [STAThread]
  239. static void Main() 
  240. {
  241. Application.Run(new Form1());
  242. }
  243. private void button1_Click(object sender, System.EventArgs e)
  244. {
  245. string  selectColor="none";
  246. if (this.radioButton1.Checked==true){selectColor="red";};
  247. if (this.radioButton2.Checked==true){selectColor="yellow";};
  248.          int fStyle=this.comboBox1.SelectedIndex;
  249.              
  250. //获得或创建代表Form1信息的节点
  251. RegistryKey  hLocalMachine=Registry.LocalMachine;
  252. RegistryKey  software=hLocalMachine.OpenSubKey("SOFTWARE",true);
  253. RegistryKey  newNode=software.CreateSubKey("Form1");
  254. //创建一个子键formColor
  255. RegistryKey  formColor=newNode.CreateSubKey("formColor");
  256. //设置一个叫color键的键值
  257. formColor.SetValue("color",selectColor);
  258.         
  259. //创建一个子键borderStyle
  260. RegistryKey  borderStyle=newNode.CreateSubKey("borderStyle");
  261. //设置一个叫borderStyle的键值
  262. borderStyle.SetValue("borderStyle",fStyle);
  263. MessageBox.Show("已成功写入注册表","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  264. }
  265. private void button2_Click(object sender, System.EventArgs e)
  266. {
  267. if (MessageBox.Show("您确定要删除写入的键值吗?","提示",
  268. MessageBoxButtons.OKCancel,MessageBoxIcon.Information)==DialogResult.OK)
  269. {
  270. RegistryKey  hLocalMachine=Registry.LocalMachine;
  271. RegistryKey  software=hLocalMachine.OpenSubKey("SOFTWARE",true);
  272. RegistryKey  newNode=software.CreateSubKey("Form1");
  273. //删除子项和任何子级子项
  274. software.DeleteSubKeyTree("Form1");
  275.                 MessageBox.Show("已经成功删除写入的键值","提示",
  276. MessageBoxButtons.OK,MessageBoxIcon.Information);
  277. }
  278. else
  279. {
  280. //不做任何处理
  281. }
  282. }
  283. private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
  284. {
  285. //保证只有一个单选钮被选定
  286. if (this.radioButton1.Checked==true)
  287. {
  288. this.BackColor=Color.Red;
  289. this.radioButton2.Checked=false;
  290. this.radioButton3.Checked=false;
  291. }
  292. }
  293. private void radioButton2_CheckedChanged(object sender, System.EventArgs e)
  294. {
  295. //保证只有一个单选钮被选定
  296. if (this.radioButton2.Checked==true)
  297. {
  298. this.BackColor=Color.Yellow;
  299. this.radioButton1.Checked=false;
  300. this.radioButton3.Checked=false;
  301. }
  302. }
  303. private void radioButton3_CheckedChanged(object sender, System.EventArgs e)
  304. {
  305. //保证只有一个单选钮被选定
  306. if (this.radioButton3.Checked==true)
  307. {
  308. this.BackColor=Color.Empty;
  309. this.radioButton1.Checked=false;
  310. this.radioButton2.Checked=false;
  311. }
  312. }
  313. private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  314. {
  315. //改变窗体边框样式
  316. int i=this.comboBox1.SelectedIndex;
  317. FormBorderStyle fStyle=(FormBorderStyle)i;
  318. this.FormBorderStyle=fStyle;
  319. }
  320. private void button3_Click(object sender, System.EventArgs e)
  321. {
  322. //退出程序
  323. this.Close();
  324. Application.Exit();
  325. }
  326. }
  327. }