章鱼哥—VB.NET RichTextBox.AllowDrop属性的实例

这篇文章讲述allowDrop属性的另一种用法,即接受文字,读者可以借鉴下,该文为转载文章地址:http://www.lob.cn/sl/control/431.shtml


RichTextBox.AllowDrop属性的实例

下面的代码示例演示如何使用 ListBox 控件(包含要放入 RichTextBox 控件的项)来执行拖放操作。窗体的构造函数AllowDrop 属性设置为 true 以使拖放操作能够在 RichTextBox 中进行。该示例使用 ListBox 的 MouseDown 事件通过调用 DoDragDrop 方法来启动拖动操作。该示例使用 dragenter 事件来确定拖放到 RichTextBox 中的项是否为有效的数据类型。DragDrop 事件将被拖动的项实际放在 RichTextBox 控件中 RichTextBox 内的当前光标位置。该示例要求 DragDropdragenter 事件已连接到此示例中定义的事件处理程序。

  1. PublicSubNew()
  2. MyBase.New()
  3. 'ThiscallisrequiredbytheWindowsFormDesigner.
  4. InitializeComponent()
  5. richTextBox1.AllowDrop=True
  6. EndSub
  7. PrivateSublistBox1_MouseDown(ByValsenderAsObject,ByValeAsSystem.Windows.Forms.MouseEventArgs)HandleslistBox1.MouseDown
  8. 'Determineswhichitemwasselected.
  9. DimlbAsListBox=CType(sender,ListBox)
  10. DimptAsNewPoint(e.X,e.Y)
  11. 'RetrievetheitematthespecifiedlocationwithintheListBox.
  12. DimindexAsInteger=lb.IndexFromPoint(pt)
  13. 'Startsadrag-and-dropoperation.
  14. Ifindex>=0Then
  15. 'RetrievetheselecteditemtexttodragintotheRichTextBox.
  16. lb.DoDragDrop(lb.Items(index).ToString(),DragDropEffects.copy)
  17. EndIf
  18. EndSub'listBox1_MouseDown
  19. PrivateSubrichTextBox1_dragenter(ByValsenderAsObject,ByValeAsDragEventArgs)HandlesrichTextBox1.dragenter
  20. 'Ifthedataistext,copythedatatotheRichTextBoxcontrol.
  21. Ife.Data.GetDataPresent("Text")Then
  22. e.Effect=DragDropEffects.copy
  23. EndIf
  24. EndSub'richTextBox1_dragenter
  25. PrivateSubrichTextBox1_DragDrop(ByValsenderAsObject,ByValeAsDragEventArgs)HandlesrichTextBox1.DragDrop
  26. 'PastethetextintotheRichTextBoxwhereatselectionlocation.
  27. richTextBox1.SelectedText=e.Data.GetData("System.String",True).ToString()
  28. EndSub'richTextBox1_DragDrop

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...