Hello,
I have a main form which displays information which the user can update by clicking an update button, this creates a form with text fields and a submit button which updates a text file when the user clicks the button.
However, the user can click the update button on the main form as many times as they want and it creates lots of the smaller form! Is there a way to allow the smaller form to be displayed while the main form is behind it, but only allow one instance of the form even if the user clicks the button again?
How to allow only one instance of a form?
6 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Manuel20 pointsPosted Jul 3, 2007, 3:21 AM EDT
Declare the smaller form as variable in your main form.
If the smaller form has to be shown, check if that variable isn't null - if it does just reset properties in that form to defaults and show that one, otherwise set that variable by creating an instance and then show it.
Drew Taylor66 pointsPosted Jul 3, 2007, 3:27 AM EDT
That sounds difficult! But I'm guessing it's not, could you show me how you would do it in code?
Mahesh ChandRank #1 · 392.9K pointsPosted Jul 3, 2007, 4:06 AM EDT
Chintan Desai8 pointsPosted Jul 3, 2007, 4:41 AM EDT
assign one Reference of small form in Main form. Before calling or creating instance of small form, check whether it is null of not.If it is null that mean its instance has not created yet,otherwise it is null.
Former memberPosted Jul 3, 2007, 5:51 AM EDT
Sam112 pointsPosted Jul 3, 2007, 9:46 AM EDT
You could just disable the Update button when it's clicked
then re-enable it when the 2nd Form closes
e.g
public partial class MainForm: Form
{
//... RAD Code
private void UpdateButton_Click(object sender, EventArgs e)
{
UpdateButton.Enabled = false;// disable button
Form2 UpdateForm = new Form2(); // create 2nd Form
UpdateForm.FormClosing += new FormClosingEventHandler(this.UpdateForm_FormClosing);
// attach event handler
UpdateForm.Show(); // shows the form
}
private void UpdateForm_FormClosing(object sender, EventArgs e)
{
UpdateButton.Enabled = true;// enable button
}
}