Sreedhar
Sreedhar

Reputation: 11

How to disable multi clicks in the c# application programming?

In the c sharp application program, if a button is clicked then a dialog box will be opened but if the user clicked more than one time then more dialog boxes are opened. How to overcome it? Please give me the solution regarding this issue.

Upvotes: 1

Views: 88

Answers (3)

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59101

Furquan has a good answer. Edit: as does Fun.

If you can't or don't want your dialog to be modal, you can add extra state to see if the subdialog is already open. Here's some example pseudo-code (it probably won't compile):

class MyForm : Form
{
  public void OnButtonClick()
  {
    if(!isSubDialogOpen)
    {
      isSubDialogOpen = true;
      ShowSubDialog();
    }
  }

  private void OnSubDialogClose()
  {
    isSubDialogOpen = false;
  }

  private void ShowSubDialog()
  {
    SubDialog subDialog = new SubDialog(this);
    subDialog.OnClose += OnSubDialogClose;
    subDialog.Show();
  }

  private bool isSubDialogOpen;
}

class SubDialog : Form
{
  // ...
}

Upvotes: 2

Fun Mun Pieng
Fun Mun Pieng

Reputation: 6891

use ShowDialog function:

using (Form2 frm = new Form2())
{
    frm.ShowDialog();
}

This will disable the current form and only make the new form usable.

Alternatively, you can disable the button to prevent it from being clicked again.

button1.Enabled = false;

But make sure you enable the button when it should be accessible again.

Upvotes: 2

Furqan Hameedi
Furqan Hameedi

Reputation: 4400

If its a desktop application use Modal Dialogs.

Upvotes: 3

Related Questions