Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

VB 2009, dinamicki kreirani TextBox-ovi kako dodati event

[es] :: .NET :: VB 2009, dinamicki kreirani TextBox-ovi kako dodati event

[ Pregleda: 1561 | Odgovora: 2 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

ksrele
Programer - informatičar
Gold Drink D.O.O. Subotica
Subotica

Član broj: 14253
Poruke: 1642
*.dynamic.isp.telekom.rs.

ICQ: 66444502


+47 Profil

icon VB 2009, dinamicki kreirani TextBox-ovi kako dodati event23.02.2011. u 16:56 - pre 160 meseci
Dakle, dinamicki, programski, sam kreirao nekoliko TextBox-ova i treba da im nabacim neke eventove.

Nasao sam ovaj kod koji je za C
Code (c):

foreach (Control c in MyControls.Controls)
 {
       if (c.GetType().FullName == "System.Windows.Forms.TextBox")
        {

            TextBox textBoxControl = (TextBox)c;
            textBoxControl.Leave += new EventHandler(textBoxControl_Leave);

         }
 }

private void textBoxControl_Leave(object sender, EventArgs e)
            {
                decimal broj;
                TextBox myTb = (TextBox)sender;
                if (decimal.TryParse(myTb.Text, out broj))
                {
                    myTb.Text = broj.ToString("N2");
                }
            }
 


Preveo sam ga u VB i dobio ovako nesto:
Code (vbnet):

For Each c As Control In MyBase.Controls
            If c.[GetType]().FullName = "System.Windows.Forms.TextBox" Then

                Dim textBoxControl As TextBox = DirectCast(c, TextBox)

                textBoxControl.Leave += New EventHandler(textBoxControl_Leave)
            End If
Next

Private Sub textBoxControl_Leave(ByVal sender As Object, ByVal e As EventArgs)
        Dim broj As Decimal
        Dim myTb As TextBox = DirectCast(sender, TextBox)
        If Decimal.TryParse(myTb.Text, broj) Then
            myTb.Text = broj.ToString("N2")
        End If
End Sub
 


Ali imam problema sa redom textBoxControl.Leave += New EventHandler(textBoxControl_Leave) i dobijem sledecu gresku:

Citat:

Public Event Leave(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
 
Odgovor na temu

ksrele
Programer - informatičar
Gold Drink D.O.O. Subotica
Subotica

Član broj: 14253
Poruke: 1642
*.dynamic.isp.telekom.rs.

ICQ: 66444502


+47 Profil

icon Re: VB 2009, dinamicki kreirani TextBox-ovi kako dodati event23.02.2011. u 17:12 - pre 160 meseci
OK, skontao sam nesto sam.

Dakle, ovako se to radi da VB ne izbaci gresku:
Code (vbnet):

For Each c As Control In MyBase.Controls
            If c.[GetType]().FullName = "System.Windows.Forms.TextBox" Then

                Dim textBoxControl As TextBox = DirectCast(c, TextBox)

                AddHandler textBoxControl.KeyPress, AddressOf textBox_KeyPress

            End If
        Next
    End Sub
    Private Sub textBox_KeyPress(ByVal sender As Object, ByVal e As EventArgs)
        Dim myTb As TextBox = DirectCast(sender, TextBox)
        MsgBox(myTb.Text)
        'If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
        '   AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> DecimalSeparator AndAlso e.KeyChar <> "-" Then
        '    'cancel keys
        '    e.Handled = True
        'End If
    End Sub
 


Ali, na zalost, ni ne odradi akciju (MsgBox) a ovo sto je u komnetaru tek ne radi jer e objekat nema KeyChar membera (ne znam kako da kazem na srpskom)...

Molim vas za pomoc.
 
Odgovor na temu

ksrele
Programer - informatičar
Gold Drink D.O.O. Subotica
Subotica

Član broj: 14253
Poruke: 1642
*.dynamic.isp.telekom.rs.

ICQ: 66444502


+47 Profil

icon Re: VB 2009, dinamicki kreirani TextBox-ovi kako dodati event23.02.2011. u 17:30 - pre 160 meseci
E dok meni neko ne pomogne ja uvek sam sebi resim problem. Valjda prebrzo radim i nemam vremena da cekam pomoc pa se sam pomucim i nadjem resenje.
I kao poklon svima koji ovo citaju nudim resenje. Mozda neko muci isti problem kao ja.

Code (vbnet):

For Each c As Control In MyBase.Controls

            If c.[GetType]().FullName = "System.Windows.Forms.TextBox" Then

                Dim textBoxControl As TextBox = DirectCast(c, TextBox)

                AddHandler textBoxControl.KeyPress, AddressOf textBox_KeyPress
                AddHandler textBoxControl.KeyDown, AddressOf textBox_KeyDown

            End If
        Next
    End Sub
    Private Sub textBox_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        Dim myTb As TextBox = DirectCast(sender, TextBox)
        If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
           AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> DecimalSeparator AndAlso e.KeyChar <> "-" Then
            'cancel keys
            e.Handled = True
        End If
    End Sub
    Private Sub textBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Dim myTb As TextBox = DirectCast(sender, TextBox)
        If (e.KeyCode = Keys.Enter) Then
            Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
        End If
    End Sub
 
 
Odgovor na temu

[es] :: .NET :: VB 2009, dinamicki kreirani TextBox-ovi kako dodati event

[ Pregleda: 1561 | Odgovora: 2 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.