0

私のビジュアルベーシックコードが各テキストボックス間で30秒の遅延で複数のテキストボックスを話せるようにしようとしています。

パブリック クラス Form1

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 to speechコードがありますが、ボタン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 に答える