How can i create one button for two events in C# 2008 vs?
-
Hello How can i create one button for two events? For example: I want to have one button for start/stop. At the beginning of the application button name is start (start the event ), when I click the button second time it change the name to Stop (stop the event)???? I am very new to C# programming, i know to do this in VB6... Sorry for my bad english! ThankYou
-
Hello How can i create one button for two events? For example: I want to have one button for start/stop. At the beginning of the application button name is start (start the event ), when I click the button second time it change the name to Stop (stop the event)???? I am very new to C# programming, i know to do this in VB6... Sorry for my bad english! ThankYou
You can just create 2 event handlers one called Stop, the other Start and just swop between the 2, something like this
void Stop(object sender, RoutedEventArgs e)
{
button1.Click -= new RoutedEventHandler(Stop);
button1.Click += new RoutedEventHandler(Start);
button1.Content = "Start";
//Do something here
}void Start(object sender, RoutedEventArgs e) { button1.Click -= new RoutedEventHandler(Start); button1.Click += new RoutedEventHandler(Stop); button1.Content = "Stop"; //Do something here }
Initially the Click event would be set to Start(). Hope this helps
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
-
You can just create 2 event handlers one called Stop, the other Start and just swop between the 2, something like this
void Stop(object sender, RoutedEventArgs e)
{
button1.Click -= new RoutedEventHandler(Stop);
button1.Click += new RoutedEventHandler(Start);
button1.Content = "Start";
//Do something here
}void Start(object sender, RoutedEventArgs e) { button1.Click -= new RoutedEventHandler(Start); button1.Click += new RoutedEventHandler(Stop); button1.Content = "Stop"; //Do something here }
Initially the Click event would be set to Start(). Hope this helps
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
Ok understand that. But now i have problem to call this voids in private void btnstart_Click(object sender, EventArgs e) { if (button1.Content = "Start") { } else { } } I get error:The type or namespace name 'RoutedEventArgs' could not be found (are you missing a using directive or an assembly reference?)
-
Ok understand that. But now i have problem to call this voids in private void btnstart_Click(object sender, EventArgs e) { if (button1.Content = "Start") { } else { } } I get error:The type or namespace name 'RoutedEventArgs' could not be found (are you missing a using directive or an assembly reference?)
OK, my error, I assumed you were using WPF not WinForms,. Here is the correct version
private void Start(object sender, EventArgs e)
{
button1.Click -= new EventHandler(Start);
button1.Click += new EventHandler(Stop);
button1.Text = "Stop";
}
private void Stop(object sender, EventArgs e)
{
button1.Click -= new EventHandler(Stop);
button1.Click += new EventHandler(Start);
button1.Text = "Start";
}Sorry about that!
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
-
Hello How can i create one button for two events? For example: I want to have one button for start/stop. At the beginning of the application button name is start (start the event ), when I click the button second time it change the name to Stop (stop the event)???? I am very new to C# programming, i know to do this in VB6... Sorry for my bad english! ThankYou
A singe event with an
if
statement should solve your purpose. Why do you need two events?private void button1_click(object sender, EvenArgs e) {
if (button1.Text == "Start") {
button1.Text = "Stop";
//Start the job
} else {
button1.Text = "Start";
//Stop the job
}
}I am assuming that you're doing the job in a different thread or a
BackgroundWorker
. -
A singe event with an
if
statement should solve your purpose. Why do you need two events?private void button1_click(object sender, EvenArgs e) {
if (button1.Text == "Start") {
button1.Text = "Stop";
//Start the job
} else {
button1.Text = "Start";
//Stop the job
}
}I am assuming that you're doing the job in a different thread or a
BackgroundWorker
.I was thinking the same.Write two methods and handle it in that single event click.
-
A singe event with an
if
statement should solve your purpose. Why do you need two events?private void button1_click(object sender, EvenArgs e) {
if (button1.Text == "Start") {
button1.Text = "Stop";
//Start the job
} else {
button1.Text = "Start";
//Stop the job
}
}I am assuming that you're doing the job in a different thread or a
BackgroundWorker
.As an aside (ish): Please please please don't use the button text to work out what state you're in, use something like a boolean member variable instead (or, if you absolutly must use a property on the button, something like the Tag property). The code snippet you've given will break as soon as you try to localise your application, and just really isn't good practice. Something like:
private bool started = false;
private void button1_click(object sender, EventArgs e) {
if (!started) {
button1.Text = "Stop";
// Start the job
// ...started = true; } else { button1.Text = "Start"; // Stop the job // ... started = false; }
}
Also, if you're working in a different thread, don't forget cross-thread access issues.
-
As an aside (ish): Please please please don't use the button text to work out what state you're in, use something like a boolean member variable instead (or, if you absolutly must use a property on the button, something like the Tag property). The code snippet you've given will break as soon as you try to localise your application, and just really isn't good practice. Something like:
private bool started = false;
private void button1_click(object sender, EventArgs e) {
if (!started) {
button1.Text = "Stop";
// Start the job
// ...started = true; } else { button1.Text = "Start"; // Stop the job // ... started = false; }
}
Also, if you're working in a different thread, don't forget cross-thread access issues.
-
Hello How can i create one button for two events? For example: I want to have one button for start/stop. At the beginning of the application button name is start (start the event ), when I click the button second time it change the name to Stop (stop the event)???? I am very new to C# programming, i know to do this in VB6... Sorry for my bad english! ThankYou
I've done that, I won't do it again; I'd create two Buttons but have only one visible at a time.
-
I've done that, I won't do it again; I'd create two Buttons but have only one visible at a time.
Can you tell us why ?
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
-
Can you tell us why ?
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
Wayne Gaylard wrote:
Can you tell us why ?
Because its just a better way of doing it. Because it avoids the previously mentioned problem of using the button's text to determine what to do, with all the inherant problems associated with using this method. Because the 2 buttons do a different job and therefore should not be the same button (separation of concern) I could go on.
-
As an aside (ish): Please please please don't use the button text to work out what state you're in, use something like a boolean member variable instead (or, if you absolutly must use a property on the button, something like the Tag property). The code snippet you've given will break as soon as you try to localise your application, and just really isn't good practice. Something like:
private bool started = false;
private void button1_click(object sender, EventArgs e) {
if (!started) {
button1.Text = "Stop";
// Start the job
// ...started = true; } else { button1.Text = "Start"; // Stop the job // ... started = false; }
}
Also, if you're working in a different thread, don't forget cross-thread access issues.
If you're going to use the localisation argument then surely you should set the button text to resource strings ;) I like to use Tag for this, unless I am already using it for something else, as it really is a property of the button and should be stored with it. Of course in most cases the button is controlling the state of some business object and you should check that instead:
private Task someBigJob;
private void button1_click(object sender, EventArgs e) {
if (someBigJob == null || !someBigJob.Running) {
button1.Text = "Stop";
// Start the job
someBigJob.Start();
}
else {
button1.Text = "Start";
// Stop the job
someBigJob.Stop();
}
}(for the benefit of the OP, I'm sure you know already!)
-
Wayne Gaylard wrote:
Can you tell us why ?
Because its just a better way of doing it. Because it avoids the previously mentioned problem of using the button's text to determine what to do, with all the inherant problems associated with using this method. Because the 2 buttons do a different job and therefore should not be the same button (separation of concern) I could go on.
Now this is an interesting thread. I don't agree with you that it's a better way to do it, because it comes with other problems (you have to manually place two buttons in the same place, make them the same size, tab order and keyboard mnemonic, etc). If you want to have it look like one button, then make it one button. (Of course a Start and Stop button next to each other is also acceptable.) I would say that the button does one single job: it toggles the state of the underlying thing that is being started or stopped. Logically, one might make it a check box, but users don't expect that.
-
Wayne Gaylard wrote:
Can you tell us why ?
Because its just a better way of doing it. Because it avoids the previously mentioned problem of using the button's text to determine what to do, with all the inherant problems associated with using this method. Because the 2 buttons do a different job and therefore should not be the same button (separation of concern) I could go on.
J4amieC wrote:
Because its just a better way of doing it.
It's just a different way of doing it. There's this thing called a ToggleButton[^]. I wouldn't use a button, but a checkbox. Makes it clear to the user that there are two different states, and that it toggles.
J4amieC wrote:
Because the 2 buttons do a different job and therefore should not be the same button (separation of concern)
If they're calling the same method, one with a boolean "true" and the other with "false"? The world isn't that black-and-white.
Bastard Programmer from Hell :suss:
-
Can you tell us why ?
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
I could, but J4amieC did a good enough job.
-
I've done that, I won't do it again; I'd create two Buttons but have only one visible at a time.
thank you solve my problem with this: If (txtstart.text="OK") { do somthing } else if (txtstart.text="Stop") { application.close(); }
-
thank you solve my problem with this: If (txtstart.text="OK") { do somthing } else if (txtstart.text="Stop") { application.close(); }
That's bogus. X|