Cannot pass variable in Private sub BackgroundWorker [duplicate] - vb.net

Imports SpeechLib
Public Class Form1
Public vox = CreateObject("sapi.spvoice")
Private Sub cmdSpeak_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSpeak.Click
Dim text2 As String = "Hello , This is a Text. Hello , This is a Text."
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub cmdPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPause.Click
vox.pause()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim SVEPhoneme As Integer = 64
vox.EventInterests = SVEPhoneme
vox.AlertBoundary = SVEPhoneme
End Sub
Private Sub cmdResume_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdResume.Click
vox.resume()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
vox.Speak(Text, SpeechVoiceSpeakFlags.SVSFlagsAsync)
End Sub
End Class
How can I pass text2 to vox.speak?

In cmdSpeak_Click, pass text2 as a parameter to RunWorkerAsync
BackgroundWorker1.RunWorkerAsync(text2)
In BackgroundWorker1_DoWork, retrieve the value of the parameter
vox.Speak(DirectCast(e.Argument, String), SpeechVoiceSpeakFlags.SVSFlagsAsync)

Related

How to save data to a text file?

Basically, I have this program, where you click a link and it opens it. But it doesn't save all the links I put in. There is a name list and link list. You can add links and names with the buttons, and then open link with 'watch'. I was primarily going to use this for youtubers and streamers. Here is the code.
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim NamePath As String = ("C:\Favoriter\NameList.txt\")
Dim nsr As StreamReader
nsr = New StreamReader(NamePath)
Do Until nsr.EndOfStream
lstName.Items.Add(nsr.ReadLine)
Loop
nsr.Close()
Dim URLPath As String = ("C:\Favoriter\URLList.txt\")
Dim usr As StreamReader
usr = New StreamReader(URLPath)
Do Until usr.EndOfStream
lstURL.Items.Add(usr.ReadLine)
Loop
usr.Close()
End Sub
Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim NamePath As String = ("C:\Favoriter\NameList.txt\")
Dim nsw As StreamWriter
nsw = File.CreateText(NamePath)
Dim NameItems As String
Do Until lstName.Items.Count.Equals(0)
NameItems = lstName.Items.Item(0)
lstName.Items.RemoveAt(0)
nsw.WriteLine(NameItems)
Loop
nsw.Flush()
nsw.Close()
Dim URLPath As String = ("C:\Favoriter\URLList.txt\")
Dim usw As StreamWriter
usw = File.CreateText(URLPath)
Dim URLItems As String
Do Until lstURL.Items.Count.Equals(0)
URLItems = lstURL.Items.Item(0)
lstURL.Items.RemoveAt(0)
usw.WriteLine(URLItems)
Loop
usw.Flush()
usw.Close()
End Sub
Private Sub lblTitle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblTitle.Click
End Sub
Private Sub lstName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstName.SelectedIndexChanged
End Sub
Private Sub lstURL_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstURL.SelectedIndexChanged
End Sub
Private Sub txtName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtName.TextChanged
End Sub
Private Sub txtURL_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtURL.TextChanged
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
lstName.Items.Add(txtName.Text)
lstURL.Items.Add(txtURL.Text)
End Sub
Private Sub btnWatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWatch.Click
If lstURL.SelectedItem = True Then
Process.Start(lstURL.SelectedItem)
End If
End Sub
Private Sub lblName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblName.Click
End Sub
Private Sub lblURL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblURL.Click
End Sub
End Class
As first look i see you are using File.CreateText(Path), this function will overwrite the old file what ever it has data , so everytime after you write your data you delete them by overwriting, you can use
if File.Exists(Path) then
File.Open(Path)
else
File.CreateText(Path)
End If
like this you will not overwrite each time you write the text .

VB.Net Web Browser controls not working

I'm trying to implement a simple web browser in Visual Studio 2013 using VB.Net. However, when I run the application, only the GO button works, and none of the other functions are firing, except for the Form1_Load function. Please help me.
Here is my code:
Public Class Form1
Dim int As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("www.google.com")
End Sub
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs)
If e.KeyCode = Keys.Enter Then
WebBrowser1.Navigate(TextBox1.Text)
End If
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
WebBrowser1.ShowPropertiesDialog()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
WebBrowser1.GoBack()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
WebBrowser1.GoForward()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
WebBrowser1.Refresh()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
WebBrowser1.Stop()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
WebBrowser1.Navigate("www.google.com")
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
WebBrowser1.Navigate(TextBox1.Text)
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
End Sub
End Class
You need to add the Handles Button1.Click, Handles Button2.Click, etc to each sub

VB.net Form unexpectingly terminating

Hi I have the following form but cant figureout why its upbrubtly terminiating when difrent buttons are clicked?
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub button1_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
Dim TEST1 As Integer = System.IO.Directory.GetFiles("C:\test\test").Length
If TEST1 = 0 Then
Me.WebBrowser1.Navigate("http://www.hotmail.com")
End If
End Sub
Private Sub button1_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave
Me.WebBrowser1.Navigate("http://WWW.facebook.com")
End Sub
Private Sub button2_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.MouseLeave
Me.WebBrowser1.Navigate("http://WWW.facebook.com")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.WebBrowser1.Navigate("file://C:\test\test")
Button1.Enabled = False
Button2.Enabled = True
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.WebBrowser1.Navigate("file://C:\test")
Button2.Enabled = False
Button1.Enabled = True
End Sub
Private Sub button2_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.MouseEnter
Dim TEST2 As Integer = System.IO.Directory.GetFiles("C:\test\test").Length
If TEST2 = 0 Then
Me.WebBrowser1.Navigate("http://www.hotmail.com")
End If
End Sub
The terms face book and hotmail are just random to keep company site private :)
I suspect the Mouse_Enter event and the Mouse_Leave events are not giving time to the webbrowser to fully load the document and maybe it is internally crashing.
Try checking if the webbrowser has finished working before navigating again and tell us:
Use
If WebBrowser1.ReadyState = WebBrowserReadyState.Complete
Me.WebBrowser1.Navigate("http://www.google.com")
End if

Progress Bar won't work for the third download in VB.NET

I'm trying to download 4 files simultaneously using 4 webclients. It download all 4 files simultaneously but the progress bar of first two works fine and the third progress bar exactly moves with the 1st one, and the fourth progress bar exactly moves with the 2nd one. Here's my code relevant to the issue.
Public WithEvents downloadFile1 As WebClient
Public WithEvents downloadFile2 As WebClient
Public WithEvents downloadFile3 As WebClient
Public WithEvents downloadFile4 As WebClient
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
System.Net.ServicePointManager.DefaultConnectionLimit = 10
End Sub
Private Sub startDownloadFile1()
downloadFile1 = New WebClient
Dim targetURL As String = lstURLs.Items.Item(0)
Dim destinationPath As String = "e:\Downloads\0.jpg"
downloadFile1.DownloadFileAsync(New Uri(targetURL), destinationPath)
End Sub
Private Sub startDownloadFile2()
downloadFile2 = New WebClient
Dim targetURL As String = lstURLs.Items.Item(1)
Dim destinationPath As String = "e:\Downloads\1.jpg"
downloadFile2.DownloadFileAsync(New Uri(targetURL), destinationPath)
End Sub
and similler for startDownloadFile3() and startDownloadFile()
Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
startDownloadFile1()
startDownloadFile2()
startDownloadFile3()
startDownloadFile4()
End Sub
Private Sub downloadFile1_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles downloadFile1.DownloadProgressChanged
pb1.Value = e.ProgressPercentage
End Sub
Private Sub downloadFile2_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles downloadFile2.DownloadProgressChanged
pb2.Value = e.ProgressPercentage
End Sub
Private Sub downloadFile3_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles downloadFile1.DownloadProgressChanged
pb3.Value = e.ProgressPercentage
End Sub
Private Sub downloadFile4_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles downloadFile2.DownloadProgressChanged
pb4.Value = e.ProgressPercentage
End Sub
Private Sub downloadFile3_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles **downloadFile1.**DownloadProgressChanged
pb3.Value = e.ProgressPercentage
End Sub
Private Sub downloadFile4_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles **downloadFile2.**DownloadProgressChanged
pb4.Value = e.ProgressPercentage
Change to: simple error
Private Sub downloadFile3_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles **downloadFile3.**DownloadProgressChanged
pb3.Value = e.ProgressPercentage
End Sub
Private Sub downloadFile4_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles **downloadFile4.**DownloadProgressChanged
pb4.Value = e.ProgressPercentage

How to pass arguments to a BackGroundWorker

Imports SpeechLib
Public Class Form1
Public vox = CreateObject("sapi.spvoice")
Private Sub cmdSpeak_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSpeak.Click
Dim text2 As String = "Hello , This is a Text. Hello , This is a Text."
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub cmdPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPause.Click
vox.pause()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim SVEPhoneme As Integer = 64
vox.EventInterests = SVEPhoneme
vox.AlertBoundary = SVEPhoneme
End Sub
Private Sub cmdResume_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdResume.Click
vox.resume()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
vox.Speak(Text, SpeechVoiceSpeakFlags.SVSFlagsAsync)
End Sub
End Class
How can I pass text2 to vox.speak?
In cmdSpeak_Click, pass text2 as a parameter to RunWorkerAsync
BackgroundWorker1.RunWorkerAsync(text2)
In BackgroundWorker1_DoWork, retrieve the value of the parameter
vox.Speak(DirectCast(e.Argument, String), SpeechVoiceSpeakFlags.SVSFlagsAsync)