7 Replies - 470 Views - Last Post: 17 January 2013 - 07:27 AM Rate Topic: -----

#1 MFKJ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-January 13

Restrict my form to not open more then once

Posted 17 January 2013 - 01:01 AM

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.
Is This A Good Question/Topic? 0
  • +

Replies To: Restrict my form to not open more then once

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

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.
Was This Post Helpful? 0
  • +
  • -

#3 b0zhidar  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 21-December 12

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


Was This Post Helpful? 0
  • +
  • -

#4 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

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.
Was This Post Helpful? 0
  • +
  • -

#5 Vishu Sukhdev  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 150
  • Joined: 19-February 10

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






Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3524
  • View blog
  • Posts: 12,031
  • Joined: 12-December 12

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.
Was This Post Helpful? 0
  • +
  • -

#7 MFKJ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-January 13

Re: Restrict my form to not open more then once

Posted 17 January 2013 - 06:56 AM

View Postandrewsw, on 17 January 2013 - 06:12 AM, said:

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.

Was This Post Helpful? 0
  • +
  • -

#8 MFKJ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-January 13

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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1