Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 9th, 2011, 01:34 PM
Authorized User
Points: 149, Level: 2
Points: 149, Level: 2 Points: 149, Level: 2 Points: 149, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Location: Belgrade,Serbia
Posts: 34
Thanks: 14
Thanked 0 Times in 0 Posts
Default Chapter 8 Ch08Ex01

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
((Button)sender).Text = "pRVi Klik";
Button newbutton = new Button();
newbutton.Text = "novo dugme";
newbutton.Click += new EventHandler(newbutton_click);
Controls.Add(newbutton);
}
private void newbutton_click(object sender, System.EventArgs e)
{
((Button)sender).Text = "kliknuto";
}
}
}

private void button1_Click(object sender, EventArgs e):

is "object sender" represent the metod?

Thanks in advance
  #2 (permalink)  
Old November 9th, 2011, 07:50 PM
Wrox Author
Points: 1,163, Level: 13
Points: 1,163, Level: 13 Points: 1,163, Level: 13 Points: 1,163, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2010
Location: Austria
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Quote:
Originally Posted by dampyr View Post
private void button1_Click(object sender, EventArgs e):

is "object sender" represent the metod?

Thanks in advance
sender is a variable name. This variable is declared to be of type object. The method name is button1_Click.
At runtime the variable sender contains the object that invoked the event where the event handler is assigned to.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
The Following User Says Thank You to ChristianNagel For This Useful Post:
dampyr (November 10th, 2011)
  #3 (permalink)  
Old November 9th, 2011, 10:33 PM
Friend of Wrox
Points: 689, Level: 9
Points: 689, Level: 9 Points: 689, Level: 9 Points: 689, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2010
Posts: 171
Thanks: 0
Thanked 14 Times in 14 Posts
Default

judging by your question, i'm guessing you're not intimately familiar with events. An event can be many things, including a button being pressed with the mouse, a key on the keyboard being pressed, a process being completed, etc. In the case of the code you posted, I'm guessing there's only one sender, which is "button1" being clicked. So "object sender" refers to the button you just clicked.

I'm weak on events, but imagine you have a form that asks you what types of vegetables you eat, then at the bottom there are two buttons: one that says you eat less than 3 servings a day and one that says you eat more than 3 servings a day. Let's say this form estimates how much vitamin A you get a week.

Instead of writing very similar code (calculating how much vitamin A there is in each vegetable), for both button1 and button2, when they get clicked, you can have both button1 and button2 refer to the same code and do your business with the math, then test if it's button1 or button2 to find your multiplier, i.e.

Code:
if (sender.name == button1)
{
myResult = myResult * 3
}
else
{
myResult *= 4
}
etc.

Dunno if that helps. The short answer is the "object sender" refers to whatever raised the event, and sometimes it's helpful to know what raised the event as many objects can raise the same event.

Hope that helps.
The Following User Says Thank You to mtranchi For This Useful Post:
dampyr (November 10th, 2011)
  #4 (permalink)  
Old November 10th, 2011, 06:09 AM
Authorized User
Points: 149, Level: 2
Points: 149, Level: 2 Points: 149, Level: 2 Points: 149, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Location: Belgrade,Serbia
Posts: 34
Thanks: 14
Thanked 0 Times in 0 Posts
Default

You are right,i im not familiar with events,your answer is very helpfull,it's certain that in oncoming chapters i will post more questions on this forum because i have no prior programming experience.Thank You
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 6 - Code Download Missing for this Chapter dbaechtel BOOK: Professional SharePoint 2007 Development ISBN: 978-0-470-11756-9 0 August 11th, 2009 11:02 AM
Chapter 2 - End of chapter exercises whizzkid1892 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 July 30th, 2008 12:02 PM
Generics chapter 12 difficult chapter i found ...? Larryz C# 2005 1 July 4th, 2007 09:40 PM



All times are GMT -4. The time now is 10:02 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.