Beginner Question...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex

    #1

    Beginner Question...

    Hi Everyone,

    I'm working through the Beginning Visual Basic 2005 book, but I have a
    question that though might be obvious, I'm not seeing an explanation.
    THe first example in the book is creating a simple form with two
    buttons and a text box, but for example on the OK button, here's waht
    is in the .vb file for this object:

    Private Sub btnOK_Click(ByV al sender As System.Object, ByVal e As
    System.EventArg s) Handles
    End Sub

    Can someone explain what all this is? I know btn_OK is the name I
    gave the button, and _Click I assume is the action (when you click on
    it, run this sub). But what's the rest, like everything in the
    parenthesis? What does all this mean?

    Alex

  • Rory Becker

    #2
    Re: Beginner Question...

    Private Sub btnOK_Click(ByV al sender As System.Object, ByVal e As
    System.EventArg s) Handles
    End Sub
    Can someone explain what all this is? I know btn_OK is the name I
    gave the button, and _Click I assume is the action (when you click on
    it, run this sub). But what's the rest, like everything in the
    parenthesis? What does all this mean?

    btnOK_Click is the name of the method. It was created by the form designer
    when you double clicked the button on the designer surface. This method is
    technically known also as an event.

    The text inside the parenthesis represent 2 parameters.

    By convention the parameters to the event are....
    ....Sender: The object responsible for calling the method
    ....e : a structure with properties and methods related to the type of event
    being called/raised.

    The idea behind Sender is that you can hookup multiple buttons to work against
    the same event and can use Sender to differentiate between them.

    The e object passed to a button's Click event is just the defacto System.EventArg s.
    It copnveys little to no information that is really useful.

    Your posted code is however missing some detail.

    After the "Handles" keyword is missing the phrase " btnOk.Click"

    This is the code which tells your program to call/raise this event when the
    button is clicked.

    If your program is working, then this code is present and you simply did
    not copy all of it into your post.


    Try adding a second button to your form called "btnCancel" ... then replace
    the method you pasted into your message with this...
    -------------------------------------------------------------
    Public Sub Click(Sender as Object, E as System.eventArg s) handles btnOk.Click,
    btnCancel.Click
    Call Messagebox.show (String.format( "You clicked button '{0}'",Ctype(Se nder,Button).Na me))
    End Sub
    -------------------------------------------------------------

    Now run your program and try clicking the buttons


    --
    Rory



    Comment

    • eBob.com

      #3
      Re: Beginner Question...

      I'm not the OP, but thanks Phill W for your very complete response to Alex.
      Two questions related to part of your response ...

      If an event handler is exclusively for a Button click, could you declare the
      first argument as "ByVal sender as Button" instead of "ByVal sender as
      Object"? Also, if sender is ByVal, are you really changing anything in the
      statement ...

      DirectCast(send er, Button).Enabled = False

      My understanding is that you would be changing something in a copy of
      'sender' and that the caller would not be aware of any change to a ByVal
      object.

      Thanks, Bob


      "Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in message
      news:f84r1h$369 $1@south.jnrs.j a.net...
      Alex wrote:
      >
      <snip"ByVal sender as Object" - sender is a reference (dare I say
      "Pointer") to the object that raised this event, /whatever/ that might be.
      It's typed "As Object" because it really could be /any/ object you can
      define, and Object is the base class for all other objects. Using this
      reference, you can access the control that sent you the event, so that you
      can manipulate it, but you have to know what Type it is:
      >
      Private Sub Button1_Click( _
      ByVal sender as Object _
      , byval e as EventArgs _
      ) Handles Button1.Click
      >
      DirectCast(send er, Button).Enabled = False
      >
      End Sub
      >>
      HTH,
      Phill W.

      Comment

      • Phill W.

        #4
        Re: Beginner Question...

        eBob.com wrote:
        I'm not the OP, but thanks Phill W for your very complete response to Alex.
        Two questions related to part of your response ...
        >
        If an event handler is exclusively for a Button click, could you declare the
        first argument as "ByVal sender as Button" instead of "ByVal sender as
        Object"?
        [Annoyingly] Probably not.
        Your method has to "match" the signature of the event delegate method
        that you want it "hooked up" to. That sounds clumsy but, here's what I
        think the compiler does:

        You code:

        Sub X_Click( sender as Object, e as EventArgs ) Handles X.Click

        The compiler takes this and churns it around into something more like

        Sub X_Click( sender as Object, e as EventArgs)

        .... and ...

        AddHandler X.Click, AddressOf X_Click

        .... which, internally, gets churned into ...

        AddHandler X.Click, New ClickEventHandl er( X_Click )

        "ClickEventHand ler" is a delegate method that gets called when the event
        is raised. Each event can have lots of these delegated methods, which
        is how you can have many routines handling a single event.

        For this last line to work, though, the signature of "X_Click" has to
        "match" the signature of the ClickEventHandl er delegate method - just
        how tightly the compiler enforces this, I don't know.

        Best advice: try it and see! ;-)

        Also, if sender is ByVal, are you really changing anything in the
        statement ...
        >
        DirectCast(send er, Button).Enabled = False
        Remember: you're playing with Objects here, not basic, Value Types.
        (Damn! I miss not being able to say "Pointer"!)

        Whenever you have a variable "containing " an Object, you /don't/
        actually have the whole object - you only have a "reference" (a.k.a
        "Pointer") to that object; the compiler does all the de-referencing for
        you so you don't notice the difference, so when you say

        Sub X_Click( Byval sender as Object ...

        .... you're passing the /reference/ By Value, not the whole object.
        You /can/ change the properties on that object (as in my example) but
        what you /can't/ do is assign and return a whole /new/ object, so ...

        Sub Button1_Click( Byval sender as Object ...
        sender = New Button()

        .... /won't/ have any effect on the original button.

        My understanding is that you would be changing something in a copy of
        'sender' and that the caller would not be aware of any change to a ByVal
        object.
        Nope. Pass an Object By Value and you can still modify /that instance/
        of the object. If you want to "return" a /new/ instance, though, you
        have to pass the reference By Reference.

        BTW, there's no easy way to "copy" an Object; there internals are so
        variable that the framework simply can't do the job automatically; you
        have to code a [ICloneable.]Clone method yourself.

        HTH,
        Phill W.

        Comment

        Working...