Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   A Button That Activates The Other Buttons Code (http://www.programmingforums.org/showthread.php?t=12521)

J-V Feb 6th, 2007 4:27 PM

A Button That Activates The Other Buttons Code
 
Title is little bit confusing :p . Lets say i have a BUTTON1 that have much code in it and i want to make a BUTTON2 somewhere in my form, but BUTTON2 have to do the same thing that BUTTON1 does. One option is to write same code to BUTTON2 as BUTTON1 has, but it takes time :( . Is it possible to make BUTTON2 without typing again the same code, like shortcut or something?

My first post :banana: .

kruptof Feb 6th, 2007 4:41 PM

i havent't done any c#, but i think that language has functions so just put your code that is inside button1 into a function/procedure then call that function/procedure when ever you need to perform that task.

Edit: or you could call button1s procedure instead of doing suggested above

Infinite Recursion Feb 6th, 2007 5:24 PM

A copy-and-paste isn't very time consuming, but why edit the code in two locations?

In the click event of Button2, have it call Button1's click event function.

r30 Feb 7th, 2007 7:05 AM

private void doThisStuff()
{
int i = 0;
string str = "All your commands can go here.";
}

private void button1_Click(object sender, EventArgs e)
{
doThisStuff();
}

private void button2_Click(object sender, EventArgs e)
{
doThisStuff();
}

Infinite Recursion Feb 7th, 2007 10:21 AM

Even better. More descriptive. :)

J-V Feb 7th, 2007 1:15 PM

Thanks everyone, i got it work :) .
I already love this forum :p .


All times are GMT -5. The time now is 1:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC