单击动态文本框时选择的表单 [英] Form selected when dynamic textboxes clicked
问题描述
我已经使用这些文本框很多年了,但是现在我们有了Windows 7,它们无法正常工作.当我单击文本框时,应该切换0和1并更改颜色.有时什么也没有发生,无论背景是什么窗口 或选择表单.如果我选择其他文本框,则可以返回并选择所需的文本框.这是非常奇怪的行为.希望有人能帮忙.
I have had these textboxes working for many years but now that we have Windows 7 they are not working properly. When I click on a textbox, it's supposed to toggle 0 and 1 and change color. Sometimes nothing happens and whatever window is in the background or the form gets selected. If I select a different textbox then I can go back and select the one I wanted. This is very strange behavior. Hope some can help.
这是一些部分代码.
For i As Integer = 0 To 15
txtbx(i) = New TextBox
txtbx(i).Font = New System.Drawing.Font("Microsoft Sans Serif", 8.0F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, (0))
txtbx(i).Text = "0"
txtbx(i).Width = 16
If i < 1 Then
txtbx(i).Top() = lbxSI.Top
Else
txtbx(i).Top() = txtbx(i - 1).Top + downlocation 'txtbx(i - 1).Top + txtbx(i).Height
End If
txtbx(i).Left() = lbxSI.Location.X + lbxSI.Width '128 'csbCk(i - 1).Left + csbCk(i).Width
Me.Controls.Add(txtbx(i))
Next
TextBox1 = txtbx(0)
TextBox2 = txtbx(1)
TextBox3 = txtbx(2)
TextBox4 = txtbx(3)
TextBox5 = txtbx(4)
TextBox6 = txtbx(5)
TextBox7 = txtbx(6)
TextBox8 = txtbx(7)
TextBox9 = txtbx(8)
TextBox10 = txtbx(9)
TextBox11 = txtbx(10)
TextBox12 = txtbx(11)
TextBox13 = txtbx(12)
TextBox14 = txtbx(13)
TextBox15 = txtbx(14)
TextBox16 = txtbx(15)
私有
TextBox1_MouseDown( 发送方 如 对象 , As System.Windows.Forms. 句柄 TextBox1.MouseDown
SubTextBox1_MouseDown(ByValsender AsSystem.Object, ByVale AsSystem.Windows.Forms.MouseEventArgs) HandlesTextBox1.MouseDown
changeSingleCommand(0)
changeSingleCommand(0)
changeColor(0)
changeColor(0)
Sub
Sub TextBox2_MouseDown( 对象 , ByVal 跨度> e MouseEventArgs )
PrivateSubTextBox2_MouseDown(ByValsender AsSystem.Object, ByVale AsSystem.Windows.Forms.MouseEventArgs) HandlesTextBox2.MouseDown
changeSingleCommand(1)
changeSingleCommand(1)
changeColor(1)
changeColor(1)
Sub
Sub TextBox3_MouseDown( 对象 , ByVal 跨度> e MouseEventArgs )
PrivateSubTextBox3_MouseDown(ByValsender AsSystem.Object, ByVale AsSystem.Windows.Forms.MouseEventArgs) HandlesTextBox3.MouseDown
changeSingleCommand(2)
changeSingleCommand(2)
changeColor(2)
changeColor(2)
Sub
Sub TextBox4_MouseDown( 对象 , ByVal 跨度> e MouseEventArgs )
PrivateSubTextBox4_MouseDown(ByValsender AsSystem.Object, ByVale AsSystem.Windows.Forms.MouseEventArgs) HandlesTextBox4.MouseDown
changeSingleCommand(3)
changeSingleCommand(3)
changeColor(3)
changeColor(3)
Sub
Sub TextBox5_MouseDown( 对象 , ByVal 跨度> e MouseEventArgs )
PrivateSubTextBox5_MouseDown(ByValsender AsSystem.Object, ByVale AsSystem.Windows.Forms.MouseEventArgs) HandlesTextBox5.MouseDown
changeSingleCommand(4)
changeSingleCommand(4)
changeColor(4)
changeColor(4)
Sub
Dick Hutchings
Dick Hutchings
推荐答案
我不确定在没有看到更多代码的情况下代码为什么会这样做,我也不确定为什么在执行"TextBox1 = txtbx(0)"之类的事情.也许这样做会更好.添加MouseUp或MouseDown
创建每个文本框时,将处理程序添加到每个文本框,然后将这些文本框添加到一个范围为List(Of TextBox)的类中.然后,您可以对所有文本框仅使用一个处理程序.您投放了发件人"进入文本框,然后您可以访问的所有属性
引发事件的文本框.
I am not sure exactly why your code is doing that without seeing more of your code and i am also not sure why you are doing things like "TextBox1 = txtbx(0)". Maybe doing it something like this would be better. Add a MouseUp or MouseDown
handler to each textbox as they are created and then add the textboxes to a class scoped List(Of TextBox). Then you can use just one handler for all the textboxes. You cast the "sender" into a textbox and then you can access all the properties of
the textbox that raised the event.
Public Class Form1
Private TBList As New List(Of TextBox)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For x As Integer = 0 To 15
Dim tb As New TextBox
AddHandler tb.MouseUp, AddressOf TB_MouseUp
tb.Cursor = Cursors.Hand
tb.Text = "0"
tb.Width = 16
tb.Location = New Point(0, x * tb.Height)
TBList.Add(tb)
Next
Me.Controls.AddRange(TBList.ToArray)
End Sub
Private Sub TB_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim tb As TextBox = DirectCast(sender, TextBox)
If tb.Text = "0" Then
tb.Text = "1"
tb.ForeColor = Color.Red
Else
tb.Text = "0"
tb.ForeColor = Color.Black
End If
End Sub
'This is just to show that you can retreive info from the textbox list when it is needed
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str As String = ""
For Each t As TextBox In TBList
str &= t.Text & vbNewLine
Next
MessageBox.Show(str)
End Sub
End Class
这篇关于单击动态文本框时选择的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!