Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 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 December 3rd, 2007, 03:03 PM
Authorized User
 
Join Date: Dec 2007
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default Event - Sender & Event args

Hi !!

I went through a dozens of articles to find usefull tips on sender object and eventargs object , their utility with some examples. I still can't figure out how do we use sender object to refer to a property of the control within its event procedure.

for example i want a reference to a button object to modify its enabled property from within its click event routine , without using the name of the control. i just want to perform the task using the sender object only. but dont know how .

please help. Thanks in advance.

dev(1);
__________________
dev(1);
 
Old December 3rd, 2007, 03:08 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The sender is the object which fired the event, so the following might work:

Button b = (Button)sender;
b.Enabled = false;



/- Sam Judson : Wrox Technical Editor -/
 
Old December 3rd, 2007, 03:08 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Consider:

private void ButtonClick(object sender, EventArgs e)
{
   Console.WriteLine("Button Click Event Fired");
}

Obviously, you could wire this up to a single button's click event without a problem. However, I think what you want to know is how can you use one method to handle the events from many buttons (or other controls). In that case you could do soemthing like this:

private void ButtonClick(object sender, EventArgs e)
{
   Button b = (Button)sender;
   switch(b.ID)
   {
     case "button1":
     b.Enabled = false;
     break;

     case "button2":
     b.Enabled = true;
     break;

     case "button3":
     b.Enabled = true;
     break;
   }
}

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
 
Old December 3rd, 2007, 03:08 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Awww, Sam beat me by 6 seconds! ;]

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
 
Old December 3rd, 2007, 03:22 PM
Authorized User
 
Join Date: Dec 2007
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

MY GOD !!

Such a quick response (Within a minute) !!

thank you very very much. That's exactly what i needed. Can u give me some use full tips like this on EVENTARGs E too. How can i use it say for eample in a button click event ?

I am waiting !!






Quote:
quote:Originally posted by dparsons
 Consider:

private void ButtonClick(object sender, EventArgs e)
{
Console.WriteLine("Button Click Event Fired");
}

Obviously, you could wire this up to a single button's click event without a problem. However, I think what you want to know is how can you use one method to handle the events from many buttons (or other controls). In that case you could do soemthing like this:

private void ButtonClick(object sender, EventArgs e)
{
Button b = (Button)sender;
switch(b.ID)
{
     case "button1":
     b.Enabled = false;
     break;
    
     case "button2":
     b.Enabled = true;
     break;

     case "button3":
     b.Enabled = true;
     break;
}
}

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
dev(1);
 
Old December 3rd, 2007, 03:25 PM
Authorized User
 
Join Date: Dec 2007
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by samjudson
 The sender is the object which fired the event, so the following might work:

Button b = (Button)sender;
b.Enabled = false;



/- Sam Judson : Wrox Technical Editor -/

Thank you too Sam !! that was really helpfull . Can you provide some usefull tips on eventargs e. How do i use it & when ?

dev(1);
 
Old December 3rd, 2007, 03:26 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

In a buton click event the EventArgs has no properties, so is completely useless, just a placeholder.

In other events you'll find that the second parameter is a sub class of EventArgs, such as MouseEventArgs which contains properties detailing where the mouse was and what buttons where held down (for example).

/- Sam Judson : Wrox Technical Editor -/
 
Old December 8th, 2007, 06:28 PM
Registered User
 
Join Date: Dec 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I try to do the same thing in C++ but that didn't work.

((PictureBox)sender)

When i try to cast sender, i have an error who says that i cannot convert from System::Object ^ to System::Windows::Forms::PictureBox.

I really don't know how that dosen't work.

I use .NET 2005 whit a Windows Form project if that might help.

 
Old December 8th, 2007, 07:06 PM
Registered User
 
Join Date: Dec 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's ok, i find the solution.

It's juste because cause i forgot something, instead of :

((PictureBox)sender)

i have to do

((PictureBox ^)sender)

don't know why i have to write ^ but that's work now.

If stupidity is a disease, your case appears to be malignant.
But don't worry, i've heard that the cure is easily administered via a delicate process involving a baseball bat...
 
Old December 9th, 2007, 07:24 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

That's not valid C#. Are you using C++ or C#? If you're using C++ then this is the incorrect forum really...

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
ToolStripMenuItem Enter & Leave Event angelboy C# 2005 6 November 21st, 2008 04:57 PM
first event bostek Excel VBA 2 September 7th, 2006 02:13 AM
Interrupting DoCmd.Close & Form Unload Event SerranoG Access 2 November 19th, 2005 03:41 PM
About Button event and Keydown event zhangxujun1981 XSLT 1 March 6th, 2004 04:59 AM
how to override an event with an event? blah VB.NET 2002/2003 Basics 5 November 13th, 2003 03:06 PM





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