简体   繁体   English

在vb.net中向文本框输入随机字母

[英]Input random letter to textbox in vb.net

I have problem with inputting random letter in textbox. 我在文本框中输入随机字母时遇到问题。 Here's code: 这是代码:

Imports Microsoft.VisualBasic
Imports System.Timers

Public Class Form1
Dim SlovaTimer As Timer
Dim AbecedaArray() As Char = {"A", "B", "C", "Č", "Ć", "D", "Dž", "Đ", "E", "F", "G", "H" _
                             , "I", "J", "K", "L", "Lj", "M", "N", "Nj", "O", "P", "R" _
                             , "S", "Š", "T", "U", "V", "Z", "Ž"}
Dim counter As Integer = 0

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    SlovaTimer = New Timer(200)
    AddHandler SlovaTimer.Elapsed, New ElapsedEventHandler(AddressOf Handler)
    SlovaTimer.Enabled = True
    Button1.Enabled = False
End Sub

Private Sub Handler(ByVal sender As Object, ByVal e As ElapsedEventArgs)
    If counter = 11 Then
        SlovaTimer.Stop()
        Button2.Enabled = False
    Else
        Dim ctrl As Control
        For Each ctrl In Me.Controls
            If (ctrl.GetType() Is GetType(TextBox)) Then
                Dim txt As TextBox = CType(ctrl, TextBox)
                If txt.Tag = counter Then
                    Dim random As New Random
                    Dim randletter As Integer = random.Next(0, 29)
                    Dim letter As String
                    letter = AbecedaArray(randletter)
                    txt.Text = letter
                End If
            End If
        Next
        SlovaTimer.Start()
    End If

Here's error: Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on. 这是错误: 跨线程操作无效:控件'TextBox1'是从不是在其上创建线程的线程访问的。 Any idea? 任何想法? Thanks! 谢谢!

You are getting this exception because you are trying to change the text of a Textbox in a thread that is not the UI thread. 之所以收到此异常,是因为您试图在不是UI线程的线程中更改文本框的文本。

In this case, you can replace the System.Timers.Timer with System.Windows.Forms.Timer as Plutonix suggested in his comment, and this will probably solve the problem. 在这种情况下,您可以按照Plutonix的建议在System.Windows.Forms.Timer替换System.Timers.Timer ,这很可能会解决此问题。

However, you should know how to handle these exceptions if you come across them in the future. 但是,如果将来遇到异常,您应该知道如何处理这些异常。

To make a cross-thread call to a UI control in winforms you need to use Invoke . 要在Winforms中对UI控件进行跨线程调用,您需要使用Invoke Create a method for setting the text of a textbox, and a delegate to that method: 创建一个用于设置文本框文本的方法,以及该方法的委托:

Delegate Sub SetTextCallback(txt as TextBox, newString As String)

Private Sub SetText(txt as TextBox, newString As String)

    ' Calling from another thread? -> Use delegate
    If txt.InvokeRequired Then
        Dim d As New SetTextCallback(AddressOf SetText)
        ' Execute delegate in the UI thread, pass args as an array
        Me.Invoke(d, New Object() {txt, newString})
    Else ' Same thread, assign string to the textbox
        txt.Text = newString
    End If
End Sub

Now, as you can see, this method actually invokes itself if the property InvokeRequired of the textbox returns True . 现在,您可以看到,如果textbox InvokeRequired属性返回True ,则此方法实际上会调用自身。 if it returns False , it means that you can safely set the Text of the textbox. 如果返回False ,则意味着您可以安全地设置Text框的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM