Vb.net - FolderBrowserDialog - vb.net

I am having some troubles with FolderBrowserDialog
I've tried all the post I could find here and I'm almost there in terms of what I want.
following is my code:
Private Sub ButtonBrowseOutput_Click(sender As Object, e As EventArgs) Handles ButtonBrowseOutput.Click
Dim dialog = New FolderBrowserDialog()
dialog.SelectedPath = Application.StartupPath
If DialogResult.OK = dialog.ShowDialog() Then
TextBoxShowOutput.Text = dialog.ToString & "/helloforum" & ".txt"
End If
End Sub
would give me something like this:
System.Windows.Forms.FolderBrowserDialog/helloforum.txt
Where I want it to give it for example:
c:/users/sexyname/desktop/helloforum.txt

TextBoxShowOutput.Text = dialog.ToString & "/helloforum" & ".txt"
Must be:
TextBoxShowOutput.Text = dialog.SelectedPath & "/helloforum" & ".txt"

SelectedPath - Gets or sets the path selected by the user.
dialog.SelectedPath & "/helloforum.txt"

Just for your knowledge
Private Sub AbsolutePathOfDialogBoxes()
Dim dlgFolder = New FolderBrowserDialog
Dim dlgOpenFile = New OpenFileDialog
Dim dlgSaveFile = New SaveFileDialog
Dim absolutePath As String
'/*-----------------------------------*/'
absolutePath = dlgFolder.SelectedPath
absolutePath = dlgOpenFile.FileName
absolutePath = dlgSaveFile.FileName
'/*-----------------------------------*/'
End Sub
Happy Coding

Related

Create folder in a specified location with strings

I´ve been working in a code that allow user to create a folder in a location specified with folder browser and name it with two text strings, but at this far, I've managed to create the folder on the default location.
How can I set the location with the folder browser and have the new folder name with these texts as default.
The code this far is.
For create folder button:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim snombrecarpeta As String
Dim sruta As Object
snombrecarpeta = "QUO " & (quo.Text) & "_" & (proy.Text)
sruta = ccliente()
My.Computer.FileSystem.CreateDirectory(snombrecarpeta)
MsgBox("Se ha creado la carpeta del proyecto")
End Sub
For search folder button:
Private Sub ccliente_Click(sender As Object, e As EventArgs) Handles ccliente.Click
Dim ccliente = New FolderBrowserDialog()
ccliente.SelectedPath = ("E:\Crear_carpetas\Crear_carpetas")
If DialogResult.OK = ccliente.ShowDialog() Then
End If
Thanks in forward.
And yes, I´m just starting in the vb world.
Creation of folder on Desktop, see Environment.GetFolderPath, and IO.Directory.CreateDirectory
Dim snombrecarpeta As String
Dim sruta As Object
snombrecarpeta = "QUO " & (quo.Text) & "_" & (proy.Text)
sruta = ccliente()
'
' e.g. create directory on Desktop
'
snombrecarpeta = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), snombrecarpeta)
Try
IO.Directory.CreateDirectory(snombrecarpeta)
MessageBox.Show("Se ha creado la carpeta del proyecto")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
edit:
'
' https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.folderbrowserdialog?view=net-5.0
'
Dim path As String = ""
Dim snombrecarpeta As String
snombrecarpeta = String.Format("QUO{0}_{1}", (quo.Text), (proy.Text))
Using fbd As New FolderBrowserDialog
fbd.RootFolder = Environment.SpecialFolder.Desktop
If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
path = IO.Path.Combine(fbd.SelectedPath, snombrecarpeta)
End If
End Using

vb.net change combobox options depending on selected item in 2 previous comboboxes

I am creating a program which someone can input a search into a textbox and then narrow down the results using a series of comboboxes (or just use the comboboxes to search through everything).
The program looks like this: form 1
I have made the options in the 2nd combobox change using the following code:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim type As String = ComboBox1.SelectedItem
Dim make As String = ComboBox2.SelectedItem
Dim model As String = ComboBox3.SelectedItem
Dim version As String = TextBox2.Text
Dim memory As String = TextBox3.Text
Dim problem As String = TextBox4.Text
Dim casenumber As Integer = Int(Rnd() * 9999) + 1000
If type = "Phone" Then
ComboBox2.Items.Clear()
Dim file As New System.IO.StreamReader("E: \phone.txt")
For i = 1 To 20
q(i) = file.ReadLine() & vbNewLine
ComboBox2.Items.Add(q(i))
Next
ElseIf type = "Tablet" Then
ComboBox2.Items.Clear()
Dim file As New System.IO.StreamReader("E:\tablet.txt")
For i = 1 To 20
q(i) = file.ReadLine() & vbNewLine
ComboBox2.Items.Add(q(i))
Next
ElseIf type = "Desktop computer" Then
ComboBox2.Items.Clear()
Dim file As New System.IO.StreamReader("E:\pc.txt")
For i = 1 To 20
q(i) = file.ReadLine() & vbNewLine
ComboBox2.Items.Add(q(i))
Next
ElseIf type = "Laptop" Then
ComboBox2.Items.Clear()
Dim file As New System.IO.StreamReader("E:\laptop.txt")
For i = 1 To 20
q(i) = file.ReadLine() & vbNewLine
ComboBox2.Items.Add(q(i))
Next
'Else
'Dim objwriter As System.IO.StreamWriter
'objwriter = My.Computer.FileSystem.OpenTextFileWriter("E:\unknown.txt", True)
'File.AppendText("type:" And ComboBox1.Text And "make" & ComboBox2.Text And "model: " & ComboBox3.Text And "version: " And TextBox2.Text & "memory" And TextBox3.Text)
End If
End Sub
However,the code won't work to change what is in the 3rd box. I have repeated the following code for each option:
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
Dim type As String = ComboBox1.SelectedItem
Dim make As String = ComboBox2.SelectedItem
Dim model As String = ComboBox3.SelectedItem
Dim version As String = TextBox2.Text
Dim memory As String = TextBox3.Text
Dim problem As String = TextBox4.Text
If type = "Phone" Then
If make = "apple" Then
ComboBox2.Items.Clear()
Dim file As New System.IO.StreamReader("E:\apple.txt")
For i = 1 To 20
q(i) = file.ReadLine() & vbNewLine
ComboBox3.Items.Add(q(i))
Next
ElseIf make = "samsung" Then
ComboBox2.Items.Clear()
Dim file As New System.IO.StreamReader("E:\samsung.txt")
For i = 1 To 20
q(i) = file.ReadLine() & vbNewLine
ComboBox3.Items.Add(q(i))
Next
ElseIf make = "htc" Then
ComboBox2.Items.Clear()
Dim file As New System.IO.StreamReader("E:\htc.txt")
For i = 1 To 20
q(i) = file.ReadLine() & vbNewLine
ComboBox3.Items.Add(q(i))
Next
ElseIf make = "Nokia" Then
ComboBox2.Items.Clear()
Dim file As New System.IO.StreamReader("E:\Nokia.txt")
For i = 1 To 20
q(i) = file.ReadLine() & vbNewLine
ComboBox3.Items.Add(q(i))
Next
ElseIf make = "Blackberry" Then
ComboBox2.Items.Clear()
Dim file As New System.IO.StreamReader("E:\blackberry.txt")
For i = 1 To 20
q(i) = file.ReadLine() & vbNewLine
ComboBox3.Items.Add(q(i))
Next
I have checked the obvious problems like putting the wrong text file names and capital letters etc, but can't get this to work whatever I do.
Does anyone know why the third combobox remains blank even when both conditions are met (both combobox1 and 2 have the right thing selected)? Any suggestion would be much appreciated.
Firstly I suspect that this code :-
Dim type As String = ComboBox1.SelectedItem.ToString
Dim make As String = ComboBox2.SelectedItem.ToString
Dim model As String = ComboBox3.SelectedItem.ToString
Dim version As String = TextBox2.Text
Dim memory As String = TextBox3.Text
Dim problem As String = TextBox4.Text
shouldn't be in each if your events. They should really be placed somewhere that executes after all the information has been chosen yes?
OK for a start paste this into a text file called "device type.txt"
Phone,E:\phone.txt
Tablet,E:\tablet.txt
Desktop Computer,E:\pc.txt
Laptop,E:\laptop.txt
Then edit your phones.txt file and the rest of the above files to match that format e.g this phone.txt file
Apple,E:\apple.txt
Samsung,E:\samsung.txt
Htc,E:\htc.txt
Nokia,E\nokia.txt
Blackberry,E:\blackberry.txt
and so on for each item that links to another file. For the last files in the tree - which I presume are the files containing a list of the models for each brand of phone, just leave them as a simple list. No commas or anything after each model.
Use this code to do the populating of each ComboBox
Private Sub PopulateComboBox(ByRef cboBox As ComboBox, ByVal itemSource As String)
RemoveHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
RemoveHandler ComboBox2.SelectedIndexChanged, AddressOf ComboBox2_SelectedIndexChanged
RemoveHandler ComboBox3.SelectedIndexChanged, AddressOf ComboBox3_SelectedIndexChanged
Dim devices As New List(Of item)
Dim csvFlag As Boolean = False
cboBox.Items.Clear()
Using MyReader As New Microsoft.VisualBasic.
FileIO.TextFieldParser(itemSource)
If MyReader.ReadLine.Contains(",") Then csvFlag = True
End Using
Using MyReader As New Microsoft.VisualBasic.
FileIO.TextFieldParser(itemSource)
If csvFlag Then
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
End If
Dim currentRow As String() = {"", ""}
While Not MyReader.EndOfData
Try
If csvFlag Then
currentRow = MyReader.ReadFields()
Dim tempItem As New item
tempItem.item = currentRow(0)
tempItem.fileName = currentRow(1)
devices.Add(tempItem)
Else
currentRow(0) = MyReader.ReadLine
Dim tempItem As New item
tempItem.item = currentRow(0)
tempItem.fileName = ""
devices.Add(tempItem)
End If
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
End Try
End While
End Using
If csvFlag Then
cboBox.DataSource = devices
cboBox.DisplayMember = "item"
cboBox.ValueMember = "fileName"
Else
cboBox.DataSource = devices
cboBox.DisplayMember = "item"
cboBox.ValueMember = "item"
End If
AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
AddHandler ComboBox2.SelectedIndexChanged, AddressOf ComboBox2_SelectedIndexChanged
AddHandler ComboBox3.SelectedIndexChanged, AddressOf ComboBox3_SelectedIndexChanged
End Sub
What it does is firstly check to see if the file being read is a comma-delimited file.
If it is delimited, it will read the file referred to in itemSource and expect a pair of values. The first value is what you see in the box(the DisplayMember), and the second value is what clicking on the box actually returns(the ValueMember)
If the file isn't comma-delimited, it will just read each line and add it to the combobox so that it acts normally (this will be important for the last files in the tree)
Next you need these methods for the ComboBoxes
Private Sub PopulateComboBox1()
PopulateComboBox(ComboBox1, "E:\device type.txt")
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
PopulateComboBox(ComboBox2, ComboBox1.SelectedValue.ToString)
End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
PopulateComboBox(ComboBox3, ComboBox2.SelectedValue.ToString)
End Sub
Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
PopulateComboBox(ComboBox3, ComboBox2.SelectedValue.ToString)
End Sub
Call the method to populate Combobox1 possibly in the form's load event.

prompt for opening a save dialog for excel file using vb.net

Iam using visual studio 2012,
i would like to open "save dialog" to choose where to save my file instead of using fixed path,
the following code is a sample of what i would like to use it in:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim xlsWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlsWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim xls As New Microsoft.Office.Interop.Excel.Application
Dim resourcesFolder = IO.Path.GetFullPath(Application.StartupPath & "\Resources\")
Dim fileName = "book1.xlsx"
xlsWorkBook = xls.Workbooks.Open(resourcesFolder & fileName)
xlsWorkSheet = xlsWorkBook.Sheets("a")
xlsWorkSheet.Cells(1, 1) = TextBox1.Text
xlsWorkBook.SaveAs("C:\output\book1.xlsx")
xlsWorkBook.Close()
xls.Quit()
End Sub
i would like to change this path "C:\output\book1.xlsx" to save dialog, so i can choose where to save it manually.
thanks alot..
Like this, don't forget to dispose of com objects with the Marshal class like I added.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim xls As New Microsoft.Office.Interop.Excel.Application
Dim xlsWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlsWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim resourcesFolder = IO.Path.GetFullPath(Application.StartupPath & "\Resources\")
Dim fileName = "book1.xlsx"
xlsWorkBook = xls.Workbooks.Open(resourcesFolder & fileName)
xlsWorkSheet = xlsWorkBook.Sheets("a")
xlsWorkSheet.Cells(1, 1) = TextBox1.Text
Using sfd As New SaveFileDialog
If sfd.ShowDialog() = DialogResult.OK Then
xlsWorkBook.SaveAs(sfd.FileName)
MessageBox.Show(sfd.Filename)
End If
End Using
xlsWorkBook.Close()
xls.Quit()
Marshal.FinalReleaseComObject(xlsWorkSheet)
Marshal.FinalReleaseComObject(xlsWorkBook)
Marshal.FinalReleaseComObject(xls)
End Sub
A little more comprehensive way to open the Save As Dialog than OneFineDay's answer (using the same method).
This opens the Save As dialog using a designated directory, filename, extension type, and window title; it also prompts before overwritting any existing files.
Dim dir as String = "C:\output\"
Dim fName As String = "Book1"
Using sfd As New SaveFileDialog
sfd.InitialDirectory = dir
sfd.Title = "Save As"
sfd.OverwritePrompt = True
sfd.FileName = fName
sfd.DefaultExt = ".xlsx"
sfd.Filter = "Excel Workbook(*.xlsx)|"
sfd.AddExtension = True
If sfd.ShowDialog() = DialogResult.OK Then
xlsWorkBook.SaveAs(sfd.FileName)
End If
End Using
Add an openfiledialog to your form and then ...
With OpenFileDialog1
.Title = " whatever"
.InitialDirectory = "c:\"
.Multiselect = False
If .ShowDialog() = DialogResult.OK Then
xlsWorkBook.SaveAs(.FileName)
End If
End With
You can use this :
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim xlsWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlsWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim xls As New Microsoft.Office.Interop.Excel.Application
Dim resourcesFolder = IO.Path.GetFullPath(Application.StartupPath & "\Resources\")
Dim fileName = "book1.xlsx"
xlsWorkBook = xls.Workbooks.Open(resourcesFolder & fileName)
xlsWorkSheet = xlsWorkBook.Sheets("a")
xlsWorkSheet.Cells(1, 1) = TextBox1.Text
xlsWorkBook.SaveAs("C:\output\book1.xlsx")
xls.Application.DisplayAlerts = False
xlsWorkBook.Close()
xls.Quit()
End Sub

Print Preview not displaying file

I am working on a menu strip Print Preview event. i worked out the following procedure to view a pdf file located in the same path as my workbook.
Private WithEvents docPrint As New PrintDocument()
' Declare a string to hold the entire document contents.
Private documentContents As String
' Declare a variable to hold the portion of the document that
' is not printed.
Private stringToPrint As String
Private Sub ReadDocument()
Dim xlWBPath As String = Globals.ThisWorkbook.Application.ActiveWorkbook.Path
Dim docName As String = xlWBPath & "\" & "CustomRanges.pdf"
docPrint.DocumentName = docName
Dim stream As New FileStream(docName, FileMode.Open)
Try
Dim reader As New StreamReader(stream)
Try
documentContents = reader.ReadToEnd()
Finally
reader.Dispose()
End Try
Finally
stream.Dispose()
End Try
stringToPrint = documentContents
End Sub
Private Sub prnPrvStripCustomRange_Click(sender As Object, e As EventArgs) Handles prnPrvStripCustomRanges.Click
ReadDocument()
prnPrvCustomRanges.Document = docPrint
prnPrvCustomRanges.ShowDialog()
End Sub
The dialog pops up on click, but my document page is blank. I have another event for printing and that one prints the same document. So I don't understand what I am doing wrong on my preview.
Here is my print procedure:
Private Sub prnCustonPages_Click(sender As Object, e As EventArgs) Handles prnCustonPages.Click
Dim xlWBPath As String = Globals.ThisWorkbook.Application.ActiveWorkbook.Path
Dim docName As String = xlWBPath & "\" & "CustomRanges.pdf"
docPrint.DocumentName = docName
prnPrtCustomRanges.Document = docPrint
prnPrtCustomRanges.PrinterSettings = docPrint.PrinterSettings
prnPrtCustomRanges.AllowSomePages = True
If prnPrtCustomRanges.ShowDialog = DialogResult.OK Then
docPrint.PrinterSettings = prnPrtCustomRanges.PrinterSettings
docPrint.Print()
End If
End Sub

How to add else statement into downloading code?

I'm making a file downloader and I want it to download files to either my selected directoy or default directory.
Downloading code is here:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Button1.Enabled = False
Button1.Text = "Updating..."
WebBrowser1.Visible = True
Dim uri As System.Uri = New System.Uri("http://199.91.154.170/e9f6poiwfocg/pei02c8727sa720/Ultra+v08.zip")
Dim webclient As System.Net.WebClient = New System.Net.WebClient()
Dim path As String =
If apppath = Nothing then
New String(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "test\\Test.zip"))
Else New String
(apppath)
End if
Dim fileInfo As System.IO.FileInfo = New System.IO.FileInfo(path)
If Not System.IO.Directory.Exists(fileInfo.Directory.FullName) Then
System.IO.Directory.CreateDirectory(fileInfo.Directory.FullName)
End If
AddHandler webclient.DownloadFileCompleted, AddressOf webclient_DownloadDataCompleted
webclient.DownloadFileAsync(uri, path)
End Sub
Private Sub
And user selected path apppath is defined here:
If apppath = "" Then
Dim dialog As New FolderBrowserDialog()
dialog.RootFolder = Environment.SpecialFolder.Desktop
dialog.SelectedPath = Path.Combine(Environment.GetFolderPath( _
Environment.SpecialFolder.ApplicationData))
dialog.Description = "Select directory where to install the files"
If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
apppath = dialog.SelectedPath
End If
My.Computer.FileSystem.WriteAllText(apppath & " apppath.txt", apppath, False)
End If
How to fix Dim path As String else statement?
Thanks in advance!
If I am understanding your question correctly then I believe there is a simple solution, which is this:
Dim path As String
If apppath = Nothing then
path = New String(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "test\Test.zip"))
Else New String
path = apppath
End if
If I have not understood your requirements correctly, then please provide more information.