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.
Printable View
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.
Do you mean you're looking for "Handles"? For example, blah blah blah Handles TextBox1.KeyPress ?
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.
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.Quote:
Originally Posted by luckyboy
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:
Private Syb UserControl1_SomeEvent(ByVal sender As Object, ByVal e As EventArgs) Handles UserControl1.SomeEvent '... End Sub
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?
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?
yes, that is. is it possible? how?
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:where HandleKeyPress is the method you declared to handle the event.vb.net Code:
AddHandler Form1.TextBox1.KeyPress, AddressOf HandleKeyPress
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?
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.
now i can do it. here my code:
Anyidea of my code? and why it is always does two times?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