Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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).

VB
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
Posted

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.
VB
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 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!
 
Share this answer
 
.... 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?


VB
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
 
Share this answer
 
Comments
F. Xaver 25-Sep-14 2:47am    
you don't need that index anymore!
sender contains the Textbox itself, where the KeyPress Event occured.

Dim MyTextBox as TextBox = DirecCast(sender, TextBox)
and you have easy access..


Also.. dont use Control array anymore.. i used them alot in VB6 but nowerdays ;) why not adding a Panel to your Form where thoose 10 Textboxes are in..
you could iterate throug it with Panel1.Controls and also do nice things like disable all Textboxes with just Panel1.enabled = false
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
 
Share this answer
 
Now I got it :-).
Via giving the textboxes a .Name I create e.g. TxtA_1, TxtA_2, TxtA_3, ... instead of TxtA(i).
Hmmm ... I have a lot sourcecode adressing such textbox arrays via the same name and an index. Sometimes I feel that TxtA(i) is more compact to code than TxtA_1, TxtA_2 ... isn't it?
But I guess your way is the best practice solution to handel VB6 graphical arrays. I will check my source to see how much effort it is.

Thanks a lot for supporting me.
 
Share this answer
 
Hi F.Xaver,
From my point of view it could work with a mixture.
I create TxtA(i) and give each textbox a .Name TxtA_1, TxtA_2 ...
Filling the textbox array could be done by TxtA(i).Text = ...
But getting textbox input could be realized by the textbox.Name TxtA_1 ...
This works with the following code.
Is this good practice?

VB
Option Explicit On
Option Strict On
Option Infer Off

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)
                .Name = "TxtA_" + i.ToString()
                .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
            AddHandler tb.GotFocus, AddressOf TA_GotFocus
            AddHandler tb.LostFocus, AddressOf TA_LostFocus
        Next

        TxtA(0).Text = "000"
        TxtA(1).Text = "1111"

    End Sub


    Private Sub TA_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        Dim MyTextBox As TextBox = DirectCast(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


    Private Sub TA_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim MyTextBox As TextBox = DirectCast(sender, TextBox)

        MyTextBox.BackColor = Color.LightBlue

    End Sub


    Private Sub TA_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim MyTextBox As TextBox = DirectCast(sender, TextBox)

        MyTextBox.BackColor = Color.Yellow

    End Sub




End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900