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

来源:互联网 发布:淘宝企业账号怎么注册 编辑:程序博客网 时间:2024/05/17 01:58

这篇文章讲述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. Public Sub New()   
  2.    MyBase.New()   
  3.   
  4.    'This call is required by the Windows Form Designer.  
  5.    InitializeComponent()   
  6.   
  7.    richTextBox1.AllowDrop = True  
  8.   
  9. End Sub  
  10.   
  11. Private Sub listBox1_MouseDown(ByVal sender As ObjectByVal e As System.Windows.Forms.MouseEventArgs) Handles listBox1.MouseDown   
  12.    ' Determines which item was selected.  
  13.    Dim lb As ListBox = CType(sender, ListBox)   
  14.    Dim pt As New Point(e.X, e.Y)   
  15.    'Retrieve the item at the specified location within the ListBox.  
  16.    Dim index As Integer = lb.IndexFromPoint(pt)   
  17.   
  18.    ' Starts a drag-and-drop operation.  
  19.    If index >= 0 Then  
  20.       ' Retrieve the selected item text to drag into the RichTextBox.  
  21.       lb.DoDragDrop(lb.Items(index).ToString(), DragDropEffects.Copy)   
  22.    End If  
  23. End Sub 'listBox1_MouseDown  
  24.   
  25.   
  26. Private Sub richTextBox1_DragEnter(ByVal sender As ObjectByVal e As DragEventArgs) Handles richTextBox1.DragEnter   
  27.    ' If the data is text, copy the data to the RichTextBox control.  
  28.    If e.Data.GetDataPresent("Text"Then  
  29.       e.Effect = DragDropEffects.Copy   
  30.    End If  
  31. End Sub 'richTextBox1_DragEnter  
  32.   
  33. Private Sub richTextBox1_DragDrop(ByVal sender As ObjectByVal e As DragEventArgs) Handles richTextBox1.DragDrop   
  34.    ' Paste the text into the RichTextBox where at selection location.  
  35.    richTextBox1.SelectedText = e.Data.GetData("System.String"True).ToString()   
  36. End Sub 'richTextBox1_DragDrop  
0 0
原创粉丝点击