vb.net 如何使用按键VB更改文本框的背景色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19073076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change backcolor of textbox with keypress VB
提问by user2803165
I need a little help with understanding how to change the .backcolor of multiple text boxes.
我需要一些帮助来理解如何更改多个文本框的 .backcolor。
What is of asked of me is this.
问我的就是这个。
"Simulate a traffic light with 3 small square textboxes placed vertically on a form. Initially, the top textbox will be green and the other 2 will be white. When the tab key is pressed, the middle textbox turns yellow and the green box changes to white. The next time the tab key is pressed, the bottom box turns red and the middle box turns white."
"模拟交通灯,在窗体上垂直放置 3 个小方形文本框。最初,顶部的文本框为绿色,另外 2 个为白色。当按下 Tab 键时,中间的文本框变为黄色,绿色框变为白色。下一次按下tab键时,底部框变为红色,中间框变为白色。”
I can't figure out how to write the code as I am pretty new at this if someone could help me learn how to use .keypress I would really appreciate the help.
我不知道如何编写代码,因为我对此很陌生,如果有人可以帮助我学习如何使用 .keypress,我将非常感谢您的帮助。
回答by Joe
It would be easier to do if you used labels. Before you start, declare:
如果你使用标签会更容易做到。在开始之前,声明:
Dim count as Integer
To declare something, you put it under wherever it says: Public Class (...)
声明某事,你把它放在它说的任何地方:Public Class (...)
Private Sub keyreceive(ByVal sender As System.Object, ByVal e As KeyPressEventArgs) Handles MyBase.KeyPress
If e.KeyChar = Convert.ToChar(Keys.Tab) Then
trafficlights()
End If
End Sub
Sub trafficlights()
count += 1
topbox.BackColor = Color.White
middlebox.BackColor = Color.White
bottombox.BackColor = Color.White
If count = 1 Then
topbox.BackColor = Color.Green
End If
If count = 2 Then
middlebox.BackColor = Color.Yellow
End If
If count = 3 Then
bottombox.BackColor = Color.Red
count = 0
End If
End Sub

