Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Create a function to open form with MdiParent

Posted on 2009-02-23
9
Medium Priority
?
710 Views
Last Modified: 2013-12-17
Hi

I need to create a bool  function that has two forms parameters. First is the name of wanted form, and second is the MDI parent.
And, before open it must check if already created or no.

I've post my code that did not work.
Please help.

Thanks


public static void TenderOpenForm(Form frmNewForm, Form frmMDIParent)
        {
            frmNewForm frmNewForm1 = (frmNewForm)FindFormByType(typeof(frmNewForm));
            if (frmNewForm1 == null)
            {
                frmNewForm1 = new frmNewForm();
                frmNewForm1.MdiParent = frmMDIParent;
                frmNewForm1.Show();
            }
            else
                frmNewForm1.Focus();
        }
 
        public static Form FindFormByType(Type formType)
        {
            foreach (Form f in Application.OpenForms)
                if (f.GetType() == formType)
                    return f;
            return null;
        }

Open in new window

0
Comment
Question by:Abdurahman Almatrodi
  • 4
  • 3
  • 2
9 Comments
 
LVL 39

Expert Comment

by:abel
ID: 23709486
What part of your code does not work? To assign a form as MdiParent, that form muct be an MDI container. It does not work if its both a child and an MDI container and it won't work when either of the two is from a different thread (info from MSDN).

Do you get an exception and if so, where and what?
0
 
LVL 1

Author Comment

by:Abdurahman Almatrodi
ID: 23709530
The problem is not from assigning the Mdi Parent. It is in: frmNewForm frmNewForm1 = (frmNewForm)FindFormByType(typeof(frmNewForm));
 I cannot run it at all. I got this error:" The type or namespace name 'frmNewForm' could not be found (are you missing a using directive or an assembly reference?) "   
0
 
LVL 39

Expert Comment

by:abel
ID: 23709595
Aha, that makes sense. Apparently this code is situation outside of the scope of your frmNewForm. If you create a form in C# using Visual Studio it is automatically placed inside a namespace. To use the form, you should add the namespace. Either per name, or using a "using" directive.

using MyFormNamespace;

or:

MyFormNamespace.frmNewForm frmNewForm1 = (MyFormNamespace.frmNewForm)FindFormByType(typeof(MyFormNamespace.frmNewForm));

To find this namespace, go to the form itself, open de code view by double clicking it and scroll to the top of the code window. It should say somewhere in the beginning:

namespace MyFormNamespace
{
     public partial class frmNewForm : Form
     {
        .... etc


(replace "MyFormNamespace" with the name in your own code)

0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 1

Author Comment

by:Abdurahman Almatrodi
ID: 23709854
It is in same namespace. But, the error message is related to for itself. the problem is in "frmNewForm" when trying to crearte new instance frmNewForm frmNewForm1 = (frmNewForm)FindFormByType(typeof(frmNewForm)); the error occurs thanks
0
 
LVL 7

Accepted Solution

by:
keustermans earned 1000 total points
ID: 23710084
Where is your FindFormByType function located.  I suspect that you have a clas with a static method FindFormByType.

If that class is not on the same namespace as your form then you will receive that error or maybe the class is in a differnt library then you will also get that error.  I suspect you are following this route o avoid having 2 instances of the same form.

A better way IMO is to use the singleton pattern to prevent multiple instances of a form(see attached snippet)
public partial class Form1 : Form
{
     private static Form1 thisForm= null;
     public static Form1 Instance(Form parent)
     {
         If(thisForm==null)
         {
            thisForm = new Form1();
            thisForm.MdiParent = parent;
         }
         return thisForm;
     }
 
public static Form1 Instance()
     {
         If(thisForm==null)
         {
            thisForm = new Form1();
         }
         return thisForm;
     }
 
 
     public Form1()
     {
         InitializeComponent();
     }
     ......
}
 
In your MDI form you can open it like this.
 
private void menuItem1_Click(object sender, System.EventArgs e)
{
    Form f = Form1.Instance();
    f.MdiParent = this;
    f.Show();
    f.Activate();
}
 
OR
private void menuItem1_Click(object sender, System.EventArgs e)
{
    Form f = Form1.Instance(this);
    f.Show();
    f.Activate();
}

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 23710171
> the problem is in "frmNewForm"

Ah, maybe I get the culprit here. There's something odd in your opening code:

public static void TenderOpenForm(Form frmNewForm, Form frmMDIParent)
        {
            frmNewForm frmNewForm1 = (frmNewForm)FindFormByType(typeof(frmNewForm));


frmNewForm is a parameter of the static method. Then, you use that as a type. That type does not exist and as such will not compile.  The line above can never work in the way it is currently depicted.

What was your intend, to find a form based on the typename of a form?

0
 
LVL 1

Author Comment

by:Abdurahman Almatrodi
ID: 23710285
My intend is that instead of repeating such as:---------------------            frmNewForm frmNewForm1 = (frmNewForm)FindFormByType(typeof(frmNewForm));
            if (frmNewForm1 == null)
            {
                frmNewForm1 = new frmNewForm();
                frmNewForm1.MdiParent = frmMDIParent;
                frmNewForm1.Show();
            }
            else
                frmNewForm1.Focus();
-------------------- many times. I will instead use a one statement: enderOpenForm( MyNextOpenForm, TheMdiParen); I mean I want to reduce my code.  
0
 
LVL 7

Expert Comment

by:keustermans
ID: 23710374
What you are doing here is mimicking the Singleton pattern.

Implement the pattern by making the object a singleton and you will save your self headaches later.

With the implemenation that you want to use you have to loop through all open forms which is unnecessary overhead.  

With the singleton you go straight to the type that you want and get an instance of it.  (new or exisiting)
0
 
LVL 39

Assisted Solution

by:abel
abel earned 1000 total points
ID: 23711143
I have to agree with keustermans. However, if you want to use your approach, you'll have to change a couple of things:

  1. Create an Interface, call it, say, CreatableForm

  2. Put one method signature in it which return a Form. Call it CreateNewInstance()

  3. Inherit from that interface. Inside the implementation, use:

         return new Form1();  // your formname here

  4. Instead of trying to use the typeof on the form and create an instance from that (this is possible, but you'll need in-depth knowledge of the Reflection API and that might be a bit too much here), you change the signature of your TenderOpenForm as follows:

        public static void TenderOpenForm(CreatableForm frmChild, Form frmMDIParent)

  5. Inside the implementation of TenderOpenForm, you do the following:

        frmMDIParent.MdiParent = frmChild.CreateNewInstance();
Now, there's a problem with this. That is, when you would want to create a new form by passing null. That won't work anymore, so you'll have to add some code to check whether you want a new or an existing form.

Considering that code and the effort, the method of keustermans really is much simpler and will get you less headaches. You say you don't want the extra coding. If you use Visual Studio 2008, you can create an extension method on a form (the two Instance methods of keustermans) and just import them. That way you do not need to code again and again.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Integration Management Part 2
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question