當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET ListBox.IndexFromPoint方法代碼示例

本文整理匯總了VB.NET中System.Windows.Forms.ListBox.IndexFromPoint方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET ListBox.IndexFromPoint方法的具體用法?VB.NET ListBox.IndexFromPoint怎麽用?VB.NET ListBox.IndexFromPoint使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Windows.Forms.ListBox的用法示例。


在下文中一共展示了ListBox.IndexFromPoint方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: New

Public Sub New()
   MyBase.New()

   'This call is required by the Windows Form Designer.
   InitializeComponent()

   richTextBox1.AllowDrop = True

End Sub

Private Sub listBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles listBox1.MouseDown
   ' Determines which item was selected.
   Dim lb As ListBox = CType(sender, ListBox)
   Dim pt As New Point(e.X, e.Y)
   'Retrieve the item at the specified location within the ListBox.
   Dim index As Integer = lb.IndexFromPoint(pt)

   ' Starts a drag-and-drop operation.
   If index >= 0 Then
      ' Retrieve the selected item text to drag into the RichTextBox.
      lb.DoDragDrop(lb.Items(index).ToString(), DragDropEffects.Copy)
   End If
End Sub


Private Sub richTextBox1_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles richTextBox1.DragEnter
   ' If the data is text, copy the data to the RichTextBox control.
   If e.Data.GetDataPresent("Text") Then
      e.Effect = DragDropEffects.Copy
   End If
End Sub

Private Sub richTextBox1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles richTextBox1.DragDrop
   ' Paste the text into the RichTextBox where at selection location.
   richTextBox1.SelectedText = e.Data.GetData("System.String", True).ToString()
End Sub
開發者ID:VB.NET開發者,項目名稱:System.Windows.Forms,代碼行數:36,代碼來源:ListBox.IndexFromPoint

示例2: MainClass

' 導入命名空間
Imports System.Drawing.Drawing2D
Imports System
Imports System.Drawing.Text
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Math

Public Class MainClass

   Shared Sub Main()
       Dim form1 As Form = New Form1()
       Application.Run(form1)
   End Sub 

End Class

Public Class Form1
    Private m_DragSource As ListBox = Nothing

    Private Sub Form1_Load(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles MyBase.Load
        lstUnselected.AllowDrop = True
        lstSelected.AllowDrop = True

        ' Add event handlers.
        AddHandler lstUnselected.MouseDown, AddressOf List_MouseDown
        AddHandler lstUnselected.DragOver, AddressOf List_DragOver
        AddHandler lstUnselected.DragDrop, AddressOf List_DragDrop
        AddHandler lstUnselected.DragLeave, AddressOf List_DragLeave

        AddHandler lstSelected.MouseDown, AddressOf List_MouseDown
        AddHandler lstSelected.DragOver, AddressOf List_DragOver
        AddHandler lstSelected.DragDrop, AddressOf List_DragDrop
        AddHandler lstSelected.DragLeave, AddressOf List_DragLeave
    End Sub

    Private Sub List_MouseDown(ByVal sender As Object, _
     ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim this_list As ListBox = DirectCast(sender, ListBox)
        this_list.SelectedIndex = this_list.IndexFromPoint(e.X, e.Y)
        If this_list.SelectedIndex < 0 Then Exit Sub
        m_DragSource = this_list
        this_list.DoDragDrop(this_list.SelectedItem.ToString,DragDropEffects.Move)
        m_DragSource = Nothing
    End Sub

    Private Sub List_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
        If m_DragSource Is Nothing Then Exit Sub

        e.Effect = DragDropEffects.Move
        Dim this_list As ListBox = DirectCast(sender, ListBox)
        Dim pt As Point = this_list.PointToClient(New Point(e.X, e.Y))
        Dim drop_index As Integer = this_list.IndexFromPoint(pt.X, pt.Y)
        this_list.SelectedIndex = drop_index
    End Sub

    Private Sub List_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim this_list As ListBox = DirectCast(sender, ListBox)
        this_list.SelectedIndex = -1
    End Sub

    ' Accept the drop.
    Private Sub List_DragDrop(ByVal sender As Object, _
     ByVal e As System.Windows.Forms.DragEventArgs)
        Dim this_list As ListBox = DirectCast(sender, ListBox)
        MoveItem(e.Data.GetData(DataFormats.Text).ToString, _
            m_DragSource, this_list, e.X, e.Y)
    End Sub

    ' Move the value txt from drag_source to drop_target.
    Private Sub MoveItem(ByVal txt As String, ByVal drag_source As ListBox, _
     ByVal drop_target As ListBox, ByVal X As Integer, ByVal Y As Integer)
        Dim drop_index As Integer = drop_target.SelectedIndex
        If drop_index < 0 Then
            drop_index = drop_target.Items.Add(txt)
        Else
            drop_target.Items.Insert(drop_target.SelectedIndex, txt)
        End If

        drop_target.SelectedIndex = drop_index

        If drag_source Is drop_target Then
            Dim target_index As Integer = drag_source.FindStringExact(txt)
            If target_index = drop_index Then _
                target_index = drag_source.FindStringExact(txt, target_index)
            drag_source.Items.RemoveAt(target_index)
        Else
            drag_source.Items.Remove(txt)
        End If
    End Sub
End Class



<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label1 = New System.Windows.Forms.Label
        Me.lstSelected = New System.Windows.Forms.ListBox
        Me.lstUnselected = New System.Windows.Forms.ListBox
        Me.SuspendLayout()
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(152, 0)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(144, 16)
        Me.Label2.TabIndex = 11
        Me.Label2.Text = "Right"
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(0, 0)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(144, 16)
        Me.Label1.TabIndex = 10
        Me.Label1.Text = "Left"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'lstSelected
        '
        Me.lstSelected.FormattingEnabled = True
        Me.lstSelected.Items.AddRange(New Object() {"1", "2", "3", "4", "5", "6", "7", "8"})
        Me.lstSelected.Location = New System.Drawing.Point(152, 16)
        Me.lstSelected.Name = "lstSelected"
        Me.lstSelected.Size = New System.Drawing.Size(144, 264)
        Me.lstSelected.TabIndex = 9
        '
        'lstUnselected
        '
        Me.lstUnselected.FormattingEnabled = True
        Me.lstUnselected.Items.AddRange(New Object() {"A", "B", "C", "D", "E", "F", "G", "H"})
        Me.lstUnselected.Location = New System.Drawing.Point(0, 16)
        Me.lstUnselected.Name = "lstUnselected"
        Me.lstUnselected.Size = New System.Drawing.Size(144, 264)
        Me.lstUnselected.TabIndex = 8
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(297, 281)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.lstSelected)
        Me.Controls.Add(Me.lstUnselected)
        Me.Name = "Form1"
        Me.Text = "DragBetweenListBoxes"
        Me.ResumeLayout(False)

    End Sub
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents lstSelected As System.Windows.Forms.ListBox
    Friend WithEvents lstUnselected As System.Windows.Forms.ListBox

End Class
開發者ID:VB程序員,項目名稱:System.Windows.Forms,代碼行數:178,代碼來源:ListBox.IndexFromPoint


注:本文中的System.Windows.Forms.ListBox.IndexFromPoint方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。