Save file path on show dialog - vb.net

I want to save path on text box after Directory 1: whenever I try to close the application and open again the path is not there.I want it to show every time I open the application.
Private Sub btnRootBrowse1_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowse1.Click
RootFolderBrowserDialog.ShowDialog()
txtPath1.Text = RootFolderBrowserDialog.SelectedPath
End Sub

You need save your data (in this case txtPath1.Text) to Hard-Drive (File or DataBase) and reload it in the next execution.
It can be easy when you use Application-Setting:
In the Solution Explorer, Double-Click "My Project"
in left pane click "Setting"
in the table, Fill in the fields as needed, example for you case: Name: DirectoryLocation Type: String Scope: User Value: Empty.
Sample Code Using:
Public Sub New()
InitializeComponent()
'load from the hard-disk
txtPath1.Text = My.Settings.DirectoryLocation
End Sub
Private Sub btnRootBrowse1_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowse1.Click
RootFolderBrowserDialog.ShowDialog()
txtPath1.Text = RootFolderBrowserDialog.SelectedPath
'save to hard-disk
My.Settings.DirectoryLocation = txtPath1.Text
My.Settings.Save()
End Sub

Related

Simple application with opening webpage in VB

I would like to create simple exe app in VB, which will open default browser with specified page. I have this code
Public Class Form1
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
System.Diagnostics.Process.Start("www.google.com")
End Sub End Class
If I click on the button I get error message:
System.ComponentModel.Win32Exception: System cannot find this file
Could someone help me where is the problem? Or is there any other approach how to open webpage?
"System cannot find this file" means that your system is not reading the URL as one and it's trying to get the resource at the local path: "www.google.com" which obviously doesn't exist. Check this link because the issue seems to be the same.
Here in an application I call Best Links is the process I use to view URL's.
It works by storing website URL links in a SQLite Database (DB).
The DB also stores the Site Name and the Last Visit Date.To view the site the user just clicks on the DB entry displayed in DataGridView. Screen Shot Below. And the code that opens the link in what ever your default browser is.
Private Sub dgvLinks_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvLinks.CellClick
selRow = e.RowIndex
If e.RowIndex = -1 Then
gvalertType = "4"
frmAlert.ShowDialog()
Exit Sub
End If
Dim row As DataGridViewRow = Me.dgvLinks.Rows(e.RowIndex)
If row.Cells(2).Value Is Nothing Then
gvalertType = "5"
frmAlert.ShowDialog()
Return
Exit Sub
ElseIf gvTxType = "View" Then
webPAGE = row.Cells(2).Value.ToString()
siteID = CInt(row.Cells(0).Value.ToString())
UpdateSiteData()
Process.Start(webPAGE)
Data Grid View
If you would like the complete code or and exe file let me know and I will add it to my GitHub repository code will be a ZIP file.

How to save a Forms Background Image using My.Settings.Save Visual Basic

I'm trying to make an Operating System in Visual Basic (program based of course) and it needs personalisation.
I want the user to be able to choose from a select group of images, stored in the Resources of the project, and I want that image to be saved, so that the next time they log on to the software, the form has the same image they selected saved.
Extra Information:
The image selection is on a seperate form. Using:
If ComboBox1.Text = "Beach Fade" Then
PictureBox1.BackgroundImage = My.Resources.beach_fade
End If
Main Desktop form uses the "Background image" to have the image selected.
Use My.Settings to persist user settings.
This is the code I used to demo it. I have a form with ComboBox1 and PictureBox1. With this code, you can have your image selection persist.
Go into your project properties and click the Settings option on the left. Create a setting called BackgroundImageName of type String. You can choose if the scope is saved per-user or per-application.
Then in project properties go to Resources and add two images named "beach_fade" and "mountain_fade". You know how to do this
Then paste this code
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.ComboBox1.Items.Add("Beach Fade")
Me.ComboBox1.Items.Add("Mountain Fade")
Me.ComboBox1.Text = My.Settings.BackgroundImageName
setBackgroundImage()
End Sub
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
My.Settings.BackgroundImageName = Me.ComboBox1.Text
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
setBackgroundImage()
End Sub
Private Sub setBackgroundImage()
If ComboBox1.Text = "Beach Fade" Then
PictureBox1.BackgroundImage = My.Resources.beach_fade
ElseIf ComboBox1.Text = "Mountain Fade" Then
PictureBox1.BackgroundImage = My.Resources.mountain_fade
End If
End Sub
End Class
The application will start up every time with the image selected in the ComboBox before last close.

Is there a way to get the input from a Load event handler (entered via inputBox) to be used Globally in the program?

I am creating a form that will prompt the user to enter a file name upon loading the file. The issue I encounter is that my variable I use to store the input is not recognized in another one of my procedures. The code here shows my current set up.
Imports System.IO
Public Class frmEmployee
Sub frmEmployee_load(ByVal sender As Object, e As System.EventArgs)
Dim strFileName = InputBox("Please name the file you would like to save the data to: ")
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtEmail.Clear()
txtExtension.Clear()
txtFirst.Clear()
txtLast.Clear()
txtMiddle.Clear()
txtNumber.Clear()
txtPhone.Clear()
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim inputFile As New StreamWriter(strFileName)
If File.Exists(strFileName) = True Then
inputFile = File.CreateText(strFileName)
inputFile.Write(txtEmail.Text)
inputFile.Write(txtExtension.Text)
inputFile.Write(txtFirst.Text)
inputFile.Write(txtLast.Text)
inputFile.Write(txtMiddle.Text)
inputFile.Write(txtNumber.Text)
inputFile.Write(txtPhone.Text)
inputFile.Write(cmbDepart.Text)
Else
MessageBox.Show("" & strFileName & "Cannot be created or found.")
End If
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
The load event handler is where I want the user to input the name of the file.
Change the scope of your variable to outside of your load method...
Public Class frmEmployee
Private strFileName As String
Sub frmEmployee_load(ByVal sender As Object, e As System.EventArgs)
strFileName = InputBox("Please name the file you would like to save the data to: ")
End Sub
You could use My.Settings and load the file string there and save it. All forms can now access that. When you close the the app set it to String.Empty if you want it to be.
You also could be taking advantage of a class object for the fields you are capturing then using either BinaryFormatter or XmlSerializer to store the data. Better databinding, creation and reconstruction using an object.
My.Settings.Filename = Inputbox("Filename?")
My.Settings.Save()
The problem with the approach you have there is that your variabl strFileName is scoped to the class frmEmployee.
You need to set up a genuinely global variable (at the application startup) an then use that. So you will need a Sub Main as an entry point to your application See here, and just before that create a public variable to hole the file name.
So you might have something like this for your application startup:
Public fileNametoUse as string
Public Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New Form1)
End Sub
Then in you load sub for frmEmployee you would have:
fileNametoUse = InputBox("Please name the file you would like to save the data to: ")

Streamwriter will not save to FileName entered by User with Default var.FileName initiated - VB.NET

I am new here, and relatively new to VB.NET. I have a specific problem with getting my StreamWriter to work properly. One of the requirements of my project is to give the file a default name when the user clicks the Save button, which I have done by setting mysave.Filename = "MyLog.log". I call a new instance of my streamwriter trying to save to the filename specified by the user (mySave.Filename again), but every time, it saves to the default MyLog.log file. I have pasted my code below.
If someone could tell me how I can make sure the data is being saved to the value entered by the user for File name, that would be greatly beneficial. Also, I apologize for the code format, its not perfectly readable, but I'm trying to learn how to use the 4 space indents to my advantage!!
Thanks!
Imports System.IO
Public Class Form1
Dim Initial As String = "C:\Users\Brian Frick\Documents\Visual Studio 2010\Projects\HW5_Frick_Creator\HW5_Frick_Creator\bin\Debug" 'give variable for full path without log file
Dim Fullpath As String = "C:\Users\Brian Frick\Documents\Visual Studio 2010\Projects\HW5_Frick_Creator\HW5_Frick_Creator\bin\Debug\MyLog.log" 'give one path for full path
Dim filewriter As New StreamWriter(Fullpath, True)
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit() ' quit application
filewriter.Close()
End Sub
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
Dim mySave As New SaveFileDialog
mySave.Filter = "LOG File (*.log)|*.log" 'set filter to .log
mySave.DefaultExt = "log" 'set default extension
mySave.InitialDirectory = Initial 'set default directory
mySave.FileName = "MyLog.log"
mySave.OverwritePrompt = True ' make sure to ask user if they want to over write
If mySave.ShowDialog() = DialogResult.OK Then
Dim filewriter As New StreamWriter(mySave.FileName, True)
filewriter.Close()
'filewriter = New StreamWriter(mySave.FileName, True)
'filewriter.Close() 'close filewriter to allow access to write directory
'Dim Stream As New StreamWriter(mySave.FileName, True) 'save file to path chosen by user in SaveDialog
'Stream.Close() ' Close stream to allow access to directory
' filewriter.Close()
Else
'dialog cancelled - no action
End If
filewriter.Close()
filewriter = New StreamWriter(Fullpath, True) 're initiate filewriter to be used in successive iterations through the program without error
End Sub
Private Sub SavingsDepositBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SavingsDepositBtn.Click
'write to file for SavDep
filewriter.WriteLine("SavDep")
filewriter.WriteLine(AccountBox.Text)
filewriter.WriteLine(AmountBox.Text)
End Sub
Private Sub SavingsWithdrawBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SavingsWithdrawBtn.Click
'write to file for SavWith
filewriter.WriteLine("SavWith")
filewriter.WriteLine(AccountBox.Text)
filewriter.WriteLine(AmountBox.Text)
End Sub
Private Sub CheckDepsotBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckDepsotBtn.Click
'write to file for CheckDep
filewriter.WriteLine("CheckDep")
filewriter.WriteLine(AccountBox.Text)
filewriter.WriteLine(AmountBox.Text)
End Sub
Private Sub CheckWithdrawBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckWithdrawBtn.Click
'write to file for CheckWith
filewriter.WriteLine("CheckWith")
filewriter.WriteLine(AccountBox.Text)
filewriter.WriteLine(AmountBox.Text)
End Sub
End Class
the general way you are using the filewriter won't work, and there are some other serious issues like that there are two 'FileWriter' variables with overlapping scope.
for your save routine, you can't just re-instantiate your filewriter to point it to a different path. that will destroy all the data that had been in the underlying stream, so you are just saving nothing.
try caching the data in memory while the program is running, and don't create the stream until you are ready to write all the data to it, and it to a file, all in one go. your current approach makes it very hard to prevent leaking handles to the log files, so if the app crashes the user will have to reboot to release the write lock before they can run it again. look into the 'using' construct and 'Try... Finally'

VB.NET - Choose folder by browsing and copy file from application directory

Hello StackOverflow users. I am trying to create an application where you can browse to a folder, press install button and it will copy some files to the directory of your choosing?
I found some example code but
how do i go on with my code from here? Cant figure out how to copy the files. You can see at last in the code i tried to copy files but its not really working, how do i use the function? I want the files to come from the application directory. And copy to the browsed folder.
Public Class Installer
Private Sub Installer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub LinkLabel1_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
End Sub
Private Sub BrowseButton_Click_1(sender As System.Object, e As System.EventArgs) Handles BrowseButton.Click
' Declare a variable named theFolderBrowser of type FolderBrowserDialog.
Dim theFolderBrowser As New FolderBrowserDialog
' Set theFolderBrowser object's Description property to
' give the user instructions.
theFolderBrowser.Description = "Please browse to your GTAIV directory."
' Set theFolderBrowser object's ShowNewFolder property to false when
' the a FolderBrowserDialog is to be used only for selecting an existing folder.
theFolderBrowser.ShowNewFolderButton = False
' Optionally set the RootFolder and SelectedPath properties to
' control which folder will be selected when browsing begings
' and to make it the selected folder.
' For this example start browsing in the Desktop folder.
theFolderBrowser.RootFolder = System.Environment.SpecialFolder.Desktop
' Default theFolderBrowserDialog object's SelectedPath property to the path to the Desktop folder.
theFolderBrowser.SelectedPath = My.Computer.FileSystem.SpecialDirectories.Desktop
' If the user clicks theFolderBrowser's OK button..
If theFolderBrowser.ShowDialog = Windows.Forms.DialogResult.OK Then
' Set the FolderChoiceTextBox's Text to theFolderBrowserDialog's
' SelectedPath property.
Me.FolderTextBox.Text = theFolderBrowser.SelectedPath
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles InstallButton.Click
My.Computer.FileSystem.CopyFile(My.Application.Info.DirectoryPath, theFolderBrowser.SelectedPath)
End Sub
End Class
SnoX
When you call this line of code
My.Computer.FileSystem.CopyFile(My.Application.Info.DirectoryPath, _
theFolderBrowser.SelectedPath)
the theFolderBrowser object cannot be referenced because it was a local variable inside another method. However, before exiting, you have copied the selected path into a textbox. You could use that textbox as destination of your copy
My.Computer.FileSystem.CopyDirectory(My.Application.Info.DirectoryPath, _
Me.FolderTextBox.Text )
And also keep in mind that CopyFile copies only one file, if you intend to copy an entire folder you need the CopyDirectory method. There is another important detail in this method:
If your files exist in the destination directory and you don't use the overload with the overwrite flag you will get an exception
My.Computer.FileSystem.CopyDirectory(My.Application.Info.DirectoryPath, _
Me.FolderTextBox.Text, True )