Solved

Can any experts explain me (How it work) about the 2 arguments of event procedure in VB.NET?

Posted on 2004-10-16
5
261 Views
Last Modified: 2010-04-23
I am new to vb.net. I know only when I double a button on a form I will write code for its click event. But now I would like to understand about its 2 arguments: sender and e.

     sub button1_click(sender as object, e as eventargs) handles button1.click

How they work? and can they be used in anyway?

Thank you.

0
Comment
Question by:veayou
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 150 total points
ID: 12327590
Sender contains a reference to the button that was clicked. In your examples, you are sure that it will always be Button1 (unless you used AddHandler somewhere) but it is essential if you do things like this:

sub button1_click(sender as object, e as eventargs) handles button1.click, button2.click

This let the same procedure handles the click of button1 and button2. You then use the sender to detect which one was pressed.

In the case of a button click event, the eventargs argument is not essential. You will need it in events like MouseDown (to detect which mouse button was used at which location), KeyPress (to detect which key was pressed), ...
0
 
LVL 10

Assisted Solution

by:Hans Langer
Hans Langer earned 150 total points
ID: 12327628
button1 can fired a lots of events. in this case is "click" who need to send those 2 argument "Sender" and "System.EventArgs"
Sender is the button and EventArgs are the argument sended to the event.

The "Sender is the object that fired the event" in this case "button1"

But suppose you need that 2 button use the same function and you dont want to copy the function:
You have to you "handler"

Sub ChangeMyText(sender as object, e as eventargs) handles button1.click, button2.click ' that mean button1 or button2 can fired the "click Event"

'How you can know if button1 or button2 fired the event ?
'you can cast the sender

Dim bt As New Button
bt = CType(sender, Button)
bt.Text = "I fired the event"   'if button1 fired the event then it will change its text, the same for button2


GL




0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 200 total points
ID: 12327883
Hi veayou,

Both emoreau and GERENTE have given you great explanations.  Here is a simple application that demonstrates those concepts.  It consists of six buttons on a form that can all be repositioned by dragging them around the form.

It uses common routines to accomplish this, so you must make use of the sender variable to determine which button fired the event.  It must also use different members of the eventargs variable to determine which mouse button was clicked and where on the button the click was made.

Hope it helps you understand better,

Idle_Mind


Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents Button4 As System.Windows.Forms.Button
    Friend WithEvents Button5 As System.Windows.Forms.Button
    Friend WithEvents Button6 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.Button3 = New System.Windows.Forms.Button
        Me.Button4 = New System.Windows.Forms.Button
        Me.Button5 = New System.Windows.Forms.Button
        Me.Button6 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(8, 8)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(88, 24)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(184, 8)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(88, 24)
        Me.Button2.TabIndex = 1
        Me.Button2.Text = "Button2"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(360, 8)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(88, 24)
        Me.Button3.TabIndex = 2
        Me.Button3.Text = "Button3"
        '
        'Button4
        '
        Me.Button4.Location = New System.Drawing.Point(8, 232)
        Me.Button4.Name = "Button4"
        Me.Button4.Size = New System.Drawing.Size(88, 24)
        Me.Button4.TabIndex = 3
        Me.Button4.Text = "Button4"
        '
        'Button5
        '
        Me.Button5.Location = New System.Drawing.Point(184, 232)
        Me.Button5.Name = "Button5"
        Me.Button5.Size = New System.Drawing.Size(88, 24)
        Me.Button5.TabIndex = 4
        Me.Button5.Text = "Button5"
        '
        'Button6
        '
        Me.Button6.Location = New System.Drawing.Point(360, 232)
        Me.Button6.Name = "Button6"
        Me.Button6.Size = New System.Drawing.Size(88, 24)
        Me.Button6.TabIndex = 5
        Me.Button6.Text = "Button6"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(456, 266)
        Me.Controls.Add(Me.Button6)
        Me.Controls.Add(Me.Button5)
        Me.Controls.Add(Me.Button4)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private startPoint As Point

    Private Sub Button_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles Button1.MouseDown, Button2.MouseDown, Button3.MouseDown, _
        Button4.MouseDown, Button5.MouseDown, Button6.MouseDown

        If e.Button = MouseButtons.Left Then
            Dim b As Button = CType(sender, Button)
            startPoint = b.PointToScreen(New Point(e.X, e.Y))
        End If
    End Sub

    Private Sub Button_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles Button1.MouseMove, Button2.MouseMove, Button3.MouseMove, _
        Button4.MouseMove, Button5.MouseMove, Button6.MouseMove

        If e.Button = MouseButtons.Left Then
            Dim b As Button = CType(sender, Button)
            Dim curPoint As Point = b.PointToScreen(New Point(e.X, e.Y))
            b.Left = b.Left + (curPoint.X - startPoint.X)
            b.Top = b.Top + (curPoint.Y - startPoint.Y)
            startPoint = curPoint
        End If
    End Sub

End Class
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12362877
<From Feedback>

Thank you very much. This answer make me understand clearer. But I think I have read a book and have met this example before. However, do you have any idea about them like:
 -why they are created?
 -I heard my teacher say "the old version, some events use different arguments but in vb.net all event have only this 2 argument.

The reason they have these two arguments is because the entire .Net framework is object oriented and all events for all controls and objects derive from a common class.  So if an event doesn't actually have any information to convey except that the event itself occurred, then it still must have these two arguments (because they were inherited) but those arguments are simply empty.

So the answer to your question of "why they are created?" is simply, "because of Inheritance"

~IM
0
 

Author Comment

by:veayou
ID: 12366500

Thank you. Idle_Mind.

BM.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question