473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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

Jul 20 '07 #1
3 1086
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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.EventArgs.
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.eventArgs) handles btnOk.Click,
btnCancel.Click
Call Messagebox.show(String.format("You clicked button '{0}'",Ctype(Sender,Button).Name))
End Sub
-------------------------------------------------------------

Now run your program and try clicking the buttons
--
Rory

Jul 20 '07 #2
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(sender, 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:f8**********@south.jnrs.ja.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(sender, Button).Enabled = False

End Sub
>>
HTH,
Phill W.

Jul 24 '07 #3
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 ClickEventHandler( X_Click )

"ClickEventHandler" 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 ClickEventHandler 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(sender, 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.
Jul 25 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Svens | last post by:
Hey everyone! I'm a math student working on a short script involving logs. I have a function on my scientific calculator, and was wondering if there was a similar funtion in python. For example:...
3
by: jvax | last post by:
Hi all, I hope I'm posting in the right NG... I have a data text file I want to read from a c++ program. the data file goes like this: 90 # number of balls 33 42 13
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
14
by: z_learning_tester | last post by:
But I can't seem to find the answer. The question is how do you reverse the words in a string? Or how do you reverse the numbers listed in a string? The example is usually something like: Turn...
12
by: Blaze | last post by:
I am doing the first walk through on the Visual Studio .Net walkthrough book to learn a little about programming. I am having issues with the first tutorial not running correctly. It seems that...
5
by: optimistx | last post by:
As a beginner in javascript I had a question. I was reading FAQ and posts here. I became very unhappy: Obviously this group is mainly for wise, pedantic, unkind etc people, who already know...
10
by: Roman Zeilinger | last post by:
Hi I have a beginner question concerning fscanf. First I had a text file which just contained some hex numbers: 0C100012 0C100012 ....
4
by: a | last post by:
Dear all vb.net developer I want to know the time I need to master vb.net? I'm beginner
3
by: Ben Keshet | last post by:
I have a probably simple beginner's question - I have a script that I am currently able to print its output. instead, i want to write it into a file - I tried different versions of write() but...
2
by: roanhn | last post by:
Hello. I've to to write a master's thesis. Currently I deal with php, mysql, ajax. Fate decreed that I've to choose one of this subjects: 1.gdi+ library in .net technology 2.ado.net technology...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.