Results 1 to 12 of 12

Thread: [2005] declare event

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    81

    [2005] declare event

    hi all, how to declare event as a control event. for example i want to declare KeyPress event as Textbox's keypress event.
    Can i do this? and how? many thank for your help.

    Note:it's just an example.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] declare event

    Do you mean you're looking for "Handles"? For example, blah blah blah Handles TextBox1.KeyPress ?

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

    Re: [2005] declare event

    Your terminology is all screwed up. Events are members of types, just like properties and methods. The TextBox class, just like other controls, has a KeyPress event. That event is raised whenever an input character is sent to the control from the keyboard to notify your app that that has happened. You are able to declare a method and register it as a handler for that event, either using the Handles clause of the method as mendhak said, or with the AddHandler statement.

    So, you don't declare an event as a control event. You can declare a method and register it as a handler for a control's event. This is commonly called "creating an event handler". There are several ways you can do this:

    1. Double-click the control in the designer. This will create a handler for the control's default event. For the TextBox class that's the TextChanged event, so that's no good for you here.

    2. In the code window, select your variable from the drop-down list at the top-left and the event from the top-right. This will create a method skeleton with the appropriate signature, including the Handles clause. This would be the most common way to create a handler for a non-default event in VB.

    3. In the design window, select the control and open the Properties window. Click the Events button and then either double-click the desired event to create a new event handler or else select an existing event handler from the drop-down list. This is the least common method in VB but is the best way in some situations, especially when you want the same method to handle multiple events.
    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

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    81

    Re: [2005] declare event

    example: i have A, is user control, and b is form. i want to declare event in form b as event in user control A. so more please?

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

    Re: [2005] declare event

    Quote Originally Posted by luckyboy
    example: i have A, is user control, and b is form. i want to declare event in form b as event in user control A. so more please?
    What you're asking for doesn't make any sense. mendhak has already asked the question and I've already explained how things work in reasonable detail, but let's try again.

    If you declare an event in the UserControl then it's an event of the UserControl, plain and simple. If you declare an event in the form then it's an event of the form. If you declare a method in the form then it's a method of the form. Are you saying that you want to declare a method in the form to handle an event of the UserControl, e.g.
    vb.net Code:
    1. Private Syb UserControl1_SomeEvent(ByVal sender As Object, ByVal e As EventArgs) Handles UserControl1.SomeEvent
    2.     '...
    3. End Sub
    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

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    81

    Re: [2005] declare event

    sorry for my bad english. i have usercontrol,that have a button on it. when i click on button, it will show form1. in form1 there is textbox. i want my usercontrol have event on form1.textbox. it is possible or not? would u understand?

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

    Re: [2005] declare event

    I'm still not 100% what you mean. I realise that English isn't your first language and that makes it difficult, but that's why you have to try to provide a complete explanation.

    Let me see if I've got it. When you click a Button on your UserControl it will display an instance of Form1. On Form1 there is a TextBox and you want your UserControl to handle the KeyPress event of that TextBox. Is that correct?
    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

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    81

    Re: [2005] declare event

    yes, that is. is it possible? how?

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

    Re: [2005] declare event

    You basically have two choices:

    1. The TextBox must be declared Public or Friend so that it can be seen from outside the form.

    2. The TextBox is declared Private and the Form handles the event, then the Form raises an event of its own.

    Either way, you need to declare a method in your UserControl with the appropriate signature to handle the event of interest. You then use the AddHandler statement to attach the method to the event. MSDN can tell you more but here's an example:
    vb.net Code:
    1. AddHandler Form1.TextBox1.KeyPress, AddressOf HandleKeyPress
    where HandleKeyPress is the method you declared to handle the event.
    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

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    81

    Re: [2005] declare event

    i apologize to make u confusing. it is my mistake, actually i want form1.textbox.keypress event is belong to usercontrol's event. It mean that when i used this usercontrol in form, i can use the event keypress of that textbox. it is possible. more please, please?

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

    Re: [2005] declare event

    The TextBox's KeyPress event can't belong to anything other than the TextBox. What you're saying doesn't make any sense I'm afraid.
    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

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    81

    Re: [2005] declare event

    now i can do it. here my code:

    Code:
    'in user control
    Public Shared Event Add_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
    
    Public Shared Sub txtKeyPress(ByVal sender As Object, ByVal e As    System.Windows.Forms.KeyPressEventArgs)
            RaiseEvent Add_KeyPress(sender, e)
    End Sub
    
    'in form1
        Private Sub frm_Home_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            AddHandler txtField1.KeyPress, AddressOf uc.txtKeyPress
        End Sub
    Anyidea of my code? and why it is always does two times?

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