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?

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.

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;
        }
Comments
Thanks!

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

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. ;)

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

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

Edited 3 Years Ago by Dani: Formatting fixed

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

Edited 3 Years Ago by Dani: Formatting fixed

This article has been dead for over six months. Start a new discussion instead.