Can't get checking if file exists using textboxes to work - vb.net

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

Related

Cancel EnterCell event of spread farpoint

My program use spread farpoint as a 3rd-party control, it has a TextBox control and a Spread control for showing data. When user change active cell in spread, I want to validate that the TextBox must not empty. If the TextBox is empty, EnterCell event must be cancel and the TextBox must got focus, also active cell of spread must not change. I'm stuck here.
Currently I performed validation in LeaveCell event but it doesn't work. EnterCell event fired and spread still changed the active cell :(
If TextBox1.Text = String.Empty Then
MsgBox ("TextBox1 cannot blank")
TextBox1.Select()
'TextBox1.Focus() 'I have tried this function but still not working
End If
Please support me!
Thanks all.
As far as my knowledge, there's nothing we can do to "cancel" EnterCell event. However, I found out that we could do a little trick to achieve it.
Private Sub spread_LeaveCell(sender as Object, e as LeaveCellEventArgs) Handles sprSheet.LeaveCell
If TextBox1.Text = String.Empty Then
MsgBox ("TextBox1 cannot blank")
'This is the trick
e.NewColumn = e.Column
e.NewRow = e.Row
Exit Sub
End If
'Other leave cell proceses..
End Sub
...
Private Sub spread_EnterCell(sender as Object, e as EnterCellEventArgs) Handles sprSheet.EnterCell
If TextBox1.Text = String.Empty Then
TextBox1.Select()
End If
Exit Sub

Have to Click Form To Give Focus

I have a main form with buttons that open a custom message box form. It works fine if it's just a message and the user just needs to click OK. But if the answer to that message box is important, like "Are you sure you want to delete this file?" I use a while loop to wait for the user to respond and once they do then a flag is set from false to true and the response is recorded.
For some reason any response that uses a while loop to wait is causing the message box form to not have focus after being called. Requiring the user to first click on the form, and then click on OK.
So far I've tried using form.Activate() instead of form.Show(), as well as calling Application.DoEvents() inside the while loop since I believed the while loop was taking focus away from the message form immediately after being called. Neither solved the issue.
Code from a message box that works as intended:
If cmbLoadProgram.SelectedItem = "" Then
frmMessageBox.lblHeader.Text = "Set-Up"
frmMessageBox.lblMessageText.Text = "No Program Selected!"
frmMessageBox.Show()
Exit Sub
End If
Code from a message box that needs to be clicked twice:
If btnGetHexStart.Visible = False And cmbStartCondition.SelectedItem = "Pixel" Then
frmMessageBox.lblHeader.Text = "Hex Set-Up"
frmMessageBox.lblMessageText.Text = "Reset Hex Code Data?"
frmMessageBox.Show()
Me.Hide()
While Flag = False
If frmMain.OKCancel = "OK" Then
btnGetHexStart.Visible = True
btnGetHexStart.Enabled = True
btnGetHexStart.PerformClick()
Flag = True
End If
frmMain.delay(20)
End While
End If
I'm wanting both options to only need to be clicked once in order to confirm or cancel the action. Instead of the while loop questions needing to be clicked twice.
This just an idea from me, just how to open msgboxform, here we need MessageForm and one module1, for example:
Public Class MessageForm
'You can assign any variable to show any data in messageform display
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
theResult = MsgBoxResult.Ok
Me.Close()
Me.Dispose()
End Sub
Private Sub bttcancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttcancel.Click
theResult = MsgBoxResult.Cancel
Me.Close()
Me.Dispose()
End Sub
End Class
Module Module1
Public theResult As MsgBoxResult
'You can add some parameter here to submit to MessageForm
Public Function myMessageBox() As MsgBoxResult
myMessageBox = MsgBoxResult.Cancel
MessageForm.ShowDialog()
myMessageBox = theResult
End Function
End Module
and then you can call the myMessageBox anywhere like this:
'if myMessageBox procedure have parameter, apply the paramters too
dim myRslt = myMessageBox()
You don't need to create a form,You can use DialogResult and MessageBox.Show, code:
Dim Result As DialogResult = MessageBox.Show("Set-Up" & vbCrLf & "No Program Selected!", "Warning", MessageBoxButtons.OKCancel)
If Result = DialogResult.OK Then
ElseIf Result = DialogResult.Cancel Then
End If

Visual Basic 2013 Text duplicating

I had issues with a program, so
I was making a notepad program for my XP overlay thingy, and whenever I reopen it twice, the text is duplicated.
For example when I type in
test
and i reopen it second, it outputs:
testtest
I have no idea what is causing this, but it certainly is annoying.
If I could get some help with this issue that would be awesome!
Heres my code:
Private Sub notepad_X_Click(sender As Object, e As EventArgs) Handles notepad_X.Click
Try
My.Computer.FileSystem.WriteAllText("C:\XP\notepad.txt", RichTextBox1.Text, True)
RichTextBox1.Text = ""
Catch Exc As System.IO.DirectoryNotFoundException
My.Computer.FileSystem.CreateDirectory("C:\XP")
End Try
notepad.Enabled = False
notepad.Visible = False
End Sub
Private Sub Notepad_btn_Click(sender As Object, e As EventArgs) Handles StartM_Notepad.Click
RichTextBox1.Text = ""
Try
RichTextBox1.Text = My.Computer.FileSystem.ReadAllText("C:\XP\notepad.txt")
Catch Exc As System.IO.DirectoryNotFoundException
My.Computer.FileSystem.CreateDirectory("C:\XP")
End Try
notepad.Enabled = True
notepad.Visible = True
End Sub
This happens because you have explicitly specified it to do so. You are using the FileSystem.WriteAllText() overload with 3 parameters, where you've set the third parameter to True. That parameter (called append) specifies if you want to append text to your file or overwrite it.
As you've told it to append text to your file it will just write the new text you give it (whatever's in RichTextBox1) after the already existing text. Set the parameter to False so that the text gets overwritten instead:
My.Computer.FileSystem.WriteAllText("C:\XP\notepad.txt", RichTextBox1.Text, False)

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

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

VB2010: How Would I run this extraction process in a backgroundworker with progressbar

So I made a small extraction program which just extracts a zip file to a location, and it also shows the progress of the extraction. But the problem is that whenever it's extracting large zips, the program kinda freezes while extracting and if you go off the process, you can't go back onto it until it's finished extracting, but you can still see the progressbar's progress. This is the code I have so far:
Form2.vb
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If TextBox1.Text = "" Then
Else
ProgressBar1.Visible = True
Button2.Enabled = False
Button3.Enabled = False
TextBox1.Enabled = False
Unzip("FileToExtract.zip", "PathToExtractTo")
End If
End Sub
Unzip.vb
Imports Ionic.Zip
Module SimpleUnzip
Public Sub Unzip(ByVal ZipToUnpack As String, ByVal DirectoryToExstractTo As String)
Try
Using zip As ZipFile = ZipFile.Read(ZipToUnpack)
Form2.ProgressBar1.Maximum = zip.Entries.Count
Dim entry As ZipEntry
For Each entry In zip
entry.Extract(DirectoryToExstractTo, ExtractExistingFileAction.OverwriteSilently)
Form2.ProgressBar1.Value = Form2.ProgressBar1.Value + 1
Next
End Using
Catch ex1 As Exception
End Try
End Sub
End Module
So I have tried things like putting the SimpleUnzip sub in a background worker on the main forum and calling that, but that doesn't work at all, I have also tried a background worker on the module, it extracts but the progressbar doesn't work. Anyone know how to solve this problem?
As with any task using a BackgroundWorker, you do the work in the DoWork event handler and then you call ReportProgress to report the progress. This line:
Form2.ProgressBar1.Maximum = zip.Entries.Count
and this line:
Form2.ProgressBar1.Value = Form2.ProgressBar1.Value + 1
are going to have to be replaced with calls to ReportProgress. In the ProgressChanged event handler, you do what you normally would, i.e. update the ProgressBar.