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. namespace ch1_9
  8. {
  9. /// <summary>
  10. /// Form1 的摘要说明。
  11. /// </summary>
  12. public class Form1 : System.Windows.Forms.Form
  13. {
  14. private System.Windows.Forms.ComboBox comboBox1;
  15. private System.Windows.Forms.Panel panel1;
  16. private System.Windows.Forms.GroupBox groupBox1;
  17. private System.Windows.Forms.CheckBox checkBox1;
  18. private System.Windows.Forms.CheckBox checkBox2;
  19. private System.Windows.Forms.Label label1;
  20. private System.Windows.Forms.Label label2;
  21. private System.Windows.Forms.ComboBox comboBoxStyle;
  22. private System.Windows.Forms.ComboBox comboBoxDraw;
  23. private System.Windows.Forms.Label label3;
  24. private System.Windows.Forms.Label label4;
  25. /// <summary>
  26. /// 必需的设计器变量。
  27. /// </summary>
  28. private System.ComponentModel.Container components = null;
  29. public Form1()
  30. {
  31. //
  32. // Windows 窗体设计器支持所必需的
  33. //
  34. InitializeComponent();
  35. //初始化3个comboBox
  36. InitControl();
  37. //
  38. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  39. //
  40. }
  41. /// <summary>
  42. /// 清理所有正在使用的资源。
  43. /// </summary>
  44. protected override void Dispose( bool disposing )
  45. {
  46. if( disposing )
  47. {
  48. if (components != null) 
  49. {
  50. components.Dispose();
  51. }
  52. }
  53. base.Dispose( disposing );
  54. }
  55. #region Windows Form Designer generated code
  56. /// <summary>
  57. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  58. /// 此方法的内容。
  59. /// </summary>
  60. private void InitializeComponent()
  61. {
  62. this.comboBox1 = new System.Windows.Forms.ComboBox();
  63. this.panel1 = new System.Windows.Forms.Panel();
  64. this.groupBox1 = new System.Windows.Forms.GroupBox();
  65. this.comboBoxDraw = new System.Windows.Forms.ComboBox();
  66. this.comboBoxStyle = new System.Windows.Forms.ComboBox();
  67. this.label2 = new System.Windows.Forms.Label();
  68. this.label1 = new System.Windows.Forms.Label();
  69. this.checkBox2 = new System.Windows.Forms.CheckBox();
  70. this.checkBox1 = new System.Windows.Forms.CheckBox();
  71. this.label3 = new System.Windows.Forms.Label();
  72. this.label4 = new System.Windows.Forms.Label();
  73. this.groupBox1.SuspendLayout();
  74. this.SuspendLayout();
  75. // 
  76. // comboBox1
  77. // 
  78. this.comboBox1.AllowDrop = true;
  79. this.comboBox1.Enabled = false;
  80. this.comboBox1.Location = new System.Drawing.Point(16, 72);
  81. this.comboBox1.Name = "comboBox1";
  82. this.comboBox1.Size = new System.Drawing.Size(121, 20);
  83. this.comboBox1.TabIndex = 0;
  84. this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
  85. this.comboBox1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.comboBox1_MeasureItem);
  86. this.comboBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem);
  87. // 
  88. // panel1
  89. // 
  90. this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  91. this.panel1.Location = new System.Drawing.Point(16, 176);
  92. this.panel1.Name = "panel1";
  93. this.panel1.Size = new System.Drawing.Size(120, 24);
  94. this.panel1.TabIndex = 1;
  95. // 
  96. // groupBox1
  97. // 
  98. this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
  99. this.comboBoxDraw,
  100. this.comboBoxStyle,
  101. this.label2,
  102. this.label1,
  103. this.checkBox2,
  104. this.checkBox1});
  105. this.groupBox1.Location = new System.Drawing.Point(160, 24);
  106. this.groupBox1.Name = "groupBox1";
  107. this.groupBox1.Size = new System.Drawing.Size(256, 200);
  108. this.groupBox1.TabIndex = 2;
  109. this.groupBox1.TabStop = false;
  110. this.groupBox1.Text = "组合框设置";
  111. // 
  112. // comboBoxDraw
  113. // 
  114. this.comboBoxDraw.Location = new System.Drawing.Point(120, 128);
  115. this.comboBoxDraw.Name = "comboBoxDraw";
  116. this.comboBoxDraw.Size = new System.Drawing.Size(121, 20);
  117. this.comboBoxDraw.TabIndex = 5;
  118. this.comboBoxDraw.SelectedIndexChanged += new System.EventHandler(this.comboBoxDraw_SelectedIndexChanged);
  119. // 
  120. // comboBoxStyle
  121. // 
  122. this.comboBoxStyle.Location = new System.Drawing.Point(120, 96);
  123. this.comboBoxStyle.Name = "comboBoxStyle";
  124. this.comboBoxStyle.Size = new System.Drawing.Size(121, 20);
  125. this.comboBoxStyle.TabIndex = 4;
  126. this.comboBoxStyle.SelectedIndexChanged += new System.EventHandler(this.comboBoxStyle_SelectedIndexChanged);
  127. // 
  128. // label2
  129. // 
  130. this.label2.Location = new System.Drawing.Point(32, 128);
  131. this.label2.Name = "label2";
  132. this.label2.Size = new System.Drawing.Size(64, 23);
  133. this.label2.TabIndex = 3;
  134. this.label2.Text = "绘制模式";
  135. // 
  136. // label1
  137. // 
  138. this.label1.Location = new System.Drawing.Point(32, 96);
  139. this.label1.Name = "label1";
  140. this.label1.Size = new System.Drawing.Size(48, 24);
  141. this.label1.TabIndex = 2;
  142. this.label1.Text = "样式";
  143. // 
  144. // checkBox2
  145. // 
  146. this.checkBox2.Location = new System.Drawing.Point(32, 56);
  147. this.checkBox2.Name = "checkBox2";
  148. this.checkBox2.TabIndex = 1;
  149. this.checkBox2.Text = "排序";
  150. this.checkBox2.CheckStateChanged += new System.EventHandler(this.checkBox2_CheckStateChanged);
  151. // 
  152. // checkBox1
  153. // 
  154. this.checkBox1.Location = new System.Drawing.Point(32, 24);
  155. this.checkBox1.Name = "checkBox1";
  156. this.checkBox1.TabIndex = 0;
  157. this.checkBox1.Text = "启用";
  158. this.checkBox1.CheckStateChanged += new System.EventHandler(this.checkBox1_CheckStateChanged);
  159. // 
  160. // label3
  161. // 
  162. this.label3.Location = new System.Drawing.Point(16, 40);
  163. this.label3.Name = "label3";
  164. this.label3.Size = new System.Drawing.Size(120, 23);
  165. this.label3.TabIndex = 3;
  166. this.label3.Text = "从组合框中选取颜色";
  167. // 
  168. // label4
  169. // 
  170. this.label4.Location = new System.Drawing.Point(16, 144);
  171. this.label4.Name = "label4";
  172. this.label4.TabIndex = 4;
  173. this.label4.Text = "显示颜色";
  174. // 
  175. // Form1
  176. // 
  177. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  178. this.ClientSize = new System.Drawing.Size(440, 261);
  179. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  180.   this.label4,
  181.   this.label3,
  182.   this.groupBox1,
  183.   this.panel1,
  184.   this.comboBox1});
  185. this.Name = "Form1";
  186. this.Text = "组合框";
  187. this.groupBox1.ResumeLayout(false);
  188. this.ResumeLayout(false);
  189. }
  190. #endregion
  191. /// <summary>
  192. ///    为程序设置控制两个组合框的属性的默认状态。
  193. /// </summary>
  194. private void InitControl() 
  195. {
  196. //初始化样式组合框
  197. comboBoxStyle.Items.Add(ComboBoxStyle.Simple.ToString());
  198.         comboBoxStyle.Items.Add(ComboBoxStyle.DropDown.ToString());
  199.         comboBoxStyle.Items.Add(ComboBoxStyle.DropDownList.ToString());
  200. //初始化绘图模式组合框
  201. comboBoxDraw.Items.Add(DrawMode.Normal.ToString());
  202.     comboBoxDraw.Items.Add(DrawMode.OwnerDrawFixed.ToString());
  203. comboBoxDraw.Items.Add(DrawMode.OwnerDrawVariable.ToString());
  204. //初始化ComboBox1
  205. //获取或设置一个字符串,该字符串指定要显示其内容的数据源的属性
  206.         comboBox1.DisplayMember="Color";
  207. comboBox1.Items.Add(Brushes.Cyan);
  208. comboBox1.Items.Add(Brushes.DarkSalmon);
  209. comboBox1.Items.Add(Brushes.Gray);
  210. comboBox1.Items.Add(Brushes.Green);
  211. comboBox1.Items.Add(Brushes.AliceBlue);
  212. comboBox1.Items.Add(Brushes.Black);
  213. comboBox1.Items.Add(Brushes.Blue);
  214.     comboBox1.Items.Add(Brushes.Chocolate);
  215. comboBox1.Items.Add(Brushes.Pink);
  216. comboBox1.Items.Add(Brushes.Red);
  217. comboBox1.Items.Add(Brushes.LightBlue);
  218. comboBox1.Items.Add(Brushes.Brown);
  219. comboBox1.Items.Add(Brushes.DodgerBlue);
  220. comboBox1.Items.Add(Brushes.MediumPurple);
  221. comboBox1.Items.Add(Brushes.White);
  222. comboBox1.Items.Add(Brushes.Yellow);
  223.  
  224. }
  225. /// <summary>
  226. /// 应用程序的主入口点。
  227. /// </summary>
  228. [STAThread]
  229. static void Main() 
  230. {
  231. Application.Run(new Form1());
  232. }
  233. private void comboBox1_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
  234. {
  235. //设置绘制图形的高度比comboBox1项目的高度略小
  236. e.ItemHeight = comboBox1.ItemHeight - 2;
  237. }
  238. private void comboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
  239. {
  240.     ComboBox cmb = (ComboBox) sender;
  241. if (e.Index == -1)
  242. return;
  243. if (sender == null)
  244. return;
  245. SolidBrush selectedBrush = (SolidBrush)cmb.Items[e.Index];
  246. Graphics g = e.Graphics;
  247. // 如果选择了项,则绘制正确的背景颜色和聚焦框
  248. e.DrawBackground();
  249. e.DrawFocusRectangle();
  250. // 绘制颜色的预览框
  251. Rectangle rectPreview = e.Bounds;
  252. rectPreview.Offset(2,2);
  253. rectPreview.Width = 20;
  254. rectPreview.Height -= 4;
  255. g.DrawRectangle(new Pen(e.ForeColor), rectPreview);
  256. // 获取选定颜色的相应 Brush 对象,并填充预览框
  257. rectPreview.Offset(1,1);
  258. rectPreview.Width -= 2;
  259. rectPreview.Height -= 2;
  260. g.FillRectangle(selectedBrush, rectPreview);
  261. // 绘制选定颜色的名称
  262. g.DrawString(selectedBrush.Color.ToString(), Font, new SolidBrush(e.ForeColor), e.Bounds.X+30,e.Bounds.Y+1);
  263.     
  264. }
  265. private void comboBoxDraw_SelectedIndexChanged(object sender, System.EventArgs e)
  266. {  
  267.     //设定comboBox1的DrawMode属性
  268. String i=comboBoxDraw.SelectedItem.ToString();
  269. switch (i)
  270. {   
  271. case "Normal":
  272. comboBox1.DrawMode=DrawMode.Normal;
  273. break;
  274. case "OwnerDrawFixed":
  275. comboBox1.DrawMode=DrawMode.OwnerDrawFixed;
  276. break;
  277. case "OwnerDrawVariable":
  278. comboBox1.DrawMode=DrawMode.OwnerDrawVariable;
  279. break;
  280. }
  281. }
  282. private void comboBoxStyle_SelectedIndexChanged(object sender, System.EventArgs e)
  283. {
  284.    //设定comboBox1的DropDownStyle属性
  285. String  i=comboBoxStyle.SelectedItem.ToString();
  286. switch (i)
  287. {   
  288. case "Simple":
  289. comboBox1.DropDownStyle=ComboBoxStyle.Simple;
  290. Size size=new Size(comboBox1.Size.Width,66);
  291. comboBox1.Size=size;
  292. break;
  293. case "DropDown":
  294. comboBox1.DropDownStyle=ComboBoxStyle.DropDown;
  295. break;
  296. case "DropDownList":
  297. comboBox1.DropDownStyle=ComboBoxStyle.DropDownList;
  298. break;
  299. }
  300. }
  301. private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  302. {
  303. //绘制panel1的背景颜色
  304. if (comboBox1.SelectedIndex >= 0)
  305. {
  306. SolidBrush brush = (SolidBrush)(comboBox1.SelectedItem);
  307.         panel1.BackColor=brush.Color;
  308. }
  309. }
  310. private void checkBox1_CheckStateChanged(object sender, System.EventArgs e)
  311. {
  312. //根据checkBox的Checked属性确定是否启用comboBox1
  313. comboBox1.Enabled=checkBox1.Checked;
  314. }
  315. private void checkBox2_CheckStateChanged(object sender, System.EventArgs e)
  316. {
  317.     //根据checkBox的Checked属性确定comboBox1中的选项是否排序
  318. comboBox1.Sorted=checkBox2.Checked;
  319. }
  320. }
  321. }