VB.Net 2012 Copy the file when a picture is dragged - vb.net

I have a picture icon on my form. I would like to have it so when the user drags this icon to a windows explorer, desktop etc the associated file is written to that location.
I have the file path in a combo and as I said an image on the form for the user to drag. The image is just a general image not really associated with the actual file.
Can you help me out? I have no clue!

For the next guy....
Where pbDragger is the picture box.
It is important to note that the DataObject requires a string array even if you are only dragging one file.
Private Sub pbDragger_MouseMove(sender As Object, e As MouseEventArgs) Handles pbDragger.MouseMove
If ((e.Button And MouseButtons.Left) = MouseButtons.Left) Then
Dim strPath As String = cboRef.SelectedItem!Path.ToString ' Path of the file to be copied
Dim strArr() As String = {strPath} ' 'FileDrop requires an array of string!!
Dim oDraginfo As New DataObject(DataFormats.FileDrop, strArr)
Dim dropEffect As DragDropEffects = pbDragger.DoDragDrop(oDraginfo, DragDropEffects.Copy)
End If
End Sub

Related

Is there a way to extract a variable from a subroutine in vb .net? or how do I declare custom event handlers and trigger them in a linked fashion?

I am trying to build this file copy utility in VB.Net, and I have this window:
The current folder button opens up a folder browser dialog, and I save the selected path from the dialog to a string variable which I then pass to a function. The function adds all files and directories present in that folder to the current folder listbox.
Now I need this filepath for the all files checkbox, which when triggered lists all the subdirectories and their contents in the currentfolder listbox.
Is there any way I can extract that filepath variable dynamically without hardcoding it? Or can I create custom event handlers and trigger them inside the current folder button handler?
Here is my code:
Public Class Form1
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FB As FolderBrowserDialog = New FolderBrowserDialog()
Dim srcpath As String
Dim flag As Integer = 1
FB.ShowDialog()
FB.ShowNewFolderButton = True
If (DialogResult.OK) Then
srcpath = FB.SelectedPath()
End If
listfiles(srcpath)
End Sub
Public Function listfiles(srcpath As String)
Dim dir As DirectoryInfo = New DirectoryInfo(srcpath)
Dim dirs As DirectoryInfo() = dir.GetDirectories()
Dim d As DirectoryInfo
Dim files As FileInfo() = dir.GetFiles()
Dim file As FileInfo
For Each file In files
CurrentFolderListBox.Items.Add(file.Name)
Next
For Each d In dirs
CurrentFolderListBox.Items.Add(d)
Next
'If CheckBox1.Checked = True Then
' CheckBox1_CheckedChanged(sender, New System.EventArgs())
'End If
End Function
Public Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChange
Dim item As DirectoryInfo
For Each i As DirectoryInfo In CurrentFolderListBox.Items
item = i
Next
End Sub
Any help would be most appreciated.
Well, for the list side lbox, and the right side lbox?
Why not fill each list box with a data source? You can have 1, or 2, or even 5 columns of data. The ListBox can display TWO of the columns.
So, VERY often a list box will have two values. the "value" based on what you select (often a PK database row value), and then you have the "text" value for display.
from FoxPro, ms-access, vb.net, and even asp.net?
A list box traditional has had the ability to "store" a set of values for your selection.
So, why not just put out the file list to a on the fly "data structure". You can quite much use a struct, a class or whatever.
However, might as well use a data table, since listbox supports "binding" to a table.
So, in the ListBox settings, you have these two settings:
so above is our "display"
And then set the "value" to the FULL file name like this:
So now we can say create a form like this:
So, our code to select the "from folder" can look like this:
Private Sub cmdSelFrom_Click(sender As Object, e As EventArgs) Handles cmdSelFrom.Click
Dim f As New FolderBrowserDialog
If f.ShowDialog = DialogResult.OK Then
txtFromFolder.Text = f.SelectedPath
ListBox1.DataSource = GetFileData(txtFromFolder.Text)
End If
End Sub
Public Function GetFileData(sFolder As String) As DataTable
Dim rstData As New DataTable
rstData.Columns.Add("FullFile", GetType(String))
rstData.Columns.Add("FileName", GetType(String))
' get all files from this folder
Dim folder As New DirectoryInfo(sFolder)
Dim fList() As FileInfo = folder.GetFiles
For Each MyFile As FileInfo In fList
Dim OneRow As DataRow = rstData.NewRow
OneRow("FullFile") = MyFile.FullName
OneRow("FileName") = MyFile.Name
rstData.Rows.Add(OneRow)
Next
Return rstData
End Function
so, we setup a two column "thing" (in this case a data table).
We fill it with our two values (FileName and FullFile).
So, we now have this:
I have selected two files on the left side, and thus we get this:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
For Each MySel As DataRowView In ListBox1.SelectedItems
Debug.Print(MySel.Item("FileName"))
Debug.Print(MySel.Item("FullFile"))
Next
End Sub
OutPut:
a.pdf
C:\Test2\a.pdf
b.pdf
C:\Test2\b.pdf
We could even include say file size in that table. But the "basic" concept here is that we can store + save data items into the list box, and that REALLY makes the code simple, since the list box now has JUST the file for display, but also enables us to have the full path name also.

Asp.Net, VB, SQL Server Reporting Services..dynamically generated reports from directory to be viewed on report viewer on click?

I have an aspx webform that in it's vb code reads files that are .rdl from a directory and then lists them as say a button or hyper link etc. "reports are on local host report server"
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim FileLocation As DirectoryInfo = _
New DirectoryInfo("C:\inetpub\wwwroot\Reports")
Dim fi As FileInfo() = FileLocation.GetFiles("*.rdl")
For Each name As FileInfo In fi
' Dim i As Integer
Dim listed As New LinkButton
' listed.Attributes("CssClass") = "a"
' listed.Attributes("Class") = "a"
listed.Attributes("id") = "listed"
listed.Text = (name.Name)
mine.Controls.Add(listed)
'mine.InnerHtml = ""
'i = i + 1
Next
End Sub
and another that has a reportviewer. When a report was clicked, how can I send the name/value to the viewer and redirect to it?
Thank you very much
EDIT: i got to make the click redirect to the viewer with a string and have the viewer use that string,but with how much ever variation ive tried it still errors that
The path of the item 'salesreport.rdl' is not valid. The full path must be less than 260 characters long; other restrictions apply. If the report server is in native mode, the path must start with slash. (rsInvalidItemPath)
this is the viewer code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
' Retrieve parameter from Route "Reports/{reportName}"
Dim reportName = Picks.Selecty
'Page.Title = reportName
ReportViewer1.ProcessingMode = ProcessingMode.Remote
Dim serverReport As ServerReport
ServerReport = ReportViewer1.ServerReport
serverReport.ReportServerUrl = New Uri("http://localhost/reportserver/")
serverReport.ReportPath = reportName
' reportName
'
' add parameters here
'Dim param As New ReportParameter("name", "value")
'serverReport.SetParameters(param)
serverReport.Refresh()
End If
End Sub
You could try the following.
Create a new page say ReportViewer.aspx which hosts your ReportViewer Control that recieves the name of the .RDL file as a query string.
In the First .aspx page, for every link you display, point the NavigateURL property to ReportViewer.aspx page and pass the name of the .RDL file as a QueryString.
Hope that helps
Thanks,
Prawin

Openfile on event

I am getting my feet wet with VB .Net programming (total novice). I have a DataGridView with amongst other information, a file path to where a particular document is stored. I have added a DataGridViewButtonColumn to the DataGridView, but I cannot figure out how to get the button to open the file.
Sorry, I have no code to provide as starting point for where I get stuck.
Thanks in advance,
Sorry I didn't read the post clear enough the first time, and didn't explain my code enough so it got deleted. This uses the contentclick event.
Dim Filetext As String = "" 'At start of the class to make it available to the whole class
Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim FilePathColumn As Integer = 0 'File path is in Column 0
Dim ButtonColumn As Integer = 1 'Column buttons are in
Dim RowClicked As Integer = e.RowIndex 'This gets the row that you clicked the button in
If e.ColumnIndex = ButtonColumn Then 'Make sure you clicked a button
Dim FILE_PATH As String = DataGridView1.Rows(RowClicked).Cells(FilePathColumn).ToString 'Get the path to the file
If System.IO.File.Exists(FILE_PATH) Then 'Make sure file exists
Filetext = System.IO.File.ReadAllText(FILE_PATH) 'Save file to a variable
'OR
Process.Start(FILE_PATH) 'To open it
End If
End If
End Sub
You can get rid of most of those lines but I wrote it like that to explain how it worked

Visual Basic Form. How to let users save text file where they want

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Dim CurrentDir As String = Environment.CurrentDirectory
Dim OutputFile2 As String = IO.Path.Combine(CurrentDir, "input.txt")
IO.File.WriteAllLines(OutputFile2, Result1.Lines)
End Sub
Right now, I have coding that saves a text file in the current directory. However, I want to have a browse button for users so that they can pick where this text file is saved. How do I proceed this?
I was trying it by my self and I'm having a trouble with using save file dialog. If you can teach me how to use a save file dialog or anyway to write save browse button, I would very appreciate it!
The documentation for the SaveFileDialog object contains an example.
Here is a tutorial on how to implement SaveFileDialog using Toolbox in Visual Studio like you mentioned. The code sample is in C# but it can be easily converted to VB.
Link: www.dotnetperls.com/savefiledialog
Private Sub button1_Click(sender As Object, e As EventArgs)
' When user clicks button, show the dialog.
saveFileDialog1.ShowDialog()
End Sub
Private Sub saveFileDialog1_FileOk(sender As Object, e As CancelEventArgs)
' Get file name.
Dim name As String = saveFileDialog1.FileName
' Write to the file name selected.
' ... You can write the text from a TextBox instead of a string literal.
File.WriteAllText(name, "test")
End Sub

Items icons with listview and imagelist

Hello I've got a list view that opens items inside a folder and displays them. I want to know if there is a way to have the list view display the icons aswell, maybe using shell32 or an imagelist. Here's the code:
Imports System.IO
Imports System.Xml
Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic
Public Class cv7import
Private Sub cv7import_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim caminho As String
caminho = "C:\Documents and Settings\Software\Ambiente de trabalho\1"
lstvicon.View = View.Details
lstvicon.GridLines = False
lstvicon.FullRowSelect = True
lstvicon.HideSelection = False
lstvicon.MultiSelect = True
lstvicon.Columns.Add("Nome")
lstvicon.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
Dim DI As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(caminho)
Dim files() As System.IO.FileInfo = DI.GetFiles
Dim file As System.IO.FileInfo
Dim li As ListViewItem
For Each file In files
li = lstvicon.Items.Add(file.Name)
li.Tag = file.FullName
Next
End Sub
End Class
Here's two images, of how it looks and how I want it to look, if it helps.
How I wanted: http://imageshack.us/photo/my-images/21/wantd.png/
How it looks: http://imageshack.us/photo/my-images/13/needk.png/
Well you need to find the extension of the file to start with:
Dim file As String = "C:\scratch\newfile.txt"
Dim ext as string = IO.Path.GetExtension(file)
Then you need to find this entry in the HKEY_CLASSES_ROOT section of the registry:
HKEY_CLASSES_ROOT\.txt
The default value in this key gives the file type associated with this file in my case txtfile (remember that different registries may have different values depending on what the user has set up and what programs are installed)
You then need to look up this values DefaultIcon key in HKEY_CLASSES_ROOT:
HKEY_CLASSES_ROOT\txtfile\DefaultIcon
The default value in here gives you the location of the icon and the icon number in my case:
%SystemRoot%\system32\imageres.dll,-102
From this point I think you may need to rely on the ExtractIconEX API
to extract the icon. This link may also be of use
ListViewItems have ImageList, ImageIndex and ImageKey properties. Add a ImageList control to your form (from the components group in the toolbox). You can add images to the ImageList by clicking "Choose images" below the property grid, when the image list is selected. Then assign the image list and an image index or an image key to your list items.