文本框数组上的KeyPress事件 [英] KeyPress event on a textbox array
问题描述
我尝试从VB6迁移到VB.NET,我正在努力处理文本框数组的KeyPress事件。当我在其中一个TxtA文本框中键入文本时,不会触发包含此事件的子例程。
我的代码中缺少什么?
将文本框作为数组TxtA(i)添加是很重要的。
I try to migrate from VB6 to VB.NET and I am struggling with the KeyPress event of a textbox array. The subroutine with this event is not triggered when I type text into one of the TxtA textboxes.
What is missing in my code?
It is important to adress the textboxes as an array TxtA(i).
Public Class Form1
Dim TxtA(10) As System.Windows.Forms.TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim y As Integer = 20
Dim i As Integer = 0
For i = 0 To 9
TxtA(i) = New System.Windows.Forms.TextBox
Me.Controls.Add(TxtA(i))
With TxtA(i)
.Location = New System.Drawing.Point(360, y)
.Size = New System.Drawing.Size(100, 20)
.Visible = True
End With
y += 25
Next i
TxtA(0).Text = "000"
TxtA(1).Text = "1111"
End Sub
Private Sub TxtA_KeyPress(ByVal Index As Integer, ByVal KeyAscii As Integer)
MessageBox.Show(Index.ToString())
MessageBox.Show(KeyAscii.ToString())
End Sub
End Class
推荐答案
如评论中所述,您不需要那个索引了。
KeyPress事件为你提供了在发送者对象中称为事件的对象女巫,你只需要在你建议的类型中使用它来使用它。
在这种情况下TextBox;)
该行意味着在事件处理程序中;)
并且强制转换只返回1个Textbox对象不是你的10盒数组!这是错误;)
例如。
As written in the Comment, you don't need that Index anymore.
The KeyPress Event gives you the Object witch called the Event within the sender Object, you just need to cast it in your suggested Type to use it.
TextBox in this case ;)
that line was meant to be within the eventhandler ;)
and the cast only returns only 1 Textbox Object not the Array of your 10 Boxes! Thats the Error ;)
eg.
Private Sub TA_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
Dim MyTextBox As TextBox = DirecCast(sender, TextBox)
MessageBox.Show(String.Format("TextBox where Key was Pressed {0} Containing String {1} Actual Char {2}", MyTextBox.Name, MyTextBox.Text, e.KeyChar.ToString()))
End Sub
MyTextBox是您刚输入Char的TextBox。它就像你的老TxtA(索引)
另一个好主意,帮助避免这种类型的错误,将Option Explicit和Strict设置为On,Option to Off to Off in your项目 - 编译设置;)
希望有所帮助!
MyTextBox is the TextBox you just entered a Char. Its like you the old TxtA(Index)
Another good Idea to help avoiding that Type of errors, set Option Explicit and Strict to On and Option Infer to Off in your Project - Compile Settings ;)
hope that helps!
....我自己得到它。
我忘记了事件处理程序。
但是现在我有问题找到数组中文本框元素的选定索引。在我的第一个代码中,Index是KeyPress sub的一个参数,但这已不再可能了。
有人可以帮助我吗?
.... I got it by myself.
I forgot the event handler.
But now I have the problem to find the selected index of the textbox element inside the array. In my first code Index was an argument of the KeyPress sub, but this is not longer possible.
Could anyone help me?
Public Class Form1
Dim TxtA(10) As System.Windows.Forms.TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim y As Integer = 20
Dim i As Integer = 0
For i = 0 To 9
TxtA(i) = New System.Windows.Forms.TextBox
Me.Controls.Add(TxtA(i))
With TxtA(i)
.Location = New System.Drawing.Point(360, y)
.Size = New System.Drawing.Size(100, 20)
.Visible = True
End With
y += 25
Next i
For Each tb As TextBox In Me.Controls
AddHandler tb.KeyPress, AddressOf TA_KeyPress
Next
TxtA(0).Text = "000"
TxtA(1).Text = "1111"
End Sub
Private Sub TA_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
MessageBox.Show(e.KeyChar.ToString())
End Sub
End Class
嗨F. Xaver,
非常感谢您的快速回复。
我将检查面板功能以及是否适用于我。
当我尝试你的建议时,我收到错误消息:
Dim TxtA(10)As TextBox = DirecCast(发送者) ,TextBox)
似乎不能使用数组,或者我不理解(哪个更可能;-))。
你能用我的代码示例给我更多细节吗?
祝你好运
Hi F. Xaver,
Thanks a lot for your fast response.
I will checkout the panel functionality and if it works for me.
Regading my textbox array I get error messages, when I try your suggestion:
Dim TxtA(10) As TextBox = DirecCast(sender, TextBox)
It seems not to work with an array, or I do not understand (which is more possibel ;-)).
Could you please give me more details using my code example?
Best regards
这篇关于文本框数组上的KeyPress事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!