Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 9th, 2011, 01:34 PM
Authorized User
 
Join Date: Nov 2011
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
 
Old November 9th, 2011, 07:50 PM
Wrox Author
 
Join Date: Sep 2010
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)
 
Old November 9th, 2011, 10:33 PM
Friend of Wrox
 
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)
 
Old November 10th, 2011, 06:09 AM
Authorized User
 
Join Date: Nov 2011
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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.