Reputation: 11
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
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
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