Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 16k
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_18
  8. {
  9. /// <summary>
  10. /// Form1 的摘要说明。
  11. /// </summary>
  12. public class Form1 : System.Windows.Forms.Form
  13. {
  14. private System.Windows.Forms.ToolBar toolBar1;
  15. private System.Windows.Forms.ImageList imageList1;
  16. private System.Windows.Forms.ToolBarButton toolBarButton1;
  17. private System.Windows.Forms.ToolBarButton toolBarButton2;
  18. private System.Windows.Forms.ToolBarButton toolBarButton3;
  19. private System.Windows.Forms.GroupBox groupBox1;
  20. private System.Windows.Forms.Label label1;
  21. private System.Windows.Forms.ComboBox comboBox1;
  22. private System.Windows.Forms.GroupBox groupBox2;
  23. private System.Windows.Forms.RadioButton radioButton1;
  24. private System.Windows.Forms.RadioButton radioButton2;
  25. private System.Windows.Forms.ToolBarButton toolBarButton4;
  26. private System.Windows.Forms.ToolBarButton toolBarButton5;
  27. private System.Windows.Forms.ToolBarButton toolBarButton6;
  28. private System.Windows.Forms.ContextMenu contextMenu1;
  29. private System.Windows.Forms.MenuItem menuItem1;
  30. private System.Windows.Forms.MenuItem menuItem2;
  31. private System.Windows.Forms.CheckBox checkBox1;
  32. private System.Windows.Forms.GroupBox groupBox3;
  33. private System.Windows.Forms.RadioButton radioButton3;
  34. private System.Windows.Forms.RadioButton radioButton4;
  35. private System.Windows.Forms.OpenFileDialog openFileDialog1;
  36. private System.Windows.Forms.SaveFileDialog saveFileDialog1;
  37. private System.Windows.Forms.GroupBox groupBox4;
  38. private System.Windows.Forms.ToolTip toolTip1;
  39. private System.Windows.Forms.ToolBarButton toolBarButton7;
  40. private System.Windows.Forms.MenuItem menuItem3;
  41. private System.ComponentModel.IContainer components;
  42. public Form1()
  43. {
  44. //
  45. // Windows 窗体设计器支持所必需的
  46. //
  47. InitializeComponent();
  48. //
  49. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  50. //
  51. }
  52. /// <summary>
  53. /// 清理所有正在使用的资源。
  54. /// </summary>
  55. protected override void Dispose( bool disposing )
  56. {
  57. if( disposing )
  58. {
  59. if (components != null) 
  60. {
  61. components.Dispose();
  62. }
  63. }
  64. base.Dispose( disposing );
  65. }
  66. #region Windows Form Designer generated code
  67. /// <summary>
  68. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  69. /// 此方法的内容。
  70. /// </summary>
  71. private void InitializeComponent()
  72. {
  73. this.components = new System.ComponentModel.Container();
  74. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
  75. this.toolBar1 = new System.Windows.Forms.ToolBar();
  76. this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
  77. this.contextMenu1 = new System.Windows.Forms.ContextMenu();
  78. this.menuItem1 = new System.Windows.Forms.MenuItem();
  79. this.menuItem2 = new System.Windows.Forms.MenuItem();
  80. this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
  81. this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
  82. this.toolBarButton4 = new System.Windows.Forms.ToolBarButton();
  83. this.toolBarButton5 = new System.Windows.Forms.ToolBarButton();
  84. this.toolBarButton6 = new System.Windows.Forms.ToolBarButton();
  85. this.toolBarButton7 = new System.Windows.Forms.ToolBarButton();
  86. this.imageList1 = new System.Windows.Forms.ImageList(this.components);
  87. this.groupBox1 = new System.Windows.Forms.GroupBox();
  88. this.groupBox3 = new System.Windows.Forms.GroupBox();
  89. this.radioButton4 = new System.Windows.Forms.RadioButton();
  90. this.radioButton3 = new System.Windows.Forms.RadioButton();
  91. this.checkBox1 = new System.Windows.Forms.CheckBox();
  92. this.groupBox2 = new System.Windows.Forms.GroupBox();
  93. this.radioButton2 = new System.Windows.Forms.RadioButton();
  94. this.radioButton1 = new System.Windows.Forms.RadioButton();
  95. this.comboBox1 = new System.Windows.Forms.ComboBox();
  96. this.label1 = new System.Windows.Forms.Label();
  97. this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
  98. this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
  99. this.groupBox4 = new System.Windows.Forms.GroupBox();
  100. this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
  101. this.menuItem3 = new System.Windows.Forms.MenuItem();
  102. this.groupBox1.SuspendLayout();
  103. this.groupBox3.SuspendLayout();
  104. this.groupBox2.SuspendLayout();
  105. this.SuspendLayout();
  106. // 
  107. // toolBar1
  108. // 
  109. this.toolBar1.AllowDrop = true;
  110. this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
  111. this.toolBarButton1,
  112. this.toolBarButton2,
  113. this.toolBarButton3,
  114. this.toolBarButton4,
  115. this.toolBarButton5,
  116. this.toolBarButton6,
  117. this.toolBarButton7});
  118. this.toolBar1.DropDownArrows = true;
  119. this.toolBar1.Location = new System.Drawing.Point(0, 0);
  120. this.toolBar1.Name = "toolBar1";
  121. this.toolBar1.ShowToolTips = true;
  122. this.toolBar1.Size = new System.Drawing.Size(368, 41);
  123. this.toolBar1.TabIndex = 0;
  124. this.toolTip1.SetToolTip(this.toolBar1, "工具栏示例");
  125. this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
  126. // 
  127. // toolBarButton1
  128. // 
  129. this.toolBarButton1.DropDownMenu = this.contextMenu1;
  130. this.toolBarButton1.Style = System.Windows.Forms.ToolBarButtonStyle.DropDownButton;
  131. this.toolBarButton1.Text = "新建";
  132. this.toolBarButton1.ToolTipText = "新建一个文档";
  133. // 
  134. // contextMenu1
  135. // 
  136. this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  137.  this.menuItem1,
  138.  this.menuItem2,
  139.  this.menuItem3});
  140. this.contextMenu1.Popup += new System.EventHandler(this.contextMenu1_Popup);
  141. // 
  142. // menuItem1
  143. // 
  144. this.menuItem1.Index = 0;
  145. this.menuItem1.Text = "文本文档";
  146. this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
  147. // 
  148. // menuItem2
  149. // 
  150. this.menuItem2.Index = 1;
  151. this.menuItem2.Text = "Word文档";
  152. this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
  153. // 
  154. // toolBarButton2
  155. // 
  156. this.toolBarButton2.Text = "打开";
  157. this.toolBarButton2.ToolTipText = "打开文件";
  158. // 
  159. // toolBarButton3
  160. // 
  161. this.toolBarButton3.Text = "保存";
  162. this.toolBarButton3.ToolTipText = "保存文件";
  163. // 
  164. // toolBarButton4
  165. // 
  166. this.toolBarButton4.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
  167. // 
  168. // toolBarButton5
  169. // 
  170. this.toolBarButton5.Text = "剪切";
  171. this.toolBarButton5.ToolTipText = "剪切文本";
  172. // 
  173. // toolBarButton6
  174. // 
  175. this.toolBarButton6.Text = "拷贝";
  176. this.toolBarButton6.ToolTipText = "拷贝文本";
  177. // 
  178. // toolBarButton7
  179. // 
  180. this.toolBarButton7.Text = "粘贴";
  181. this.toolBarButton7.ToolTipText = "粘贴文本";
  182. // 
  183. // imageList1
  184. // 
  185. this.imageList1.ImageSize = new System.Drawing.Size(16, 15);
  186. this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
  187. this.imageList1.TransparentColor = System.Drawing.Color.White;
  188. // 
  189. // groupBox1
  190. // 
  191. this.groupBox1.Controls.Add(this.groupBox3);
  192. this.groupBox1.Controls.Add(this.groupBox2);
  193. this.groupBox1.Controls.Add(this.comboBox1);
  194. this.groupBox1.Controls.Add(this.label1);
  195. this.groupBox1.Location = new System.Drawing.Point(24, 64);
  196. this.groupBox1.Name = "groupBox1";
  197. this.groupBox1.Size = new System.Drawing.Size(312, 264);
  198. this.groupBox1.TabIndex = 1;
  199. this.groupBox1.TabStop = false;
  200. this.groupBox1.Text = "工具栏风格";
  201. // 
  202. // groupBox3
  203. // 
  204. this.groupBox3.Controls.Add(this.radioButton4);
  205. this.groupBox3.Controls.Add(this.radioButton3);
  206. this.groupBox3.Controls.Add(this.checkBox1);
  207. this.groupBox3.Location = new System.Drawing.Point(24, 168);
  208. this.groupBox3.Name = "groupBox3";
  209. this.groupBox3.Size = new System.Drawing.Size(248, 64);
  210. this.groupBox3.TabIndex = 5;
  211. this.groupBox3.TabStop = false;
  212. this.groupBox3.Text = "工具栏外观";
  213. // 
  214. // radioButton4
  215. // 
  216. this.radioButton4.Location = new System.Drawing.Point(88, 24);
  217. this.radioButton4.Name = "radioButton4";
  218. this.radioButton4.Size = new System.Drawing.Size(48, 24);
  219. this.radioButton4.TabIndex = 1;
  220. this.radioButton4.Text = "水平";
  221. this.radioButton4.CheckedChanged += new System.EventHandler(this.radioButton4_CheckedChanged);
  222. // 
  223. // radioButton3
  224. // 
  225. this.radioButton3.Location = new System.Drawing.Point(16, 24);
  226. this.radioButton3.Name = "radioButton3";
  227. this.radioButton3.Size = new System.Drawing.Size(48, 24);
  228. this.radioButton3.TabIndex = 0;
  229. this.radioButton3.Text = "正常";
  230. this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged);
  231. // 
  232. // checkBox1
  233. // 
  234. this.checkBox1.Location = new System.Drawing.Point(152, 24);
  235. this.checkBox1.Name = "checkBox1";
  236. this.checkBox1.Size = new System.Drawing.Size(80, 24);
  237. this.checkBox1.TabIndex = 4;
  238. this.checkBox1.Text = "按钮图片";
  239. this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
  240. // 
  241. // groupBox2
  242. // 
  243. this.groupBox2.Controls.Add(this.radioButton2);
  244. this.groupBox2.Controls.Add(this.radioButton1);
  245. this.groupBox2.Location = new System.Drawing.Point(24, 72);
  246. this.groupBox2.Name = "groupBox2";
  247. this.groupBox2.Size = new System.Drawing.Size(248, 72);
  248. this.groupBox2.TabIndex = 3;
  249. this.groupBox2.TabStop = false;
  250. this.groupBox2.Text = "按钮风格";
  251. // 
  252. // radioButton2
  253. // 
  254. this.radioButton2.Location = new System.Drawing.Point(128, 32);
  255. this.radioButton2.Name = "radioButton2";
  256. this.radioButton2.TabIndex = 1;
  257. this.radioButton2.Text = "切换按钮";
  258. this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
  259. // 
  260. // radioButton1
  261. // 
  262. this.radioButton1.Location = new System.Drawing.Point(16, 32);
  263. this.radioButton1.Name = "radioButton1";
  264. this.radioButton1.TabIndex = 0;
  265. this.radioButton1.Text = "三维按钮";
  266. this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
  267. // 
  268. // comboBox1
  269. // 
  270. this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  271. this.comboBox1.Items.AddRange(new object[] {
  272.    "无边框",
  273.    "单行边框",
  274.    "三维边框"});
  275. this.comboBox1.Location = new System.Drawing.Point(128, 40);
  276. this.comboBox1.Name = "comboBox1";
  277. this.comboBox1.Size = new System.Drawing.Size(144, 20);
  278. this.comboBox1.TabIndex = 2;
  279. this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
  280. // 
  281. // label1
  282. // 
  283. this.label1.Location = new System.Drawing.Point(24, 40);
  284. this.label1.Name = "label1";
  285. this.label1.Size = new System.Drawing.Size(80, 23);
  286. this.label1.TabIndex = 0;
  287. this.label1.Text = "工具栏边框";
  288. // 
  289. // saveFileDialog1
  290. // 
  291. this.saveFileDialog1.FileName = "doc1";
  292. // 
  293. // groupBox4
  294. // 
  295. this.groupBox4.Dock = System.Windows.Forms.DockStyle.Top;
  296. this.groupBox4.Location = new System.Drawing.Point(0, 41);
  297. this.groupBox4.Name = "groupBox4";
  298. this.groupBox4.Size = new System.Drawing.Size(368, 8);
  299. this.groupBox4.TabIndex = 2;
  300. this.groupBox4.TabStop = false;
  301. // 
  302. // menuItem3
  303. // 
  304. this.menuItem3.Index = 2;
  305. this.menuItem3.Shortcut = System.Windows.Forms.Shortcut.F2;
  306. this.menuItem3.Text = "luna";
  307. this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
  308. // 
  309. // Form1
  310. // 
  311. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  312. this.ClientSize = new System.Drawing.Size(368, 357);
  313. this.Controls.Add(this.groupBox4);
  314. this.Controls.Add(this.groupBox1);
  315. this.Controls.Add(this.toolBar1);
  316. this.Name = "Form1";
  317. this.Text = "工具栏";
  318. this.groupBox1.ResumeLayout(false);
  319. this.groupBox3.ResumeLayout(false);
  320. this.groupBox2.ResumeLayout(false);
  321. this.ResumeLayout(false);
  322. }
  323. #endregion
  324. /// <summary>
  325. /// 应用程序的主入口点。
  326. /// </summary>
  327. [STAThread]
  328. static void Main() 
  329. {
  330. Application.Run(new Form1());
  331. }
  332. private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  333. {
  334. string s=this.comboBox1.SelectedItem.ToString();
  335.     //设置ToolBar的边框风格
  336. switch (s)
  337. {
  338. case "无边框":
  339. this.toolBar1.BorderStyle=BorderStyle.None;
  340. break;
  341. case "单行边框":
  342. this.toolBar1.BorderStyle=BorderStyle.FixedSingle;
  343. break;
  344. case "三维边框":
  345. this.toolBar1.BorderStyle=BorderStyle.Fixed3D;
  346. break;
  347. }
  348. }
  349. private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
  350. {
  351. if (this.radioButton1.Checked==true)
  352. {
  353. this.toolBarButton2.Style=ToolBarButtonStyle.PushButton;
  354. this.toolBarButton3.Style=ToolBarButtonStyle.PushButton;
  355. this.toolBarButton5.Style=ToolBarButtonStyle.PushButton;
  356. this.toolBarButton6.Style=ToolBarButtonStyle.PushButton;
  357. this.toolBarButton7.Style=ToolBarButtonStyle.PushButton;
  358.             this.radioButton2.Checked=false;
  359. }
  360. }
  361. private void radioButton2_CheckedChanged(object sender, System.EventArgs e)
  362. {
  363. if (this.radioButton2.Checked==true)
  364. {
  365. this.toolBarButton2.Style=ToolBarButtonStyle.ToggleButton;
  366. this.toolBarButton3.Style=ToolBarButtonStyle.ToggleButton;
  367. this.toolBarButton5.Style=ToolBarButtonStyle.ToggleButton;
  368. this.toolBarButton6.Style=ToolBarButtonStyle.ToggleButton;
  369. this.toolBarButton7.Style=ToolBarButtonStyle.ToggleButton;
  370. this.radioButton1.Checked=false;
  371. }
  372. }
  373. private void radioButton3_CheckedChanged(object sender, System.EventArgs e)
  374. {
  375. if (this.radioButton3.Checked==true)
  376. {
  377. this.toolBar1.Appearance=ToolBarAppearance.Normal;
  378. this.radioButton4.Checked=false;
  379. }
  380. }
  381. private void radioButton4_CheckedChanged(object sender, System.EventArgs e)
  382. {
  383. if (this.radioButton4.Checked==true)
  384. {
  385. this.toolBar1.Appearance=ToolBarAppearance.Flat;
  386. this.radioButton3.Checked=false;
  387. }
  388. }
  389. private void menuItem1_Click(object sender, System.EventArgs e)
  390. {
  391. MessageBox.Show("现在创建一个文本文件","提示",MessageBoxButtons.OKCancel);
  392. }
  393. private void menuItem2_Click(object sender, System.EventArgs e)
  394. {
  395. MessageBox.Show("现在创建一个Word文档","提示",MessageBoxButtons.OKCancel);
  396. }
  397. private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
  398. {
  399. if (this.checkBox1.Checked==true)
  400. {
  401. this.toolBar1.ImageList=this.imageList1;
  402. //为每个按钮设置图片
  403. this.toolBarButton1.ImageIndex=0;
  404. this.toolBarButton2.ImageIndex=1;
  405. this.toolBarButton3.ImageIndex=2;
  406. this.toolBarButton5.ImageIndex=3;
  407. this.toolBarButton6.ImageIndex=4;
  408. this.toolBarButton7.ImageIndex=5;
  409. }
  410. else
  411. {
  412. this.toolBar1.ImageList=null;
  413. }
  414. }
  415. private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
  416. {
  417.     //判断点击的是哪个按钮,然后做出相应的动作
  418. if (e.Button==this.toolBarButton2)
  419. {
  420. this.openFileDialog1.ShowDialog();
  421. }
  422. if (e.Button==this.toolBarButton3)
  423. {
  424. this.saveFileDialog1.ShowDialog();
  425. }
  426. if (e.Button==this.toolBarButton5)
  427. {
  428. MessageBox.Show("已剪切了指定文本");
  429. }
  430. if (e.Button==this.toolBarButton6)
  431. {
  432. MessageBox.Show("已拷贝了指定文本");
  433. }
  434. if (e.Button==this.toolBarButton7)
  435. {
  436. MessageBox.Show("已粘贴了指定文本");
  437. }
  438. }
  439. private void contextMenu1_Popup(object sender, System.EventArgs e)
  440. {
  441. }
  442. private void menuItem3_Click(object sender, System.EventArgs e)
  443. {
  444. MessageBox.Show("Luna","提示",MessageBoxButtons.OKCancel);
  445. }
  446. }
  447. }