微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

章鱼哥—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

原文地址:https://www.jb51.cc/vb/257773.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐