943,545 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 48118
  • C# RSS
Feb 9th, 2007
0

A simple question : How to enable/disable a button at runtime

Expand Post »
I have two buttons in my main window frame, named btStart and btStop. btStop is initially disabled.
I want to write down such a code that ,
-when user clicks btStart, btStart will be disabled and btStop will be enabled,
-when user clicks btStop, btStop will be disabled and btStart will be enabled.

How can I do that?
Last edited by hkBattousai; Feb 9th, 2007 at 5:40 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
hkBattousai is offline Offline
33 posts
since Nov 2006
Feb 9th, 2007
0

Re: A simple question : How to enable/disable a button at runtime

why not just have one button and toggle its title between start and stop. Inside the button's event handler get the button's text, if its start change it to stop and do the start function, otherwise if its stop change it to start and do the stop function.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Feb 9th, 2007
1

Re: A simple question : How to enable/disable a button at runtime

Hi hkBattousai

Very easy to do that. Here is the code, it will do what you want it to:

c# Syntax (Toggle Plain Text)
  1. private void btStart_Click(object sender, EventArgs e)
  2. {
  3. btStart.Enabled = false;
  4. btStop.Enabled = true;
  5.  
  6. }
  7.  
  8. private void btStop_Click(object sender, EventArgs e)
  9. {
  10. btStart.Enabled = true;
  11. btStop.Enabled = false;
  12. }
Reputation Points: 70
Solved Threads: 4
Junior Poster
RwCC is offline Offline
173 posts
since Jan 2006
Feb 9th, 2007
0

Re: A simple question : How to enable/disable a button at runtime

why not just have one button and toggle its title between start and stop. Inside the button's event handler get the button's text, if its start change it to stop and do the start function, otherwise if its stop change it to start and do the stop function.
Thank you for your reply.
Your solution indeed is a good alternative. But I want to learn how to disable and enable a control.

I am new at C#, I don't believe it would be so difficult to implement. Before C# I was using Win32. Enabling/Disabling controls was done with just one function, like this :
C# Syntax (Toggle Plain Text)
  1. EnableWindow(hWnd, TRUE);
  2. // or
  3. EnableWindow(hWnd, FALSE);
Reputation Points: 10
Solved Threads: 0
Light Poster
hkBattousai is offline Offline
33 posts
since Nov 2006
Feb 9th, 2007
0

Re: A simple question : How to enable/disable a button at runtime

Thank you for your reply.
Your solution indeed is a good alternative. But I want to learn how to disable and enable a control.

I am new at C#, I don't believe it would be so difficult to implement. Before C# I was using Win32. Enabling/Disabling controls was done with just one function, like this :
C# Syntax (Toggle Plain Text)
  1. EnableWindow(hWnd, TRUE);
  2. // or
  3. EnableWindow(hWnd, FALSE);

If you check my post you will see the code to easily enable/disable the buttons based on which you click.
Reputation Points: 70
Solved Threads: 4
Junior Poster
RwCC is offline Offline
173 posts
since Jan 2006
Feb 9th, 2007
0

Re: A simple question : How to enable/disable a button at runtime

Oh, sorry. I didn't scroll down the page then...
Thank you.
Reputation Points: 10
Solved Threads: 0
Light Poster
hkBattousai is offline Offline
33 posts
since Nov 2006
Oct 14th, 2009
0
Re: A simple question : How to enable/disable a button at runtime
Actually I want to disable an InfoPath 2007 button when a user clicks a radio button on my infoPath form.
BTD
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BTD is offline Offline
2 posts
since Oct 2009
Oct 14th, 2009
0
Re: A simple question : How to enable/disable a button at runtime
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ex1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
stop.Visible = false;
}

private void start_Click(object sender, EventArgs e)
{

stop.Visible = true;
start.Visible = false;
}

private void stop_Click(object sender, EventArgs e)
{
start.Visible = true;
stop.Visible = false;
}
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
suganyavasudev is offline Offline
2 posts
since Jun 2009
Oct 14th, 2009
0
Re: A simple question : How to enable/disable a button at runtime
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ex1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
stop.Visible = false;
}

private void start_Click(object sender, EventArgs e)
{

stop.Visible = true;
start.Visible = false;
}

private void stop_Click(object sender, EventArgs e)
{
start.Visible = true;
stop.Visible = false;
}
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
suganyavasudev is offline Offline
2 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: InfoPath Question
Next Thread in C# Forum Timeline: Capture Toshiba Mute, Lights Off and Dolby Key





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC