7 Replies - 25171 Views - Last Post: 27 October 2010 - 07:56 AM Rate Topic: -----

#1 jammmie999  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 117
  • Joined: 01-April 09

C# Event Handler Arguments

Posted 27 October 2010 - 06:09 AM

Hey Guys,

In the code example below How can I pass the strings to my event handler?
private void LoadMainMenu()
{
...
string strm1 = "hello";
string strm2 = "world";
m1.Click += new EventHandler(StartHost);
m2.Click += new EventHandler(StartHost);
}

private void StartHost(object sender, EventArgs e)
{
//Do something
}



Thank You
Is This A Good Question/Topic? 0
  • +

Replies To: C# Event Handler Arguments

#2 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 383
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: C# Event Handler Arguments

Posted 27 October 2010 - 06:17 AM

Could you tell us what exactly you are trying to do... It would be easier to help you this way...
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C# Event Handler Arguments

Posted 27 October 2010 - 06:57 AM

That event handler is being called when whatever type of control m1 and m2 are (bad names for controls, by the way. No idea what it is or what it does). There's no way to slip something into the call.

However, if I understand what you're asking (and I might not), you could attach a string (or any other object) to the control's Tag property. Tag is used for storing pertinent information about a control inside a control, and can hold anything that derives from object.

So you could do something like this. I'm assuming that m1 and m2 are buttons, but it'll work with whatever UI control that they are, just change the cast.

private void LoadMainMenu()
{
  ...
  m1.Tag = "hello";
  m2.Tag = "world";
  m1.Click += new EventHandler(StartHost);
  m2.Click += new EventHandler(StartHost);
}

private void StartHost(object sender, EventArgs e)
{
  Button m = sender as Button;
  string s = m.Tag as string;
}


You need to add null checks, but that's the basic idea.
Was This Post Helpful? 2
  • +
  • -

#4 jammmie999  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 117
  • Joined: 01-April 09

Re: C# Event Handler Arguments

Posted 27 October 2010 - 07:03 AM

View PostinsertAlias, on 27 October 2010 - 02:57 PM, said:

That event handler is being called when whatever type of control m1 and m2 are (bad names for controls, by the way. No idea what it is or what it does). There's no way to slip something into the call.

However, if I understand what you're asking (and I might not), you could attach a string (or any other object) to the control's Tag property. Tag is used for storing pertinent information about a control inside a control, and can hold anything that derives from object.

So you could do something like this. I'm assuming that m1 and m2 are buttons, but it'll work with whatever UI control that they are, just change the cast.

private void LoadMainMenu()
{
  ...
  m1.Tag = "hello";
  m2.Tag = "world";
  m1.Click += new EventHandler(StartHost);
  m2.Click += new EventHandler(StartHost);
}

private void StartHost(object sender, EventArgs e)
{
  Button m = sender as Button;
  string s = m.Tag as string;
}


You need to add null checks, but that's the basic idea.


Thanks, this is exactly what I needed and works great. M1 and M2 were MenuItem's by the way.
Was This Post Helpful? 0
  • +
  • -

#5 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 383
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: C# Event Handler Arguments

Posted 27 October 2010 - 07:04 AM

I believe you could even create a custom click event handler and pass your string in EventArgs class...
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6530
  • View blog
  • Posts: 14,446
  • Joined: 02-June 10

Re: C# Event Handler Arguments

Posted 27 October 2010 - 07:10 AM

View Postjammmie999, on 27 October 2010 - 05:09 AM, said:

Hey Guys,

In the code example below How can I pass the strings to my event handler?
private void LoadMainMenu()
{
...
string strm1 = "hello";
string strm2 = "world";
m1.Click += new EventHandler(StartHost);
m2.Click += new EventHandler(StartHost);
}

private void StartHost(object sender, EventArgs e)
{
//Do something
}



Thank You


Let's start with this idea... Don't fill up a Button.Click event handler with all your functioning code. As you've already learned it makes it hard to work with.Have the button.click call a method. Now you can use that same method in a dozen different places much more easily.

m1.Click += ButtonHandler;
m2.Click += ButtonHandler;
void ButtonHander(object sender, eventargs e)
{
   // Sender will contain the actual button that sent you here.  Just and fyi for future
   if (((button)sender).name == "m1") StartHost(strm1);
   if (((button)sender).name == "m2") StartHost(strm2);
   // Multiple ifs are also a good candidate for Switch construct
}

void StartHost(string MessageToPass)
{
    // Do something
}

Was This Post Helpful? 2
  • +
  • -

#7 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C# Event Handler Arguments

Posted 27 October 2010 - 07:23 AM

I agree with you about the "not doing the work in the handler" part for sure. But I like using the tag better when it's just one value (or several easily made into a struct/class), because it makes the control more self-contained. You don't have to remember that m1 relates to strm1, you just need to know that some control triggered this event, and that control contains the information about itself I need to do my task.

Another thing I'd do differently is I wouldn't compare controls by name. I'd use the actual reference to check:

if(sender == m1)...


Since this checks to see if both identifiers are pointing to the same address, this would work, and it wouldn't rely on string comparisons.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6530
  • View blog
  • Posts: 14,446
  • Joined: 02-June 10

Re: C# Event Handler Arguments

Posted 27 October 2010 - 07:56 AM

View PostinsertAlias, on 27 October 2010 - 06:23 AM, said:

I agree with you about the "not doing the work in the handler" part for sure. But I like using the tag better when it's just one value (or several easily made into a struct/class), because it makes the control more self-contained. You don't have to remember that m1 relates to strm1, you just need to know that some control triggered this event, and that control contains the information about itself I need to do my task.

Another thing I'd do differently is I wouldn't compare controls by name. I'd use the actual reference to check:

if(sender == m1)...


Since this checks to see if both identifiers are pointing to the same address, this would work, and it wouldn't rely on string comparisons.


I agree with you 100% on both points. I use them a LOT for that purpose. Often times custom and complex object.

I was just trying to keep it simple for the OP so it would stay compliant with his course homework.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1