Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 8k
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 ch3_8
  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.RichTextBox richTextBox1;
  16. private System.Windows.Forms.ImageList imageList1;
  17. private System.Windows.Forms.ToolBarButton toolBarButton1;
  18. private System.Windows.Forms.ToolBarButton toolBarButton2;
  19. private System.Windows.Forms.ToolBarButton toolBarButton3;
  20. private System.Windows.Forms.ContextMenu contextMenu1;
  21. private System.ComponentModel.IContainer components;
  22.         
  23. //代表最近10个在剪贴板上保存的数据
  24. private string[] clipboardText=new string[]{"","","","","","","","","",""};
  25. public Form1()
  26. {
  27. //
  28. // Windows 窗体设计器支持所必需的
  29. //
  30. InitializeComponent();
  31. //
  32. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  33. //
  34. }
  35. /// <summary>
  36. /// 清理所有正在使用的资源。
  37. /// </summary>
  38. protected override void Dispose( bool disposing )
  39. {
  40. if( disposing )
  41. {
  42. if (components != null) 
  43. {
  44. components.Dispose();
  45. }
  46. }
  47. base.Dispose( disposing );
  48. }
  49. #region Windows Form Designer generated code
  50. /// <summary>
  51. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  52. /// 此方法的内容。
  53. /// </summary>
  54. private void InitializeComponent()
  55. {
  56. this.components = new System.ComponentModel.Container();
  57. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
  58. this.toolBar1 = new System.Windows.Forms.ToolBar();
  59. this.richTextBox1 = new System.Windows.Forms.RichTextBox();
  60. this.imageList1 = new System.Windows.Forms.ImageList(this.components);
  61. this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
  62. this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
  63. this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
  64. this.contextMenu1 = new System.Windows.Forms.ContextMenu();
  65. this.SuspendLayout();
  66. // 
  67. // toolBar1
  68. // 
  69. this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
  70. this.toolBarButton1,
  71. this.toolBarButton2,
  72. this.toolBarButton3});
  73. this.toolBar1.DropDownArrows = true;
  74. this.toolBar1.ImageList = this.imageList1;
  75. this.toolBar1.Name = "toolBar1";
  76. this.toolBar1.ShowToolTips = true;
  77. this.toolBar1.Size = new System.Drawing.Size(416, 37);
  78. this.toolBar1.TabIndex = 0;
  79. this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
  80. // 
  81. // richTextBox1
  82. // 
  83. this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
  84. this.richTextBox1.Location = new System.Drawing.Point(0, 37);
  85. this.richTextBox1.Name = "richTextBox1";
  86. this.richTextBox1.Size = new System.Drawing.Size(416, 304);
  87. this.richTextBox1.TabIndex = 1;
  88. this.richTextBox1.Text = "";
  89. // 
  90. // imageList1
  91. // 
  92. this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
  93. this.imageList1.ImageSize = new System.Drawing.Size(16, 15);
  94. this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
  95. this.imageList1.TransparentColor = System.Drawing.Color.White;
  96. // 
  97. // toolBarButton1
  98. // 
  99. this.toolBarButton1.DropDownMenu = this.contextMenu1;
  100. this.toolBarButton1.ImageIndex = 0;
  101. this.toolBarButton1.Text = "剪切";
  102. // 
  103. // toolBarButton2
  104. // 
  105. this.toolBarButton2.ImageIndex = 1;
  106. this.toolBarButton2.Text = "复制";
  107. // 
  108. // toolBarButton3
  109. // 
  110. this.toolBarButton3.DropDownMenu = this.contextMenu1;
  111. this.toolBarButton3.ImageIndex = 2;
  112. this.toolBarButton3.Style = System.Windows.Forms.ToolBarButtonStyle.DropDownButton;
  113. this.toolBarButton3.Text = "粘贴";
  114. // 
  115. // Form1
  116. // 
  117. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  118. this.ClientSize = new System.Drawing.Size(416, 341);
  119. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  120.   this.richTextBox1,
  121.   this.toolBar1});
  122. this.Name = "Form1";
  123. this.Text = "剪贴板循环";
  124. this.ResumeLayout(false);
  125. }
  126. #endregion
  127. /// <summary>
  128. /// 应用程序的主入口点。
  129. /// </summary>
  130. [STAThread]
  131. static void Main() 
  132. {
  133. Application.Run(new Form1());
  134. }
  135. private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
  136. {
  137. //如果是剪切或者复制按钮则进入
  138. if (e.Button==this.toolBarButton1 | e.Button==this.toolBarButton2)
  139. {   
  140. //获得要加入剪贴板的字符串
  141.     string s=this.richTextBox1.SelectedText;
  142. //实现剪切按钮的功能
  143. if (e.Button==this.toolBarButton1)
  144. {
  145. //剪切需要把字符串消除
  146. this.richTextBox1.SelectedText="";
  147. }
  148.     //实现拷贝和剪切共同的功能
  149. //将字符串放到剪贴板上
  150. Clipboard.SetDataObject(s);
  151. //下面为粘贴按钮动态添加菜单
  152. if (this.contextMenu1!=null )
  153. {
  154. //看看菜单的数目,如果少于10则添加
  155. int i=this.contextMenu1.MenuItems.Count;
  156. if (i<10)
  157. {
  158. //设置菜单项的Text属性,多出15个字符的部分不显示
  159. MenuItem newItem=new MenuItem("文本:"+s);
  160. if (s.Length>15)
  161. {
  162. newItem.Text="文本:"+s.Substring(0,15)+"...";
  163. //为菜单项的click按钮添加事件处理代理
  164. newItem.Click+=new EventHandler(menuItemClick);
  165.                         //动态添加菜单项
  166. this.contextMenu1.MenuItems.Add(newItem);
  167.         //剪贴板内容字符串数组设置
  168. this.clipboardText[i]=s;
  169. }
  170. //如果大于10,则清空第一项菜单,其余的向后递推
  171. else
  172. {
  173. string temp=this.clipboardText[0];
  174. //设置菜单项的Text属性,多出15个字符的部分不显示
  175.                         MenuItem newItem=new MenuItem("文本:"+s);
  176. if (s.Length>15)
  177. {
  178.  newItem.Text="文本:"+s.Substring(0,15)+"...";
  179.                         newItem.Click+=new EventHandler(menuItemClick);
  180.                         //递推原来菜单项0-9项成为新菜单项的2-10项
  181. this.clipboardText[0]=s;
  182. for (int j=0;j<9;j++)
  183. {
  184. string temp1=this.clipboardText[j+1];
  185. this.clipboardText[j+1]=temp;
  186. this.contextMenu1.MenuItems[j+1].Text="文本:"+temp;
  187. temp=temp1;
  188. }
  189. }
  190. }
  191. else
  192. {
  193. //初始化第一个菜单项
  194. MenuItem newItem=new MenuItem("文本:"+s);
  195. if (s.Length>15)
  196. {
  197. newItem.Text="文本:"+s.Substring(0,15)+"...";
  198. newItem.Click+=new EventHandler(menuItemClick);
  199. this.contextMenu1.MenuItems.Add(newItem);
  200. this.contextMenu1.MenuItems[0].Text="文本:"+s;
  201. this.clipboardText[0]=s;
  202. }
  203. }
  204. //实现粘贴按钮的功能
  205. if (e.Button==this.toolBarButton3)
  206. {
  207. try
  208. {
  209. IDataObject d = Clipboard.GetDataObject ( ) ;
  210. //判断剪切板中数据是不是文本
  211. if ( d.GetDataPresent ( DataFormats.UnicodeText ) ) 
  212. {
  213. //将文本对象粘贴到文本框里面
  214. this.richTextBox1.Paste();
  215. }
  216. else
  217. {
  218. //如果剪贴板上没有文本,则发出提醒
  219. MessageBox.Show("没有可粘贴的文本","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
  220. }
  221. }
  222. catch(Exception error)
  223. {
  224. //读取剪贴板出错处理
  225. MessageBox.Show("错误信息是: "+error.Message,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
  226. }
  227. }
  228. }
  229. //菜单项事件处理
  230. private void menuItemClick (object sender, System.EventArgs e)
  231. {
  232.     //获取发出信息的菜单项
  233. MenuItem mItem=(MenuItem)sender;
  234. //获得菜单项索引
  235. int i =mItem.Index;
  236. //放到剪贴板
  237. Clipboard.SetDataObject(this.clipboardText[i]);
  238. //拷贝它到richTextBox上
  239. this.richTextBox1.Paste();
  240. }
  241. }
  242. }