4 Replies - 674 Views - Last Post: 12 August 2011 - 07:14 AM Rate Topic: -----

#1 Essel  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 118
  • Joined: 08-May 09

Event Handler(Run the same codes with two buttons)

Posted 12 August 2011 - 06:42 AM

please how do i handle an event with two buttons like in vb.net.
that is i have a code under one event and i want to be able to run this same code with the click on another button on the same form. I know how to do this in vb.net but i do not know how to do this in c#.Please help me
because i do not want to duplicate code.
Is This A Good Question/Topic? 0
  • +

Replies To: Event Handler(Run the same codes with two buttons)

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9387
  • View blog
  • Posts: 35,242
  • Joined: 12-June 08

Re: Event Handler(Run the same codes with two buttons)

Posted 12 August 2011 - 06:48 AM

So this is a C# question and not a VB.NET? Moving accordingly.
Was This Post Helpful? 0
  • +
  • -

#3 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Event Handler(Run the same codes with two buttons)

Posted 12 August 2011 - 06:56 AM

Assign the same method to the click event handler, just like you'd do in VB.NET.
Was This Post Helpful? 0
  • +
  • -

#4 Essel  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 118
  • Joined: 08-May 09

Re: Event Handler(Run the same codes with two buttons)

Posted 12 August 2011 - 07:07 AM

View PostMomerath, on 12 August 2011 - 06:56 AM, said:

Assign the same method to the click event handler, just like you'd do in VB.NET.

Please can you explain further
i dont really understand what you mean

I vb.net you can have the eg.Button click event there with its code inside and so at where we can have the handle buttonI.click you can just type, button2.click

i this case it means button2 can run the same code under button1.This is what i want to do
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9387
  • View blog
  • Posts: 35,242
  • Joined: 12-June 08

Re: Event Handler(Run the same codes with two buttons)

Posted 12 August 2011 - 07:14 AM

Ugh.. okay. In your designer... click on your button1. Show the events in the property pane. Go to the click event. Select your method for the click event. Now click button2. Show the events in the property pane. Go to the click event. Select that same method for the click event.

Alternative.

In your form's constructor add the same event handler to each button's click event. This is assuming you have a proper click method already written called 'button2_click'.

button1.Click += new EventHandler(button2_Click);
button2.Click += new EventHandler(button2_Click);


Was This Post Helpful? 4
  • +
  • -

Page 1 of 1