This is my new form code it is working fine but the problem is
as i click my button a form appear.
i click again another form appear this thing continues as i click again and again.
i want to restrict my form to not open more then 1 if the form is alread active.
Restrict my form to not open more then once
Page 1 of 17 Replies - 172 Views - Last Post: 17 January 2013 - 07:27 AM
Replies To: Restrict my form to not open more then once
#2
Re: Restrict my form to not open more then once
Posted 17 January 2013 - 01:16 AM
set a boolean if the form is open. Check the boolean before you open the form.
#3
Re: Restrict my form to not open more then once
Posted 17 January 2013 - 04:47 AM
When I saw the thread title, I thought about the same solution that Momerath posted.
bool opened=false;
btnShow Code:
if(opened==false)
{
//open form here
opened=true;
}
else
{
MessageBox.Show("Form already opened");
}
btnClose Code:
opened=false;
//close the form here
#4
Re: Restrict my form to not open more then once
Posted 17 January 2013 - 05:22 AM
So you're not saying you only want it to open once; you're saying you only want the user to be allowed to open one instance at any time.
You could have permanent reference to the form in the form that calls it, and then checks if it is shown before showing. If you retain just one instance with one reference, you should restrict its usage exactly as intended.
You could have permanent reference to the form in the form that calls it, and then checks if it is shown before showing. If you retain just one instance with one reference, you should restrict its usage exactly as intended.
#5
Re: Restrict my form to not open more then once
Posted 17 January 2013 - 05:37 AM
/// <summary>
/// This method return true if form already open
/// </summary>
/// <param name="FormType">instance of form</param>
/// <returns>bool</returns>
public bool IsFormAlreadyOpen(Type FormType)
{
bool bRetval = false;
foreach (Form OpenForm in Application.OpenForms)
{
if (OpenForm.GetType() == FormType)
bRetval = true;
}
return bRetval;
}
And check form already open.
call this method on your button click .
bool bIsOpen = IsFormAlreadyOpen(typeof(frmName));
if(bIsOpen)
return;
else
frmName.show();
#6
Re: Restrict my form to not open more then once
Posted 17 January 2013 - 06:12 AM
An alternative, if it is appropriate, is to open the form modally:
Form2.ShowDialog();
That is, it retains the focus; you cannot return to another form without closing Form2.
Form2.ShowDialog();
That is, it retains the focus; you cannot return to another form without closing Form2.
#7
Re: Restrict my form to not open more then once
Posted 17 January 2013 - 06:56 AM
#8
Re: Restrict my form to not open more then once
Posted 17 January 2013 - 07:27 AM
bool opend = false;
private void quranoTafsirToolStripMenuItem_Click(object sender, EventArgs e)
{
Read_QuranoTafsir readQuran = new Read_QuranoTafsir();
if (opend == false)
{
readQuran.Show();
readQuran.MdiParent = this;
opend = true;
}
else
{
readQuran.Focus();
opend = false;
}
}
i add above code but still same problem
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|