Results 1 to 4 of 4

Thread: [RESOLVED] Call a keypress method dynamically for each control in vb.net 2005

  1. #1

    Thread Starter
    Member rsalumpit's Avatar
    Join Date
    Jan 2008
    Posts
    56

    Resolved [RESOLVED] Call a keypress method dynamically for each control in vb.net 2005

    I working on a project that includes to call a certain type of method to each control, i have this code:

    Code:
    Private Sub txtBcNum1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtBcNum1.KeyPress
    
        If Char.IsDigit(e.KeyChar) Or e.KeyChar = Chr(8) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    
    End Sub
    This code works like a charm if i want to allow only numbers and backspace on my textbox.

    Problem: I have 15 textboxes( txtBcNum1,txtBcNum2,....,txtBcNum15 ), what's the best way to call this function inside KeyPress method on each textboxes with out manually adding it to KeyPress method?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Call a keypress method dynamically for each control in vb.net 2005

    remove the handles keyword part of the sub definition, then on the form load, loop through all the text boxes in the controls collection and use the AddHandler to add the handler to the KEyPress event of each textbox.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Member rsalumpit's Avatar
    Join Date
    Jan 2008
    Posts
    56

    Re: Call a keypress method dynamically for each control in vb.net 2005

    Quote Originally Posted by techgnome View Post
    remove the handles keyword part of the sub definition, then on the form load, loop through all the text boxes in the controls collection and use the AddHandler to add the handler to the KEyPress event of each textbox.

    -tg
    Thanks for the reply. That was my idea also but I'm having problem constructing the actual code. If you have a pseudo code that i can reference that will be very helpful.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Call a keypress method dynamically for each control in vb.net 2005

    something like this:
    Code:
    for each ctrl text box in form controls
       addhandler ctrl keypress, address of event handler
    loop until no more text boxes

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Tags for this Thread

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