vb.net how to handle text dragged onto a button to open a new form with the dragged text directly copied to the richtextbox on the new form? - vb.net

I want to achieve the following:
The user drags text from any open window not related to my application ( like firefox or word, for example) onto button1 on form1 in my application. when he/she does that, a new form (called form2 that contains a richtextbox) will open and the dragged text is directly copied (or inserted) into the richtextbox of the new form. button1 has allowdrop set to true. Beyond that I don't know how to proceed.
I tried:
e.effects = DragDropEffects.Copy
But it seems it is not enough. Could you help please?
Thanks

Learning about Drag and Drop would be the first step. http://www.vb-helper.com/howto_net_drag_drop.html -or- http://msdn.microsoft.com/en-us/library/aa289508%28VS.71%29.aspx.
Essentially, you need to enable the drag and drop for the target, handle the drag and drop events, and then implement your desired action.
From MSDN regarding Dragging Text:
Private MouseIsDown As Boolean = False
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
' Set a flag to show that the mouse is down.
MouseIsDown = True
End Sub
Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove
If MouseIsDown Then
' Initiate dragging.
TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
End If
MouseIsDown = False
End Sub
Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(DataFormats.Text)) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Copy
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub TextBox2_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
' Paste the text.
TextBox2.Text = e.Data.GetData(DataFormats.Text)
End Sub

I figured it out. I'm sharing so others might benefit.
First, I declared a global variable in one of the modules:
Public draggedText As String = ""
Second, I handled the dragdrop event on the button as follows:
Private Sub button1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles button1.DragDrop
draggedText = e.Data.GetData(DataFormats.Text)
frm_form2.Show()
End Sub
Third, in the load event of frm_form2 I added the following:
If draggedText <> "" Then
richTextBox1.Text = draggedText
draggedText = ""
End If
That's it. Not as complicated as I thought. Also, you can add the code for the dragEnter event mentioned in the previous answer to change how the cursor looks.
I hope this helps.

Related

How Do I Force An Action When A Dialog Box Closes In VB.Net?

I am wondering how to force my main gui to update when a dialog box such as a SaveFileDialog box closes. I have tried Main.LostFocus, Main.GotFocus, Main.Enter, Main.MouseEnter, Main.MouseLeave, and Main.MouseMove, but no matter what function I try I can never get the result I am looking for.
The dialog box is opened when a picture is clicked. The picture changes when it is clicked and again when the icon_new.MouseUp is called. The problem is that it acts correctly until the dialog is closed. At this point the picture changes back to the image it had when the mouse was over it.
Here is what the picture does regularly:
Private Sub icon_new_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles icon_new.MouseDown
icon_new.Image = My.Resources.NewMapClick
End Sub
Private Sub icon_new_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles icon_new.MouseUp
icon_new.Image = My.Resources.NewMapHover
End Sub
Private Sub icon_new_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles icon_new.MouseEnter
icon_new.Image = My.Resources.NewMapHover
End Sub
Private Sub icon_new_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles icon_new.MouseLeave
icon_new.Image = My.Resources.NewMapDefault
End Sub
This works until the dialog box is closed, at which point it the image becomes NewMapHover when it should be NewMapDefault, because the mouse is no longer within the bounds of the picture. In the calls such as Main.LostFocus, Main.GotFocus, or Main.Whatever I have icon_new.image = My.Resources.NewMapDefault, but even if this call triggers, the image ends up as NewMapHover. I'm not sure why this is happening or how to fix it. Any help is greatly appreciated.
EDIT: This is the click event that calls the dialog_box
Private Sub icon_new_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles icon_new.Click
If file_created = True Then
save()
Else 'file_created = false'
SaveWindow.FileName = txt_name.Text
If SaveWindow.ShowDialog() = DialogResult.OK Then
file_path = SaveWindow.FileName
End If
file_created = True
End If
save()
new_file()
End Sub
If the file hasn't been saved, then a dialog box opens, prompting the user to save the file. I've also toyed with a MsgBox() that has Yes, No, and Cancel, prompts, but for simplicity I took it out because the results were the same and one third of the time the SaveFile dialog will come up anyway.
Try doubleclicking your SaveFileDialog, which will open up the SaveFileDialog_FileOk event code, and then put the same code there as in the MouseLeave event.
This event will fire when the SaveFileDialog is about to close after you've pressed the "Save" button.
EDIT:
You can try doing this in your click event:
Dim DResult As DialogResult
DResult = SaveFileDialog1.ShowDialog()
If DResult = System.Windows.Forms.DialogResult.OK Then
'Code for when you press the save button, and when the image should change
ElseIf DResult = System.Windows.Forms.DialogResult.Cancel Then
'Code for image change...
End If
Try
Private Sub icon_new_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles icon_new.MouseUp
Dim pnt As Point
Dim rect As Rectangle
rect.X = icon_new.Location.X
rect.Y = icon_new.Location.Y
rect.Width = icon_new.Width
rect.Height = icon_new.Height
pnt = PointToClient(Cursor.Position)
If Not rect.Contains(pnt) Then
Return
End If
icon_new.Image = My.Resources.NewMapHover
End Sub
Valter
This is the code I originally had.
Private Sub icon_new_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles icon_new.MouseUp
icon_new.Image = My.Resources.NewMapHover
End Sub
I added the variable dialog_open as a boolean and set it to true whenever I called SaveFile.ShowDialog(). I then changed my MouseUp event to this:
Private Sub icon_new_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles icon_new.MouseUp
icon_new.Image = My.Resources.NewMapHover
If dialog_open = True Then
icon_new.Image = My.Resources.NewMapDefault
dialog_open = False
End If
End Sub
Thanks to #valter and #Kevin Hogg for suggesting I edit the MouseUp event.

How to know if the mouse is down on clicking a column divider in a Datagridview Control

I am trying to show a message to the user that will tell them
"don't resize column"
as soon as they try to drag the column divider in a DataGridView.
Is there any event like a DataGridViewColumnDividerMouseDrag in vb.net?
I would like to know how my mouse pointer is behaving when the column divider of a DataGridView is clicked
Thanks in advance...
You could create a global variable that captures the MouseDown on DataGridView then use a DataGridView.ColumnWidthChanged event to capture a change in column width.
You would need to add some code to make the DataGridView save the width before the change and revert it back to that width. This code will allow you to mix MouseDown and DataGridView.ColumnWidthChanged
Not really a columndividermouseDrag event but its a start
Global variables
Dim mousestatus As Boolean
Dim global_e As MouseEventArgs
Capture Events
Private Sub DataGridView2_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView2.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
mousestatus = True
global_e = e
End If
End Sub
Private Sub DataGridView2_Mouseup(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView2.MouseUp
If e.Button = Windows.Forms.MouseButtons.Left Then
mousestatus = False
global_e = e
End If
End Sub
Private Sub DataGridView2_ColumnWidthChanged(ByVal sender As Object, ByVal e As _
DataGridViewColumnEventArgs) Handles DataGridView2.ColumnWidthChanged
If mousestatus = True Then
MessageBox.Show("don't resize column")
Else
End If
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.

How to check focused TextBox in vb.net winforms?

I have multiple textbox in a form. How do I know what textbox the cursor currently is?
Trying to do something like this:
If TextBox2.Focus() = True Then
MessageBox.Show("its in two")
ElseIf TextBox3.Focus = True Then
MessageBox.Show("its in three")
End If
But I think its not working.
TextBox.Focus actually assigns the focus to the given textbox. What you're looking for is TextBox.Focused. :)
In fact, all form controls have the Focused property.
I know this already has an accepted answer but I just think this method is a bit easier and should be up here for people who find this through Google or whatever.
Public focussedTextBox As TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each control As Control In Me.Controls
If control.GetType.Equals(GetType(TextBox)) Then
Dim textBox As TextBox = control
AddHandler textBox.Enter, Sub() focussedTextBox = textBox
End If
Next
End Sub
This way you can then just refer to the focussedTextBox at any time. You should make sure that you check that there is a focussedTextBox before you do however becuase when the application first loads there will not be. You can do this using:
If Not focussedTextBox Is Nothing Then
...
End If
Alternatively, you could set focussedTextBox to a TextBox of your choice on form load, either by setting its value or by focussing the TextBox.
Obviously, it will not work if you are calling your code in a Button_Click because when you click the Button then the focus is itself goes to the Button which you have clicked.
You can do two things:
Make a combined Focus event for all TextBoxes and check its Sender object.
Private Sub TextBox_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter, TextBox3.Enter
Dim currTextBox As TextBox = sender
If currTextBox.Equals(TextBox2) Then
MessageBox.Show("it's in two")
ElseIf currTextBox.Equals(TextBox3) Then
MessageBox.Show("it's in three")
End If
End Sub
OR
Take a global string variable, and set its value at each TextBox_Focus event, then check string value in the button click event.
Dim str As String
Private Sub TextBox2_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter
str = "two"
End Sub
Private Sub TextBox3_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.Enter
str = "three"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("it's in " & str)
End Sub

Drag drop a usercontrol in vb.net

I have, what should be , a very simple problem, but now I have used 5 hours without results.
I have a usercontrol, UserControl1, which I want to drag and drop on my form, Form1.
That’s it. It should be simple, but I have googled for hours without results. Does anybody have a sample code to fix this?
I dont know what a user control is (I'm still learning) but I found something that might help.
In this code, add two TextBox controls to a form and set the AllowDrop property of the second TextBox control to True.
Then use this code to enable drag and drop
Private MouseIsDown As Boolean = False
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
' Set a flag to show that the mouse is down.
MouseIsDown = True
End Sub
Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove
If MouseIsDown Then
' Initiate dragging.
TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
End If
MouseIsDown = False
End Sub
Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(DataFormats.Text)) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Copy
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub TextBox2_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
' Paste the text.
TextBox2.Text = e.Data.GetData(DataFormats.Text)
End Sub
I hope that you can use this for a usercontrol. Good luck and comment!
Here is the code, I have used, to get it work.
Now I have a form, Form1, and a usercontrol, Usercontrol1. To drag the usercontrol, I inserted a panel in the top of the usercontrol, and only if the user pressed the panel (panel1), the control should to move - like normal windows forms.
Public Class UserControl1
Shared mypositionX As Integer
Shared mypositionY As Integer
Shared mBlnFormDragging As Boolean
Shared drawBeginX As Integer
Shared drawBeginY As Integer
Shared drawing As Boolean
Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
If mBlnFormDragging = True Then
Dim position As Point = Form1.PointToClient(MousePosition)
Me.Location = New Point(position)
End If
End Sub
Private Sub Panel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
' Dim dd1 As DragDropEffects = DoDragDrop(ParentForm, DragDropEffects.Move)
mBlnFormDragging = False
Dim position As Point = Form1.PointToClient(MousePosition)
Location = New Point(position)
End Sub
Public Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
'Dim dd1 As DragDropEffects = DoDragDrop(ParentForm, DragDropEffects.Move)
mBlnFormDragging = True
End Sub