954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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?

hkBattousai
Light Poster
33 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Hi hkBattousai

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

private void btStart_Click(object sender, EventArgs e)
        {
            btStart.Enabled = false;
            btStop.Enabled = true;

        }

        private void btStop_Click(object sender, EventArgs e)
        {
            btStart.Enabled = true;
            btStop.Enabled = false;
        }
RwCC
Junior Poster
177 posts since Jan 2006
Reputation Points: 70
Solved Threads: 4
 
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 :

EnableWindow(hWnd, TRUE);
// or
EnableWindow(hWnd, FALSE);
hkBattousai
Light Poster
33 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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 :

EnableWindow(hWnd, TRUE);
// or
EnableWindow(hWnd, FALSE);


If you check my post you will see the code to easily enable/disable the buttons based on which you click. ;)

RwCC
Junior Poster
177 posts since Jan 2006
Reputation Points: 70
Solved Threads: 4
 

Oh, sorry. I didn't scroll down the page then...
Thank you.

hkBattousai
Light Poster
33 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

Actually I want to disable an InfoPath 2007 button when a user clicks a radio button on my infoPath form.

BTD
Newbie Poster
2 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

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;
}
}
}

suganyavasudev
Newbie Poster
2 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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;
}
}
}

suganyavasudev
Newbie Poster
2 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You