0

我试图让我的视觉基本代码说出多个文本框,每个文本框之间有 30 秒的延迟,到目前为止我有:

公开课形式1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
    End If
    If My.Computer.FileSystem.FileExists(OpenFileDialog1.FileName) Then
        Dim ioFile As New System.IO.StreamReader(OpenFileDialog1.FileName)
        TextBox1.Text = ioFile.ReadLine()
        TextBox2.Text = ioFile.ReadLine()
        TextBox3.Text = ioFile.ReadLine()
        TextBox4.Text = ioFile.ReadLine()
        TextBox5.Text = ioFile.ReadLine()
        TextBox6.Text = ioFile.ReadLine()
        TextBox7.Text = ioFile.ReadLine()
        TextBox8.Text = ioFile.ReadLine()
        TextBox9.Text = ioFile.ReadLine()
        TextBox10.Text = ioFile.ReadLine()
    End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim SAPI
    SAPI = CreateObject("SAPI.spvoice")
    SAPI.Speak(TextBox1.Text)




End Sub

结束类

基本上我有 10 个文本框,其中有 10 个单词(从 txt 文件加载),我有一个 txt 到语音代码,说第一个文本框,但我希望链接到按钮 2 的文本到语音代码来说出所有文本框每个文本框之间有 30 秒的延迟,我该怎么做?

4

2 回答 2

0

这是一个使用 BackgroundWorker() 和从 TextBoxes 中的单词构建的 Enumerator 的示例:

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            Dim matches() As Control
            Dim lines() As String = System.IO.File.ReadAllLines(OpenFileDialog1.FileName)
            For i As Integer = 1 To 10
                matches = Me.Controls.Find("TextBox" & i, True)
                If matches.Length > 0 AndAlso TypeOf matches(0) Is TextBox Then
                    If i <= lines.Count Then
                        DirectCast(matches(0), TextBox).Text = lines(i - 1)
                    Else
                        DirectCast(matches(0), TextBox).Text = ""
                    End If
                End If
            Next
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Not BackgroundWorker1.IsBusy Then
            Dim words As New List(Of String)
            Dim matches() As Control
            For i As Integer = 1 To 10
                matches = Me.Controls.Find("TextBox" & i, True)
                If matches.Length > 0 AndAlso TypeOf matches(0) Is TextBox Then
                    Dim TB As TextBox = DirectCast(matches(0), TextBox)
                    If TB.Text.Trim <> "" Then
                        words.Add(TB.Text.Trim)
                    End If
                End If
            Next
            If words.Count > 0 Then
                BackgroundWorker1.RunWorkerAsync(words.GetEnumerator)
            End If
        End If
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim SAPI = CreateObject("SAPI.spvoice")
        Dim wordsEnum As IEnumerator(Of String) = DirectCast(e.Argument, IEnumerator(Of String))
        While wordsEnum.MoveNext
            SAPI.Speak(wordsEnum.Current)
            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(30).TotalMilliseconds)
        End While
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        MessageBox.Show("Done!")
    End Sub

End Class
于 2013-11-08T23:45:01.020 回答
0

为此,您可能需要一个多线程应用程序。但是,如果您没有一种快速而肮脏的制作方法,那就是:

system.threading.thread.sleep(30000)

但随后用户界面将锁定。所以你真的希望它在不同的线程上运行。计时器是实现这一点的最简单方法。只需将 timer.tick 值设置为 30,000。当按下按钮 2 时,它会创建一个包含所有文本框字符串的数组。其中有一个全局变量(整数)。每次计时器被打勾时,它都会在全局变量的索引处读出数组中的文本,然后递增全局变量!

http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx

于 2013-11-08T20:24:23.777 回答