Listview - Drag & Drop cursor to PC desktop - vb.net

I'm using a function to change my cursor to icon when dragging item from Listview to PC Desktop. Cursor works when I drag item directly from "floating" window to desktop, but It changes when I try to drag It from Listview which is embedded inside another control (which is my case). In another words, when cursor leaves Listview and touches another control on my form (such as Panel), my cursor changes to something like image below:
How could I obtain My cursor same all the way until I drop Item ? Here is my code so far:
Private Sub Listview1_ItemDrag(sender As Object, e As ItemDragEventArgs) Handles Listview1.ItemDrag
Dim Lview As System.Windows.Forms.ListView = DirectCast(sender, System.Windows.Forms.ListView)
Dim filecollection As New ArrayList
If Lview.SelectedItems.Count > 0 Then
For Each myItem In Lview.SelectedItems
If File.Exists(myItem.Tag.ToString) Then
filecollection.Add(myItem.Tag.ToString)
ElseIf Directory.Exists(myItem.Tag.ToString) Then
filecollection.Add(myItem.Tag.ToString)
End If
Next
Dim result = Lview.DoDragDrop(New DataObject(DataFormats.FileDrop, DirectCast(IzbraniPredmeti.ToArray(GetType(String)), String())), DragDropEffects.Copy)
If result = DragDropEffects.Copy Then
Me.Cursor = Cursors.Default
End If
End If
End Sub
Private Sub Listview1_GiveFeedback(sender As Object, e As GiveFeedbackEventArgs) Handles Listview1.GiveFeedback
e.UseDefaultCursors = False
Dim Lview As System.Windows.Forms.ListView = DirectCast(sender, System.Windows.Forms.ListView)
Dim img As New Bitmap(MyImageList.Images(CacheShellIcon(Lview.SelectedItems(0).Tag.ToString)))
Me.Cursor = my_functions.CreateCursor(img) 'function to create cursor
End Sub
Private Sub Listview1_DragOver(sender As Object, e As DragEventArgs) Handles Listview1.DragOver
Dim Lview As System.Windows.Forms.ListView = DirectCast(sender, System.Windows.Forms.ListView)
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
End If
End Sub

Related

Dynamically create multiple button and assign single click and double click comand

I created multiple button dynamically inside a panel based on multiple click on add button. Each button deletes When I click on it.
I want each button to delete when I single click on it, and say hello when I double click. Thank you.
I have tried using this code to delete which works fine, but I cannot figure how to assign seperate code to display hello when I double click or right click on it without affecting delete aspect of it.
Private Sub btnDynamic_Click(ByVal sender As Object, ByVal e As EventArgs)
'Reference the Button which was clicked.
Dim button As Button = CType(sender, Button)
'Determine the Index of the Button.
Dim index As Integer = Integer.Parse(button.Name.Split("_")(1))
'Find the TextBox using Index and remove it.
FlowLayoutPanel1.Controls.Remove(FlowLayoutPanel1.Controls.Find(("btnDynamic_" & index), True)(0))
'Remove the Button.
FlowLayoutPanel1.Controls.Remove(button)
'Rearranging the Location controls.
For Each btn As Button In FlowLayoutPanel1.Controls.OfType(Of Button)()
Dim controlIndex As Integer = Integer.Parse(btn.Name.Split("_")(1))
If (controlIndex > index) Then
Dim btn1 As Button = CType(FlowLayoutPanel1.Controls.Find(("btnDynamic_" & controlIndex), True)(0), Button)
btn1.Top = (btn.Top - 25)
'txt.Top = (txt.Top - 25)
End If
Next
End Sub
Here is the create button code:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If TextBox1.Text = "" Then
Exit Sub
End If
Dim count As Integer = Form2.FlowLayoutPanel2.Controls.OfType(Of Button).ToList.Count
Dim button As Button = New Button
button.Size = New System.Drawing.Size(28, 21)
button.Name = "btnDynamic_" & (count + 1)
button.Text = TextBox1.Text
AddHandler button.Click, AddressOf Me.button_click
Form2.FlowLayoutPanel2.Controls.Add(button)
End Sub
You have to enable DoubleClick option first.
You do so with:
Public Class DoubleClickButton
Inherits Button
Public Sub New()
SetStyle(ControlStyles.StandardClick Or ControlStyles.StandardDoubleClick, True)
End Sub
End Class
Then create a "DoubleClick" and "SingleClick" methods and assign them to (new custom) buttons when you are creating them.
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
For x = 0 To 2
Dim ButtonX As New DoubleClickButton
ButtonX.Text = x
AddHandler ButtonX.MouseClick, AddressOf SingleClickE
AddHandler ButtonX.MouseDoubleClick, AddressOf DoubleClickE
FlowLayoutPanel1.Controls.Add(ButtonX)
Next
End Sub
Private Sub SingleClickE(sender As Object, e As MouseEventArgs)
Debug.Print("Hello!")
End Sub
Private Sub DoubleClickE(sender As Object, e As MouseEventArgs)
FlowLayoutPanel1.Controls.Remove(sender)
End Sub
Thank you all for attempting to answer my question. I have been able to figure it out. I intend firing diffrent events with dynamically created buttons using single click, double click or right click. I ended up using single and right click command. Though would have prefered double click, but it is ok. Below is the code.
'Create buttons
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i As Integer = 1 To 5
Dim button As New Button
button.Text = i
AddHandler button.Click, AddressOf SingleClickE
AddHandler button.MouseDown, AddressOf RightClickE
FlowLayoutPanel1.Controls.Add(button)
Next
End Sub
'Fire it up with single click
Private Sub SingleClickE(sender As Object, e As MouseEventArgs)
Dim button As Button = CType(sender, Button)
MsgBox("Hello World")
End Sub
'Fire it up with Right Click
Private Sub RightClickE(sender As Object, e As MouseEventArgs)
Dim button As Button = CType(sender, Button)
If e.Button = MouseButtons.Right Then
FlowLayoutPanel1.Controls.Remove(button)
End If
End Sub

How to customize the click event of dynamically created buttons from string array?

Here is the code I have:
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddNewButton()
End Sub
Public Sub AddNewButton()
Dim buttonTop As Integer = 100
For Each item As String In Globals.candidates
Dim btn As New System.Windows.Forms.Button()
Dim Location As New Point(100, (buttonTop + 20))
btn.Location = Location
btn.Text = item
btn.Width = 150
AddHandler btn.Click, AddressOf Me.buttonClick
Me.Controls.Add(btn)
buttonTop += 20
Next
End Sub
Private Sub buttonClick()
Dim result As Integer = MessageBox.Show(String.Format("Did you select {0} ?", ???????????), "Confirmation", MessageBoxButtons.YesNo)
If result = DialogResult.Yes Then
MessageBox.Show("Yes pressed")
Else
MessageBox.Show("No pressed")
End If
End Sub
End Class
Globals.candidates is a global string array variable that holds a name "LastName, FirstName" and when the form is loaded I call the AddNewButton() Sub and it creates buttons for each item in the string array. No problem.
If you see in my code the "??????????" section, I don't know how to reference the dynamically created buttons's text so that I can display the proper "Did you select thisButton.text" properly.
Any help is appreciated.
Thanks!
EDIT:
Code changed as per suggestions: (Working)
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddNewButton()
End Sub
Public Sub AddNewButton()
Dim buttonTop As Integer = 100
For Each item As String In Globals.candidates
Dim btn As New System.Windows.Forms.Button()
Dim Location As New Point(100, (buttonTop + 20))
btn.Location = Location
btn.Text = item
btn.Width = 150
AddHandler btn.Click, AddressOf Me.buttonClick
Me.Controls.Add(btn)
buttonTop += 20
Next
End Sub
Private Sub buttonClick(sender As Object, e As EventArgs)
Dim btn As Button = DirectCast(sender, System.Windows.Forms.Button)
Dim result As Integer = MessageBox.Show(String.Format("Did you select {0} ?", btn.Text), "Confirmation", MessageBoxButtons.YesNo)
If result = DialogResult.Yes Then
MessageBox.Show("Yes pressed")
Else
MessageBox.Show("No pressed")
End If
End Sub
End Class
You need to put the proper signature on your event handler:
Private Sub buttonClick(sender As Object, e As EventArgs)
Then, you can use the sender object (which will be whichever button was clicked)
Dim button As Button = DirectCast(sender, System.Windows.Forms.Button)
Dim result As Integer = MessageBox.Show(String.Format("Did you select {0} ?", button.Text), "Confirmation", MessageBoxButtons.YesNo)
To get a reference to the button clicked you need to declare the event handler of the button click with the two parameters that are passed to it by the form engine.
Private Sub buttonClick(sender as Object, e as EventArgs)
Now, this correct event handler receives a parameter named sender that is the control reference to the button clicked. You could cast it to a button and then extract the Text property
Private Sub buttonClick(sender as Object, e as EventArgs)
Dim btn = DirectCast(sender, System.Windows.Forms.Button)
if btn IsNot Nothing then
Dim result As Integer = MessageBox.Show(String.Format("Did you select {0} ?", btn.Text), "Confirmation", MessageBoxButtons.YesNo)
If result = DialogResult.Yes Then
MessageBox.Show("Yes pressed")
Else
MessageBox.Show("No pressed")
End If
End If
End Sub
This should be enough in this simple case where you have just a string data, but, if you need to associate a more complex object (like an instance of a Person class for example) you could use the Tag property of every dynamically added button to store there a reference to the instance of the class
As a side note, your code works also without the declaration of the two parameters because you have the Option Strict configuration set to OFF. This is a bad practice because it introduces subtle errors in you parameters usage and in automatic conversions of type. If you are just starting with a new project remember to set its property Option Strict to ON

ContextMenuStrip selected item lost in UserControl

I have a VB.NET User Control which is embedded into another User Control and that into a form. The inner User Control has a contextmenustrip triggered by a DataGridView row click. This successfully activates the event handler (I see the "OK" message), but the sender does not send the selected item (I don't see the other MsgBox messages). Here is the code:
Public CMSV As ContextMenuStrip
Private grdSourceViewerCurrentRow As Long
Public Sub grdSourceViewer_RowHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles grdSourceViewer.RowHeaderMouseClick
'code to review/edit source details
Select Case e.Button
Case Windows.Forms.MouseButtons.Right
grdSourceViewerCurrentRow = e.RowIndex 'retain for downstream code
CMSV = New ContextMenuStrip
AddHandler CMSV.MouseClick, AddressOf SourceViewDocumentationEdit
CMSV.Items.Add("Edit")
CMSV.Items.Add("Transfer to Evidence")
Dim Pt As Point = New Point()
Pt.X = grdSourceViewer.PointToClient(Cursor.Position).X
Pt.Y = grdSourceViewer.PointToClient(Cursor.Position).Y + 20
CMSV.Show(sender, Pt)
Case Windows.Forms.MouseButtons.Left
Exit Sub
Case Else
Exit Sub
End Select
End Sub
Public Sub SourceViewDocumentationEdit()
MsgBox("OK") 'I can see it reaches here
Dim cc As ToolStripItemCollection = CMSV.Items
Dim SelectedItem As Integer = -1
Dim SelectedValue As String = ""
For i As Integer = 0 To cc.Count - 1
If cc.Item(i).Selected Then
SelectedItem = i
SelectedValue = cc.Item(SelectedItem).Text
Exit For
End If
Next
Select Case SelectedValue
Case "Edit"
MsgBox("Edit code here")
Case "Transfer to Evidence"
MsgBox("Transfer code here")
End Select
End Sub
What is wrong here? Why am I losing the info about the item that was clicked?
Why are you recreating the menu each time?
At any rate, store the ToolStripMenuItem returned by CMSV.Items.Add() and wire that up instead.
Simplified example:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
CMSV = New ContextMenuStrip
Dim TSMI As ToolStripMenuItem
TSMI = CMSV.Items.Add("Edit")
AddHandler TSMI.Click, AddressOf TSMI_Click
TSMI = CMSV.Items.Add("Transfer to Evidence")
AddHandler TSMI.Click, AddressOf TSMI_Click
' ...
End Sub
Private Sub TSMI_Click(sender As Object, e As EventArgs)
Dim TSMI As ToolStripMenuItem = DirectCast(sender, toolstripmenuitem)
Select Case TSMI.Text
Case "Edit"
Debug.Print("...Edit Code...")
' use "grdSourceViewerCurrentRow " in here?
Case "Transfer to Evidence"
Debug.Print("...Transfer to Evidence Code...")
' use "grdSourceViewerCurrentRow " in here?
End Select
End Sub
You need to use a ToolStripMenuItem and not just a string. Then you can add the handler for it's click event.
Dim tsmi As New ToolStripMenuItem
tsmi.Text = "Edit"
AddHandler tsmi.Click, AddressOf ItemClicked
CMSV.Items.Add(tsmi)
Then the event sub:
Private Sub ItemClicked(sender As Object, e As EventArgs)
'item clicked
'sender object would be the ToolStripMenuItem
End Sub

drag and drop to tablelayoutpanel from listview

I am trying to build a control which implements a tablelayoutpanel for the design and placement of other controls within the control - I need to add functionality which will allow the tablelayoutpanel to accept content from a listview (It does not even need to process it in any fashion at this point) - I, however, can not get the tablelayout panel to even display that it will accept data - only displays the circle/slash symbol. These are kept in 2 separate child mdi forms within the same parent.
currently I have in my listview form
Private Sub Jboard_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.AllowDrop = True
ListView2.AllowDrop = True
end sub
Private Sub ListView2_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragOver, ListView2.DragOver
If e.Data.GetDataPresent(GetType(ListViewItem)) Then
e.Effect = DragDropEffects.All
End If
End Sub
Private Sub ListView2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView2.MouseDown
Dim item As ListViewItem = ListView2.HitTest(e.Location).Item
If item IsNot Nothing Then
ListView2.DoDragDrop(item, DragDropEffects.All)
End If
End Sub
on my new tablelayoutpanel control form I have
Me.AllowDrop = True
DboardScheduler1.AllowDrop = True
'dboardscheduler1 is my new control
in the code for the control I have
tablelayoutpanel1.AllowDrop = true
What am I missing?
It looks like you only coded the one side, you also need to tell the TLP(like) control how/what to do. Something like this (not sure of the constraints you want, like JUST LVs and only MOVE).
' NOT mousedown
Private Sub ItemDrag(sender As Object, e As ItemDragEventArgs) Handles ...
If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
' ToDo: Decide what to do with multiples. Singles only assumed
' add the item under the cusor as the first, effect as Move
DoDragDrop(e.Item, DragDropEffects.Move)
End Sub
LV Drag OVer:
' probably:
e.Effect = DragDropEffects.None
' because you cant really drop it here, but the No Action shows that it knows
' a D-D is happening.
TLP Drag OVer:
If (e.Data.GetDataPresent(GetType(ListViewItem)) = False) Then
e.Effect = DragDropEffects.None
Exit Sub
Else
e.Effect = DragDropEffects.Move ' or link maybe
End If
TLP DragDrop:
Dim dragLVI As ListViewItem
' get text and do whatever with it
If (e.Data.GetDataPresent(GetType(ListViewItem)) = False) Then
e.Effect = DragDropEffects.None
Exit Sub
Else
dragLVI = CType(e.Data.GetData(GetType(ListViewItem)), _
ListViewItem)
newTextThing = dragLVI.SubItems(0).Text
End If
Something along those lines. The point is that you have to write code for the piece being dropped on.

danamically creating controlltabs that can be used

I'm trying to dynamically create tabcontrols which works fine; the tabs are being created however. Once created I would also like them to become clickable and execute other code, this now posing a problem.
The code I’m using to create a tab is as follows
' do whatever wtih filename
Dim myTabPage As New TabPage()
myTabPage.Text = TextBox4.Text
TabControl1.TabPages.Add(myTabPage)
TabPage1.Hide()
Not so nice cause I can now fill my form with as many tabs as I like however none of them can be clicked to execute futher code???
EDIT:
Private Sub TabControl_SelectedIndexchaged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
Dim TabName As String
TabName = TabControl1.SelectedTab.Name
If TabName.Contains("TabPage") Then
' Do something
MsgBox("new tab created")
End If
End Sub
You have to add an event handler for the TabPage click event:
Dim myTabPage As New TabPage
myTabPage.Text = TextBox4.Text
AddHandler myTabPage.Click, AddressOf TabPage1_Click
TabControl1.TabPages.Add(myTabPage)
Which will call this code:
Private Sub TabPage1_Click(sender As Object, e As EventArgs)
MessageBox.Show(DirectCast(sender, TabPage).Text)
End Sub
Per your edit, you would have to add the name property:
myTabPage.Name = TextBox4.Text
And your SelectedIndexChanged event:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) _
Handles TabControl1.SelectedIndexChanged
If TabControl1.SelectedTab IsNot Nothing Then
MessageBox.Show(TabControl1.SelectedTab.Name)
End If
End Sub