Click here to Skip to main content
14,266,036 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a button named start. Now once I press start, the application runs fine..

I need to do like this:once the application reaches a particular point it should return to the previous form(first form which is having start button).And there now the button should be stop instead of start.

Can I do like that in C# windows application?If so, how?

Please do help me on this..


1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Here are two options to get you started

Option 1:
Keep the start button as is, place the Stop button next to it. You can use the Visible property on each to decide which is displayed (the stop button should have this as false by default). You could alternatively use the Enabled property, the disabled button will appear greyed out.

Option 2:
Just change the Text property of the existing button as required. Remember to rename the button if you have called it btnStart. Obviously you'll need to sort the extra logic to determine whether the button is Starting or Stopping. A quick hack approach would be to even use the button Text, though hopefully the rest of your code is well enough designed to make this unnecessary.

In response to OP's comment: Assuming the button is called btnStart for example, in code you do the following:

btnStart.Visible = true; //To display, false to hide obviously

As for hiding the Stop button, click on it in the designer window in Visual Studio. In the properties tab, find the "Visible" property in the list and set the value to false in that. If you are having problems finding the property tab, right click the button and choose "Properties", the tab is docked to the right on a default setup.
Vysakh Venugopal 6-Jun-13 7:23am
@keith Barrow: Thanks..If Iam opting 1,then can I make stop button visible and start button invisible??If so,how?
Keith Barrow 6-Jun-13 7:59am
HI, I've updated my answer.
Vysakh Venugopal 6-Jun-13 8:38am
@keith barrow: thanks keith..thats working!!!I went with Option 2..

Can I ask you one more doubt..I have changed my button name now at a point of my app.Now When I click the button with the new name,I should be able to do some other function..Could you please help me on how I can achieve that??
Keith Barrow 6-Jun-13 8:42am
You need to put an if statement in the Event handler, a ***hack** answer is:

But you should find a solution that is cleaner, by either keeping track of whether the state is running separately, or doing this on the object you are running the long=lived process on.
Vysakh Venugopal 6-Jun-13 8:50am
@Keith Barrow: Ok...I have a label that is hidden(visible property set to false).How can I achieve when a button is clicked that label is visible.??

I just tried your btnStart.Visible = true;

But sorry..Its not working..
Keith Barrow 6-Jun-13 9:02am
Is the start button hidden?
Vysakh Venugopal 7-Jun-13 0:59am
No..else I need to do like this..I have 2 labels say 1 is success and 2 is failed.Both are hidden by the properties tab. when some task is finished if it is a success then label 1 must be visible.else the other.Any idea on this??
Keith Barrow 7-Jun-13 5:04am
If the start button isn't hidden btnStart.Visible = true; is working, you need to change true to false. .Visible is a property on all forms controls. I suggest you create a new application, drop a label and a button on to it and practise showing/hiding the label if the button is clicked. You can use the following, assuming your label is called lblFoo:

lblFoo.Visibile = !lblFoo.Visible //Swaps the visible state of lblFoo

If you are having problems, it is often a good idea to take a step back and use a simpler construct until you get the hang of what is happening.
Vysakh Venugopal 7-Jun-13 5:09am
thanks keith..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100