簡體   English   中英

在文本框中輸入字符串輸出(VB.NET)

[英]Type String Out In TextBox (VB.NET)

我試圖使我的應用程序看起來像在文本框中鍵入一個字符串。 我的代碼的主要問題似乎是thread.sleep不會為每個單獨的字符,而是整個應用程序而睡眠。 例如,如果我用字符串“ hello”調用子項,它將停止100毫秒,然后TextBox會一次全部顯示“ hello”。

Sub typeOut(ByVal toType As String)
    toType = toType.ToCharArray()
    For Each letter As Char In toType
        TextBox2.Text = TextBox2.Text + letter
        Threading.Thread.Sleep(100)
    Next
End Sub

謝謝你的幫助!

對於計時器而言,這是一項理想的工作。 您甚至可以做到這一點,以便可以以不同的速度同時鍵入多個TextBox。

概念演示:我在新的Windows Forms項目表單上放置了兩個TextBoxes和兩個Button,並使用以下代碼實現了這一點:

Public Class Form1

    Private Class TypeText
        Property Target As TextBox
        Property TextToType As String

        Private tim As System.Windows.Forms.Timer
        Private currentChar As Integer

        Private Sub EmitCharacters(sender As Object, e As EventArgs)
            Target.Text &= TextToType.Chars(currentChar)
            currentChar += 1
            If currentChar = TextToType.Length Then
                RemoveHandler tim.Tick, AddressOf EmitCharacters
                tim.Dipose()
            End If
        End Sub

        Public Sub Start()
            AddHandler tim.Tick, AddressOf EmitCharacters
            tim.Start()
        End Sub

        'TODO: add Stop, Pause, Restart etc. methods if needed

        Public Sub New(target As TextBox, textToType As String, interval As Integer)
            Me.Target = target
            Me.TextToType = textToType
            tim = New System.Windows.Forms.Timer
            tim.Interval = interval
            currentChar = 0
        End Sub

    End Class

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim typer As New TypeText(TextBox1, "Hello, World!", 200)
        typer.Start()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim typer As New TypeText(TextBox2, "This is some other text.", 350)
        typer.Start()
    End Sub

End Class

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM