Results 1 to 6 of 6

Thread: Handle Leave Event for Multiple TextBoxes

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Handle Leave Event for Multiple TextBoxes

    Is there another way to handle the leave event of multiple textboxes other then coding in each event handler? I may not be using correct terms

    instead of doing this:

    exisitng Code:
    1. Private Sub leveladjstvaltxt_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles leveladjstvaltxt.Leave
    2.         If Me.leveladjstvaltxt.Text <> "" Then
    3.             Dim levels As Integer = CInt(Me.leveladjstvaltxt.Text)
    4.             Me.leveladjstvaltxt.Text = Format(levels, "#,###")

    I have 30 TextBoxes all need same formatting , so I was trying to come up with something like this:

    trying Code:
    1. For Each ctrl As Control In Me.Controls
    2.             If TypeOf ctrl Is TextBox And ctrl.Text <> "" Then
    3.                 Dim val As Double = CDbl(ctrl.Text)
    4.                 CType(ctrl, TextBox).Text = Format(val, "#,###")
    5.             End If

    I am stuck though and dont know where i would place that code if this is something that is even possible. Only thing I can currently think of is in the leave event which defeats the purpose, I wanted to avoid having to place the code in 30 differrent textoxes leave event

    Thanks for any input

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Handle Leave Event for Multiple TextBoxes

    You can have an event handler method handle multiple events. If you do this, you will need to work out which textbox the event is from:

    vbnet Code:
    1. Private Sub TextBox_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave,
    2.     TextBox2.Leave,
    3.     TextBox3.Leave,
    4.     TextBox4.Leave
    5.  
    6.     Dim senderTextBox As TextBox = TryCast(sender, TextBox)
    7.  
    8.     If senderTextBox IsNot Nothing Then
    9.  
    10.         If senderTextBox.Text <> "" Then
    11.             Dim levels As Integer = CInt(senderTextBox.Text)
    12.             senderTextBox.Text = Format(levels, "#,###")
    13.         End If
    14.  
    15.     End If
    16.  
    17. End Sub

    I would recommend better handling when the text in the textbox cannot be converted to an integer. I got something weird in my demo code - when you put text in the text box, you can move focus to another control, but you don't get the visual selection cues (highlight around the new textbox, cursor, etc) - you can still type and it affects the textbox, but it's not properly focused and you don't get a Leave event for that textbox.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Handle Leave Event for Multiple TextBoxes

    Quote Originally Posted by Evil_Giraffe View Post
    You can have an event handler method handle multiple events. If you do this, you will need to work out which textbox the event is from:

    vbnet Code:
    1. Private Sub TextBox_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave,
    2.     TextBox2.Leave,
    3.     TextBox3.Leave,
    4.     TextBox4.Leave
    5.  
    6.     Dim senderTextBox As TextBox = TryCast(sender, TextBox)
    7.  
    8.     If senderTextBox IsNot Nothing Then
    9.  
    10.         If senderTextBox.Text <> "" Then
    11.             Dim levels As Integer = CInt(senderTextBox.Text)
    12.             senderTextBox.Text = Format(levels, "#,###")
    13.         End If
    14.  
    15.     End If
    16.  
    17. End Sub

    I would recommend better handling when the text in the textbox cannot be converted to an integer. I got something weird in my demo code - when you put text in the text box, you can move focus to another control, but you don't get the visual selection cues (highlight around the new textbox, cursor, etc) - you can still type and it affects the textbox, but it's not properly focused and you don't get a Leave event for that textbox.
    Thanks I was going to code the keypress so only numbers are allowed

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,510

    Re: Handle Leave Event for Multiple TextBoxes

    I create a class called formutils that handles things simular to what you want to do. Heres some of the code,
    Code:
    Public Class clsFormUtils
        Dim blnFirstClick As Boolean = True
        Dim callingForm As Form
        Public Sub SetEnterAsTab(ByVal frm As Form)
            callingForm = frm
            Dim ctl As Control
            For Each ctl In frm.Controls
                If ctl.GetType.IsAssignableFrom(GetType(TextBox)) Or ctl.GetType.IsAssignableFrom(GetType(ComboBox)) Then
                    AddHandler ctl.KeyDown, AddressOf HandleEnterAsTab
                End If
            Next
        End Sub
        Private Sub HandleEnterAsTab(ByVal sender As Object, _
                                 ByVal e As System.Windows.Forms.KeyEventArgs)
    
            If e.KeyCode = Keys.Enter Then
                Dim CurrentControl As Control = CType(sender, Control)
                callingForm.SelectNextControl(CurrentControl, True, True, True, True)
                e.SuppressKeyPress = True
    
            End If
        End Sub
        Public Sub HightLightText(ByVal frm As Form)
            Dim ctl As Control
            For Each ctl In frm.Controls
                If ctl.GetType.IsAssignableFrom(GetType(TextBox)) Then
                    AddHandler ctl.Click, AddressOf TextBox_Click
                    AddHandler ctl.Leave, AddressOf TextBox_Leave
                End If
            Next
        End Sub
        Private Sub TextBox_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            If blnFirstClick Then
                Dim tb As TextBox = DirectCast(sender, TextBox)
                tb.SelectAll()
            End If
            blnFirstClick = False
        End Sub
        Private Sub TextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
            blnFirstClick = True
        End Sub
    End Class
    Then in the form load event,
    Code:
            'Setup controls to use ENTER to move to next control like TAB
            'Setup Textboxes to be highlighted when entered
            Dim frmUtils As New clsFormUtils
            frmUtils.SetEnterAsTab(Me)
            frmUtils.HightLightText(Me)
    But this this will effect all TextBoxes on the form.

    If that won't work then just create a function that formats the textbox and call it, something like this,
    Code:
        Private Sub SHARE3TextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SHARE3TextBox.Validating
            e.Cancel = ValidateNumeric(Me.SHARE3TextBox)
        End Sub

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: Handle Leave Event for Multiple TextBoxes

    Quote Originally Posted by billboy View Post
    Thanks I was going to code the keypress so only numbers are allowed
    the text will still be a string

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Handle Leave Event for Multiple TextBoxes

    You may like to follow the CodeBank link in my signature and check out my Numeric Text Box. If you use that control instead of a standard TextBox then you won't need any code. Validation and formatting are both built in.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width