切换文本框以在时间后自动输入

时间:2018-05-14 05:43:41

标签: vb.net

所以我正在制作这种时钟的东西,我发现很难找到一种从文本框切换到另一个文本框的方法。我尝试过定时器和调度器,但我没有成功,所以我正在寻找一些专家提示。 以下代码有效。

这是我的vb.net脚本:

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    AddHandler TextBox1.Enter, AddressOf TextBox1_Enter
    AddHandler TextBox2.Enter, AddressOf TextBox2_Enter
    AddHandler TextBox3.Enter, AddressOf TextBox3_Enter
    AddHandler TextBox4.Enter, AddressOf TextBox4_Enter
    AddHandler TextBox5.Enter, AddressOf TextBox5_Enter
    AddHandler TextBox6.Enter, AddressOf TextBox6_Enter
    AddHandler TextBox7.Enter, AddressOf TextBox7_Enter
    AddHandler TextBox8.Enter, AddressOf TextBox8_Enter
    AddHandler TextBox9.Enter, AddressOf TextBox9_Enter
    AddHandler TextBox10.Enter, AddressOf TextBox10_Enter
    AddHandler TextBox11.Enter, AddressOf TextBox11_Enter
    AddHandler TextBox12.Enter, AddressOf TextBox12_Enter


End Sub

Private Sub TextBox1_Enter(sender As Object, e As EventArgs)
    BackColor = Color.Red
End Sub
Private Sub TextBox2_Enter(sender As Object, e As EventArgs)
    BackColor = Color.Blue
End Sub
Private Sub TextBox3_Enter(sender As Object, e As EventArgs)
    BackColor = Color.Yellow
End Sub
Private Sub TextBox4_Enter(sender As Object, e As EventArgs)
    BackColor = Color.Green
End Sub
Private Sub TextBox5_Enter(sender As Object, e As EventArgs)
    BackColor = Color.Pink
End Sub
Private Sub TextBox6_Enter(sender As Object, e As EventArgs)
    BackColor = Color.Teal
End Sub
Private Sub TextBox7_Enter(sender As Object, e As EventArgs)
    BackColor = Color.SteelBlue
End Sub
Private Sub TextBox8_Enter(sender As Object, e As EventArgs)
    BackColor = Color.LightGray
End Sub
Private Sub TextBox9_Enter(sender As Object, e As EventArgs)
    BackColor = Color.Gold
End Sub
Private Sub TextBox10_Enter(sender As Object, e As EventArgs)
    BackColor = Color.BlueViolet
End Sub
Private Sub TextBox11_Enter(sender As Object, e As EventArgs)
    BackColor = Color.Orange
End Sub
Private Sub TextBox12_Enter(sender As Object, e As EventArgs)
    BackColor = Color.Brown
End Sub
End Class

我还发布了一个时钟设计,以更好地想象这个想法。 enter image description here

1 个答案:

答案 0 :(得分:0)

嗯,我没有专业人士(从我提出解决方案的漫长道路来看,这一点很明显,我确信以此为生的人可以做得更好更有说服力的)但这是我想出来的,因为它似乎是一项有趣的任务。

我已经在启动时说明了一个计时器,然后当点击按钮在后台启动一个新线程时,新线程每秒递增一个计数器,而timer1_tick不断检查计数器值是什么并相应地调整表格和文本框,它改变颜色,将数字放入并使其成为焦点项目。

您需要在Form1上使用以下项目: 文本框1到12(在您提供的模式中,12是最顶层的模式) 一个按钮(Button1) 将计时器(Timer1)拖到Form1

然后用下面的

替换Form1上的代码
Imports System.Threading

Public Class Form1
Dim WorkerThread As New Thread(AddressOf DoWork)
Public StartStop As Boolean
Dim Counter As Integer

Private Sub DoWork()
    StartStop = False
    Do Until StartStop = True
        Counter = 0
        Do Until Counter = 13
            Counter = Counter + 1
            Application.DoEvents()
            If Counter < 13 Then Threading.Thread.Sleep(1000)
        Loop
    Loop
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    WorkerThread.Start()
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    Timer1.Start()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    ClearAllTBs()
    If Counter = 1 Then
        TextBox1.BackColor = Color.Bisque
        TextBox1.Text = "1"
        TextBox1.Focus()
    End If
    If Counter = 2 Then
        TextBox2.BackColor = Color.Beige
        TextBox2.Text = "2"
        TextBox2.Focus()
    End If
    If Counter = 3 Then
        TextBox3.BackColor = Color.CornflowerBlue
        TextBox3.Text = "3"
        TextBox3.Focus()
    End If
    If Counter = 4 Then
        TextBox4.BackColor = Color.Crimson
        TextBox4.Text = "4"
        TextBox4.Focus()
    End If
    If Counter = 5 Then
        TextBox5.BackColor = Color.DarkCyan
        TextBox5.Text = "5"
        TextBox5.Focus()
    End If
    If Counter = 6 Then
        TextBox6.BackColor = Color.DarkMagenta
        TextBox6.Text = "6"
        TextBox6.Focus()
    End If
    If Counter = 7 Then
        TextBox7.BackColor = Color.Gold
        TextBox7.Text = "7"
        TextBox7.Focus()
    End If
    If Counter = 8 Then
        TextBox8.BackColor = Color.Fuchsia
        TextBox8.Text = "8"
        TextBox8.Focus()
    End If
    If Counter = 9 Then
        TextBox9.BackColor = Color.DarkViolet
        TextBox9.Text = "9"
        TextBox9.Focus()
    End If
    If Counter = 10 Then
        TextBox10.BackColor = Color.MediumSeaGreen
        TextBox10.Text = "10"
        TextBox10.Focus()
    End If
    If Counter = 11 Then
        TextBox11.BackColor = Color.Navy
        TextBox11.Text = "11"
        TextBox11.Focus()
    End If
    If Counter = 12 Then
        TextBox12.BackColor = Color.BlueViolet
        TextBox12.Text = "12"
        TextBox12.Focus()
    End If
    Application.DoEvents()
End Sub

Private Sub Form1_Closed(sender As Object, e As EventArgs) Handles Me.Closed
    StartStop = True
    WorkerThread.Abort()
End Sub

Private Sub ClearAllTBs()
    TextBox1.BackColor = Color.White
    TextBox2.BackColor = Color.White
    TextBox3.BackColor = Color.White
    TextBox4.BackColor = Color.White
    TextBox5.BackColor = Color.White
    TextBox6.BackColor = Color.White
    TextBox7.BackColor = Color.White
    TextBox8.BackColor = Color.White
    TextBox9.BackColor = Color.White
    TextBox10.BackColor = Color.White
    TextBox11.BackColor = Color.White
    TextBox12.BackColor = Color.White
    TextBox1.Text = ""
    TextBox2.Text = ""
    TextBox3.Text = ""
    TextBox4.Text = ""
    TextBox5.Text = ""
    TextBox6.Text = ""
    TextBox7.Text = ""
    TextBox8.Text = ""
    TextBox9.Text = ""
    TextBox10.Text = ""
    TextBox11.Text = ""
    TextBox12.Text = ""
End Sub
End Class