How to check if a folder exists automatically without ".click" function - vb.net

I have a problem. I have a program written in Visual Basic using Visual Studio 2013 and it works good. The problem is that I want the program to check if a folder exists on program start-up and return a text value in a label without having to manually "click" the label. I have searched and cannot find anything, maybe I'm not searching for the right thing?
Here is a sample of the check:
''//ASDG_JR check
Private Sub lbl_asdg_jr_pres_Click(sender As Object, e As EventArgs) Handles lbl_asdg_jr_pres.Click
If My.Computer.FileSystem.DirectoryExists(user_txt_dir.Text & "\#ASDG_JR_v0.14") Then
lbl_asdg_jr_pres.Text = "All good!"
btn_asdg_di.Text = ".zip file downloaded already"
btn_asdg_unzip.Text = "unzipped and installed already"
btn_asdg_di.Enabled = False
btn_asdg_unzip.Enabled = False
Else
lbl_asdg_jr_pres.Text = "Use buttons ----->"
btn_asdg_di.Enabled = True
btn_asdg_unzip.Enabled = True
End If
End Sub

Place your code inside the form's load event.
Private Sub myForm_Load(sender As System.Object, e As System.EventArgs) Handles myForm.Load
'If directoryExists Then update label
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.

vb.net textbox not displaying text or colors

I have many applications that reliably display data and colors in text boxes. I copied one working program to make a new application. There are about 10 text boxes that I use for data displays. Some are working - and some are not. Here are 2 samples - the Switch code works in many programs - but here it will put the word "ON" in the box but no color, and then when the switch is off - no change to the textbox. The code seems not to care between the .AppendText or the .text = variable control. I have tried changing the properties of the text box to public but no luck. I have tried adding the .show object, and verified the .visible attribute in the properties view. A similar problem is with the DistanceUnitsOut and the NextImageDistanceOut - I can step through the code and see the values, but the displayed data does not change! I have tried changing the definition of the display to public - no joy!
SwitchStatusBox.clear
SwitchStatusBox.AppendText(SwitchStatusIn)
If SwitchStatusIn = "OFF" Then
SwitchStatusBox.BackColor.Equals(Color.Red)
SwitchStatusBox.Show()
Switch_Is_On = False
Else
SwitchStatusBox.BackColor = Color.Green
SwitchStatusBox.Show()
Switch_Is_On = True
End If
If dDistance > 1.0 Then
'show distance in Miles
DistanceUnitsOut.Clear()
DistanceUnitsOut.Text = "MI"
NextImageDistanceOut.Clear()
NextImageDistanceOut.Text = CType(dDistance, String)
Else
NextImageDistanceOut.Clear()
NextImageDistanceOut.Text = CType(dDistanceFT, String)
DistanceUnitsOut.Clear()
DistanceUnitsOut.Text = "FT"
End If
Private Sub NextImageDistanceOut_TextChanged(sender As Object, e As EventArgs) Handles
NextImageDistanceOut.TextChanged
End Sub
Private Sub DistanceUnitsOut_TextChanged(sender As Object, e As EventArgs) Handles
DistanceUnitsOut.TextChanged
End Sub
To see how the .Equals function works...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a = TextBox1.BackColor.Equals(Color.Red)
Debug.Print(a.ToString)
End Sub
In my test app this prints False in the Immediate window.
The answer is that some time ago I began using <variablename_OUT> followed by the TEXT = as in :
NextImageDistanceOut.Text = CType(dDistanceFT, String)
what worked was:
NextImageDistanceOut.Appendtext(CStr(dDistanceFT))
I still don't understand why the first did not work - Both require a previous
NextImageDistanceOut.Clear

How to run an external script using visual basic?

I want to make a Windows application using visual basic and visual studio 2017.
Here I want to run an external Python script which will run when 'Button 1' is pressed in the windows form. It will take input from the Textbox in the form and do some calculations and then return the result and put it back in the Textbox in the form.
The python code can be either this :
r = input()
p = 2*3.14*r
print(p) #Instead of print(p) something that can return the value to the form
OR This:
def peri(r):
p = 2*3.14*r
return p
I managed to find this solution for you and i really hope it works:
1.Write your python code , for example : print("Hello world python !!") in text file and save to .py
2.Create project at VB.NET, drag component 1 button and 1 textbox into form.
3.Double click button1, at event click button1 write this code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim proc As Process = New Process
proc.StartInfo.FileName = "C:\Python34\python.exe" 'Default Python Installation
proc.StartInfo.Arguments = pathmypythonfile.py
proc.StartInfo.UseShellExecute = False 'required for redirect.
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden 'don't show commandprompt.
proc.StartInfo.CreateNoWindow = True
proc.StartInfo.RedirectStandardOutput = True 'captures output from commandprompt.
proc.Start()
AddHandler proc.OutputDataReceived, AddressOf proccess_OutputDataReceived
proc.BeginOutputReadLine()
proc.WaitForExit()
TextBox1.Text = Value
End Sub
Public sub proccess_OutputDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
On Error Resume Next
If e.Data = "" Then
Else
Value = e.Data
End If
End sub
4. Create module file, and write this variable global :
Module module
Public Value As String
End Module
5. Running the application, if textbox1 have populated with some string then your code was success.
HERE is a lick with the full process.
This is the answer by #Dimitar_Georgiev, with small corrections and formatting fixes.
I managed to find this solution for you and i really hope it works:
Write your python code , for example : print("Hello world python !!") in text file and save to .py
Create project at VB.NET, drag component 1 button and 1 textbox into form.
Double click button1, at event click button1 write this code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim proc As Process = New Process
proc.StartInfo.FileName = "C:\Python34\python.exe" 'Default Python Installation
proc.StartInfo.Arguments = "C:\path\to\my\pythonfile.py"
proc.StartInfo.UseShellExecute = False 'required for redirect.
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden 'don't show commandprompt.
proc.StartInfo.CreateNoWindow = True
proc.StartInfo.RedirectStandardOutput = True 'captures output from commandprompt.
proc.Start()
AddHandler proc.OutputDataReceived, AddressOf proccess_OutputDataReceived
proc.BeginOutputReadLine()
proc.WaitForExit()
TextBox1.Text = Value
End Sub
Public sub proccess_OutputDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
On Error Resume Next
If e.Data = "" Then
Else
Value = e.Data
End If
End sub
Create module file, and write this variable global :
Module module
Public Value As String
End Module
Running the application, if textbox1 have populated with some string then your code was success.
HERE is a lick with the full process.

Can't get checking if file exists using textboxes to work

I've been racking my brain for days over this code and I just can't seem to get it working. I've researched and researched with no luck. I have four textboxes on my form. Two textboxes is a folder location and the other two textboxes are file locations. I'm trying to use a function that will return true or false telling if the files in the two textboxes exist or not. I don't see anything at all wrong with this code and it just won't work! I'm sure it's something simple I'm overlooking. Maybe someone else can spot it!
Private Function doesFileExist(folderPath, fileName) As Boolean
If IO.File.Exists(folderPath & "\" & fileName) Then
Return True
Else
Return False
End If
End Function
Private Sub chkStart_CheckedChanged(sender As Object, e As EventArgs) Handles chkStart.CheckedChanged
If doesFileExist(txtCPU.Text, txtFileCPU.Text) And
doesFileExist(txtGPU.Text, txtFileGPU.Text) Then
If chkStart.Checked Then
chkStart.Text = "Stop Monitor"
Else
chkStart.Checked = False
chkStart.Text = "Start Monitor"
End If
Else
chkStart.Checked = False
MessageBox.Show("Please check directory & file locations!", "Error!", MessageBoxButtons.OK)
End if
End Sub
I want to mention that before I tried nested if statements on this I also tried to separate them both like so..
Private Sub chkStart_CheckedChanged(sender As Object, e As EventArgs) Handles chkStart.CheckedChanged
If Not doesFileExist(txtCPU.Text, txtFileCPU.Text) And
Not doesFileExist(txtGPU.Text, txtFileGPU.Text) Then
chkStart.Checked = False
MessageBox.Show("Please check directory & file locations!", "Error!", MessageBoxButtons.OK)
Exit Sub
End If
If chkStart.Checked Then
chkStart.Text = "Stop Monitor"
Else
chkStart.Checked = False
chkStart.Text = "Start Monitor"
End If
End Sub
Both of these ways will show the messagebox if the application is ran with the checkbox checked on start up. Not only will it show the messagebox it also shows the messagebox twice! I've yet to figure that one out!
Your check file exists can be simplified... (It's been a while since I used VB so apologies for any syntax errors, I don't have an IDE to hand)
Function DoesFileExist(Folder as String, Filename As String) As Boolean
Return IO.File.Exists(IO.Path.Combine(Folder, Filename))
End Function
Re: Changing whether the "check" checkbox is set shouldn't perform the check itself - otherwise you only check when people click. (Incidentally, I'm guessing you're getting a message twice as code elsewhere ticks/unticks this checkbox, but it's only a guess).
Private Sub chkStart_CheckedChanged(sender As Object, e As EventArgs) Handles chkStart.CheckedChanged
If chkStart.Checked Then
chkStart.Text = "Stop Monitor"
PollTimer.Start()
Else
chkStart.Text = "Start Monitor"
PollTimer.Stop()
End if
End Sub
Finally... You need to define when your check will happen. Ideally, you'd want to use a FileSystemWatcher which will give you events when the file system changes, but you can also poll using a timer...
Private PollTimer As System.Timers.Timer
Then in your Form Main, do some initial timer setup...
...
PollTimer = New System.Timers.Timer()
PollTimer.Interval = 30000 ' Seconds
AddHandler PollTimer.Elapsed, AddressOf CheckExistsNow
PollTimer.Start()
...
And finally the code to run every time we want to make the check....
Sub CheckExistsNow(sender As Object, e As System.Timers.ElapsedEventArgs)
If Not DoesFileExist(txtGPU.Text, txtFileGPU.Text) Then
' Handle the missing file.
End if
End Sub

How To Save/Recall Folderbrowserdialog SelectedPath

I'm currently teaching myself (with the help of SO & Google) VB.Net to create a launcher for a multiplayer mod and I need users upon first launch of my application to input where their folder is stored, so far I have;
Dim folderDlg As System.Windows.Forms.FolderBrowserDialog
folderDlg = New System.Windows.Forms.FolderBrowserDialog
folderDlg.Description = "Please select your multiplayer folder"
If My.Settings.isFirstRun Then
My.Settings.isFirstRun = False
My.Settings.Save()
folderDlg.ShowDialog()
Else
End If
The button to run the mod itself
Private Sub Launch_mp_Click(sender As Object, e As EventArgs) Handles Launch_mp.Click
If My.Computer.FileSystem.FileExists("launcher.exe") Then
Process.Start("launcher.exe")
Timer2.Interval = 1000
Timer2.Start()
End If
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
p = Process.GetProcessesByName("eurotrucks2")
If p.Count > 0 Then
Timer2.Stop()
Me.WindowState = FormWindowState.Minimized
Me.Visible = True
Else
End If
End Sub
I'm confused as to how I can store the users selected path and then recall it later on for the button without always asking for the dir.
You are almost there:
You have various options where to store the information: registry, old-style using ini-files or in the config file of your application. I would suggest using the config file since you already store the isFirstRun-varialbe in the config. In project explrorer look at the "My Project" folder and double click an item called "Settings". Add a setting of type string called "ModFolder". After that you will be able to access the value of that setting using My.Settings.ModFolder varialbe (see here).
Use the FolderBrowserDialog to store the folder (see here)
if folderDlg.ShowDialog() = DialogResult.Ok then
My.Settings.ModFoler = folderDlg.SelectedPath
My.Settings.Save
end if
When your application starts next time the ModFolder-variable will automaticall hold the value stored so instead of If My.Settings.isFirstRun Then I would check:
If File.Exists(Path.Combine(My.Settings.ModFolder, "AppToStart.Exe")) then
...
end if
If the file exists launch it, if not re-show the dialog to pick the folder.