Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - Numeric Text Box - Events?
Forum - C# / VB.NET - Numeric Text Box - Events?

Avatar
.Ch3bO (Normal User)
Rookie


Messaggi: 29
Iscritto: 07/02/2006

Segnala al moderatore
Postato alle 14:24
Giovedì, 03/07/2008
Questo topic è stato chiuso dal moderatore

Linguaggio Visual basic.net (2005)

Allora il mio problema è il seguente:
Come faccio ad associare un evento ad un controllo (Numeric Text Box) che però viene creato al momento della creazione della form (formLoad)??

Di solito si prende un controllo standard.. esempio un button, ci clicchi due volte (o vai nella lista eventi) e si genera la sub per l'evento che vogliamo gestire..
Mentre se io nella mia pagina di designer non vedo il controllo (perche ripeto si crea al momento della formload) come faccio ad associargli un evento???

Scusate, se magari è una domanda un po banale.. ma il vb è la prima volta che lo guardo.
Grazie!!
Vi posto la classe e l'utilizzo della numeric text box



CLASSE

Public Class NumericTextBox
    Inherits TextBox
    Private SpaceOK As Boolean = False

    ' Restricts the entry of characters to digits (including hex),
    ' the negative sign, the e decimal point, and editing keystrokes (backspace).
    Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
        MyBase.OnKeyPress(e)

        Dim numberFormatInfo As Globalization.NumberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat
        Dim decimalSeparator As String = numberFormatInfo.NumberDecimalSeparator
        Dim groupSeparator As String = numberFormatInfo.NumberGroupSeparator
        Dim negativeSign As String = numberFormatInfo.NegativeSign

        Dim keyInput As String = e.KeyChar.ToString()

        If [Char].IsDigit(e.KeyChar) Then
            ' Digits are OK
        ElseIf keyInput.Equals(decimalSeparator) OrElse keyInput.Equals(groupSeparator) OrElse keyInput.Equals(negativeSign) Then
            ' Decimal separator is OK
        ElseIf e.KeyChar = vbBack Then
            ' Backspace key is OK
            '    else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)
            '    {
            '     // Let the edit control handle control and alt key combinations
            '    }
        ElseIf Me.SpaceOK AndAlso e.KeyChar = " "c Then

        Else
            ' Consume this invalid key and beep.
            e.Handled = True
        End If

    End Sub


    Public ReadOnly Property IntValue() As Integer
        Get
            Return Int32.Parse(Me.Text)
        End Get
    End Property


    Public ReadOnly Property DecimalValue() As Decimal
        Get
            Return [Decimal].Parse(Me.Text)
        End Get
    End Property


    Public Property AllowSpace() As Boolean

        Get
            Return Me.SpaceOK
        End Get
        Set(ByVal value As Boolean)
            Me.SpaceOK = value
        End Set
    End Property
End Class



UTILIZZO

' Create an instance of NumericTextBox.
        Dim NumericTextBox1 As NumericTextBox = New NumericTextBox()
        NumericTextBox1.Parent = Me


        ' Draw the bounds of the NumericTextBox.
        NumericTextBox1.Bounds = New Rectangle(672, 485, 93, 38)

PM
Avatar
manvb.net (Member)
Guru


Messaggi: 663
Iscritto: 28/01/2008

Segnala al moderatore
Postato alle 19:19
Giovedì, 03/07/2008
Per aggiungere un evento in modo dinamico si usa addHandler:

AddHandler oggetto.evento, AddressOf nomep

sub nomep(argomenti)

end sub

Esempio:

AddHandler Textbox1.click, AddressOf Textbox1_C

sub Textbox1_C()
'codice
end sub

PM
Avatar
Progman-92 (Member)
Expert


Messaggi: 368
Iscritto: 16/12/2007

Segnala al moderatore
Postato alle 19:48
Giovedì, 03/07/2008
Si è giusto qll che dice manvb.net. Comunque per chiarirti le idee, puoi leggere il capitolo "Eventi e Handler" della guida di Totem qui: http://www.totem.altervista.org/guida/versione2/B19.php :k:

Ultima modifica effettuata da Progman-92 il 03/07/2008 alle 19:48
PM
Avatar
.Ch3bO (Normal User)
Rookie


Messaggi: 29
Iscritto: 07/02/2006

Segnala al moderatore
Postato alle 20:05
Giovedì, 03/07/2008
Grazie mille a tutti e due! Ho risolto :rotfl:

PM