Drop a file from shell - vb.net

I am trying to drop a file from windows explorer to my form and I am almost successful :)
After opening explorer window in shell I can drop a file to my form where I have one message box/dialog before accepting.
Problem is in fact that my messagebox with question opens in back of explorer window.
Here is a code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.AllowDrop = True
End Sub
Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim Files() As String
Files = e.Data.GetData(DataFormats.FileDrop)
If Files.Length > 0 Then
Dim ret As Integer = MsgBox("Would you like to upload file?" & vbNewLine & Files(0), MsgBoxStyle.OkCancel + MsgBoxStyle.Question, "Decide please")
If ret = DialogResult.OK Then
myModule.UploadF()
End If
End If
End If
End Sub
Private Sub Form1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.All
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub btn_open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_open.Click
Shell("explorer c:\", AppWinStyle.NormalFocus)
End Sub
End Class
1) Is here any way to get messagebox to pop in front of explorer window?
2) How to close opened explorer window from my program after usage?
EDIT: Solution for...
1) is to place Me.Activate before MsgBox!
2) for that I still don't find a solution.

I know you have already accepted an answer, but i agree with Hans that this implementation is flawed. If the user must click an upload button, then launch an openfiledialog. Opening a plain explorer window that they must drag from is incredibly counter intuitive.
You can keep the drag and drop functionality, but let the user open their own explorer window to use that.
A more intuitive implementation would be to add the dragdrop functionality to a panel, and have a label and a button so the user can choose either method.
Something like this:

Try this:
Add a Dialog to your project. (Found in the list of addable things like form, class etc).
Add a label to it and change the text in the constructor.
In the Dialogs paint event add this:
Me.BringToFront()
Then use this dialog instead of MsgBox, it should provide what you need.
It's somewhat of a workaround but it should work.
Edit:
Right, found a better solution.
Add this to the code written in this question before calling MsgBox:
Me.BringToFront()
Me.TopMost = True
And you will be fine.

Related

How to do you make a ListBox display all files in a drive?

I'm coding an Anti-Virus at the moment, so it's been very complicated to code it and design it. Anyway, the other day I ran into a problem, where my ListBox is not displaying all the files that are in the selected drive/directory.
I'll put some code and images so you get the idea.
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
ListBox1.Items.Clear()
ListBox3.Items.Clear()
FolderBrowserDialog1.SelectedPath = Path.GetPathRoot(Environment.SystemDirectory) & "Boot\"
On Error Resume Next
For Each strDir As String In System.IO.Directory.GetDirectories(FolderBrowserDialog1.SelectedPath)
For Each strFile As String In System.IO.Directory.GetFiles(strDir)
ListBox1.Items.Add(strFile)
ListBox3.Items.Add(strFile)
Next
Next
Timer1.Start()
End Sub
However, instead of the files appearing in the ListBox (ListBox3), it just gives a black screen. Maybe I should remove the TabControl that it is surrounded by?
See how it's black? It even happens when I run it.
Hope this helps! Comment if you need more information.
You may want to simplify your events by creating subs and standardizing the code instead of embedding it directly into the button click. I have created a working example of how to load directories and files dynamically.
I would also recommend doing a isolated experiment, you can easily throw together a test project to isolate the directories in question and the layout you are trying to achieve. It could be that there are other events causing noise in your debugging.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
load_dirs(ListBox2, "H:\")
End Sub
Sub load_files(LB As ListBox,
IO_dir As String)
Dim Files_ = IO.Directory.GetFiles(IO_dir)
With LB.Items
.Clear()
.AddRange(Files_)
End With
End Sub
Sub load_dirs(LB As ListBox,
IO_dir As String)
Dim dir_ = IO.Directory.GetDirectories(IO_dir)
With LB.Items
.Clear()
.AddRange(dir_)
End With
End Sub
Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged
Try
Dim lb As ListBox = sender
load_files(ListBox1, lb.SelectedItem)
Catch ex As Exception
End Try
End Sub
End Class

Hittint Print button in print dialogue box programatically

I am trying to print local SSRS report through Winforms using following code.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.tblGoodsInTableAdapter.Fill(Me.dbPOsDataSet.tblGoodsIn)
Me.ReportViewer1.RefreshReport()
End Sub
Private Sub ThreadTask()
Thread.Sleep(100)
SendKeys.SendWait("{TAB}")
Thread.Sleep(10)
SendKeys.SendWait("{ENTER}")
End Sub
Private Sub ReportViewer1_RenderingComplete(ByVal sender As Object, ByVal e As Microsoft.Reporting.WinForms.RenderingCompleteEventArgs) Handles ReportViewer1.RenderingComplete
ReportViewer1.PrintDialog()
Dim thrd As Thread
thrd = New Thread(AddressOf ThreadTask)
thrd.IsBackground = True
thrd.Start()
End Sub
It works fine till viewing the print Dialogue box.
I need to hit the Print button of Print dialogue box programatically, so printing is done without user intervention.
Please advise how to achieve this objective.
Thanks
If you also don't want to show report preview this is an interesting walkthrough by Microsoft: Printing a Local Report without Preview.
It shows how to programmatically print a report without viewing it, using the LocalReport object. It's a completely different approach than your but I think this is the correct way to do it.

Drag & drop and get file path in VB.NET

I'd like to be able to drag a file/executable/shortcut into a Windows Forms application and have the application determine the original path of the dropped file then return it as a string.
E.g. drag an image from the desktop into the application and messagebox up the local path of the image.
Is that possible? Could someone provide me with an example maybe?
It's quite easy. Just enable drap-and-drop by setting the AllowDrop property to True and handle the DragEnter and DragDrop events.
In the DragEnter event handler, you can check if the data is of the type you want using the DataFormats class.
In the DragDrop event handler, use the Data property of the DragEventArgs to receive the actual data and the GetData method
Example:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.AllowDrop = True
End Sub
Private Sub Form1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
For Each path In files
MsgBox(path)
Next
End Sub
Private Sub Form1_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
This is just a note to state that if drag and drop does not work, it could be because you are running Visual Studio in Administrator Mode (Windows 7 and up I believe).
This also has to do with the UAC level currently set on your Windows installation.

Click Button Event in VB that displays a message about user input

I have a really quick and simple question, I’m learning programming in C# and VB and I need to create a GUI application in Windows Form with Visual Studio. This GUI will prompt the user to enter in an integer. I believe I have this part alright but then I need to have the user click a button that will convert the user's entry to an integer and display a message indicating whether the user was successful. I think I even have the conversion done correctly but I am having a problem displaying that message if the user was successful. Basically I need to know how to function the click method in VB that will allow this message to appear. Any help with this would be greatly appreciated. The following code is what I have already written for this project:
Public Class Form1
Private Sub EvaluateInput()
Dim InputValue As String
InputValue = ValueTextBox.Text
If IsNumeric(InputValue) Then
MessageBox.Show(InputValue & " is a number.")
Else
MessageBox.Show(InputValue & " is not a number.")
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click 'Continue Button
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Exit Button
Dim button As DialogResult
button = MessageBox.Show _
("Are you sure you want to exit this application?", _
"Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
If button = Windows.Forms.DialogResult.Yes Then
Me.Close()
Else
'Do Nothing
End If
End Sub
End Class
If I understand your question correctly, then you would need a simply change:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) _
Handles Button2.Click 'Continue Button
EvaluateInput()
End Sub
When you press Button2, it will call the EvaluateInput sub and display a message accordingly.

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?

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.