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?

Dani AI

Generated

A short, practical summary that builds on the thread

As demonstrated, the normal WinForms pattern is to change the buttons' Enabled state in the relevant event handlers (this keeps the control present but inactive). 's single-toggle-button idea is a valid UI alternative when the action is conceptually one control that switches state. 's approach hides one control via Visible instead of disabling it — hiding removes the control from view, while disabling keeps it in the layout but grayed out. (referencesource.microsoft.com)

Practical suggestions and a small pattern to follow

Centralize state changes so the same logic is used everywhere (click handlers, completion callbacks, exception handlers). Disable the relevant controls before starting a long-running operation, run the work off the UI thread, and always restore states in a finally block so controls are not left disabled after an error. A compact pattern is to use a single helper to set both buttons' state and call it from handlers and completion code. Run heavy work with Task/async so the UI stays responsive. (learn.microsoft.com)

Threading and safety notes

When work runs on background threads, marshal any UI changes back to the UI thread (check InvokeRequired or use the invoke/BeginInvoke pattern; newer .NET adds InvokeAsync). Also be aware of the “handle not created” timing issue — checks like IsHandleCreated or invoking on the form after Load avoid the common Invoke/BeginInvoke errors. (learn.microsoft.com)

InfoPath note (for )

In InfoPath 2007 the usual approach is conditional formatting: set a rule on the button to make it Disabled (or Read-only) when the radio/option field has the chosen value. Browser-enabled templates have compatibility limits, so test in Preview and on the target server. (support.microsoft.com)

Recommended Answers

All 8 Replies

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;
        }
commented: Thanks! +1

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

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

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;
        }
    }
}
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;
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.