VB.Net程序设计:拖放数据操作-如何拖放自定义的数据类型

首先自定义数据类型:类:

  Private   Class AppDMClass
        
Private MyAppName As String
        
Private MyAppPath As String

        
Public Sub New()
        
End Sub


        
Public Sub New(ByVal Name As StringByVal Path As String)
            MyAppName 
= Name
            MyAppPath 
= Path
        
End Sub


        
Public Property Name() As String
            
Get
                
Return MyAppName
            
End Get
            
Set(ByVal value As String)
                MyAppName 
= value
            
End Set
        
End Property


        
Public Property Path() As String
            
Get
                
Return MyAppPath
            
End Get
            
Set(ByVal value As String)
                MyAppPath 
= value
            
End Set
        
End Property

    
End Class

 在一个ListBox中已经加载有AppDMClass类型是数据:

  Private   Sub LoadXml2List(ByVal XmlFilePath As String)
        XmlDoc 
= New XmlDocument
        XmlDoc.Load(XmlFilePath)
        Root 
= XmlDoc.SelectSingleNode("AppList")
        NodeList 
= Root.ChildNodes
        AppList.Clear()
        
If NodeList.Count > 0 Then
            
For Each Node In NodeList
                AppList.Add(
New AppDMClass(Node.FirstChild.InnerText, Node.LastChild.InnerText))
                
'Node.ChildNodes(0).InnerText '当只有两个节点=node.FirstChild.InnerText 
                'Node.ChildNodes(1).InnerText '可以等于=node.LastChild .InnerText 
            Next
            BindData2List()
        
End If
    
End Sub


 
Private   Sub BindData2List()
        
Me.ListBoxAPP.DataSource = Nothing
        
Me.ListBoxAPP.DataSource = AppList
        
Me.ListBoxAPP.DisplayMember = "Name"
        
Me.ListBoxAPP.ValueMember = "Path"
    
End Sub

启动拖放数据:在拖放过程中传送自定义类型数据:AppDMClass。

  Private   Sub ListBoxAPP_MouseMove(ByVal sender As ObjectByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBoxAPP.MouseMove
        
If Me.ListBoxAPP.SelectedIndex > -1 Then
            
Dim DM As AppDMClass
            DM 
= CType(AppList.Item(Me.ListBoxAPP.SelectedIndex), AppDMClass)
            
Me.ListBoxAPP.DoDragDrop(DM, DragDropEffects.Copy)
        
End If
    
End Sub

最后对拖放数据进行操作:注意接收对象:BtStartApp(一个按钮)的AllowDrop设置为True。

  Private   Sub BtStartApp_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles BtStartApp.DragDrop
        
If e.Data.GetDataPresent(GetType(AppDMClass)) Then
            
Dim DM As AppDMClass = e.Data.GetData(GetType(AppDMClass))
            System.Diagnostics.Process.Start(DM.Path)
'处理拖放进来的数据。
        End If
    
End Sub


    
Private   Sub BtStartApp_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles BtStartApp.DragEnter
        
If e.Data.GetDataPresent(GetType(AppDMClass)) Then
'启动拖放效果。
            e.Effect = DragDropEffects.Copy
        
End If
    
End Sub

程序界面:【拖放操作过程,选中聊天QQ,按住鼠标左键,拖到 启动程序按钮 上放开,就会自动运行QQ程序了。】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值