Method to automate the call of Forms in C #

0

Here's what I'm trying to do

I'm creating an application in C #, MainFrame is the MDI Parent, and I own other windows as well. What I want is to use a class called "FormCaller" to call other Forms automatically, so I do not always have to write the same thing.

Here is the code:

namespace StudentManager
{
class FormCaller
{


    public static MainFrame MAIN = new MainFrame();
    public static CadastrarAluno REG = new CadastrarAluno();



    public void CallForm(Form window)
    {
        window.MdiParent = MAIN;
        window.Show();
    }

}
}

So, what I am trying to do is to call a new Form, type MDI CHILD, using a custom method:

FormCaller caller;
private void method(...) {
   caller.CallForm(FormCaller.CAD);
}

I do not know if this is possible in C #, I already did the same in JAVA and it worked, can someone tell me if there is any known way to do this?

    
asked by anonymous 02.03.2018 / 16:27

2 answers

0

Well, if I get it right, you want to call forms and put them as children of MDIParent FormCaller. I did something similar, but before, let's put some points ...

The way you're suggesting: caller.CallForm(anotherForm) , requires the FormCaller class to "know" all of the forms that will be invoked. See if I can help you get some idea with the implementation below ...

To solve the problem, I had created a class / enumeration just to describe the child forms of MDI, but the maintenance was very constant. I then decided to change strategy and create a pattern that could be reused by any other MDIParent.

  
  • I have created an interface that forces all MDIParent know how to invoke their children (in my case, by index);
  •   
  • Implemented the interface in an abstract class that would execute the desired patterns (in this case, invoking forms);
  •   
  • Inherit the abstract class in any target MDIParent.
  •   

    Something like:

    public interface IMdiContainer
    {
        Form GetCurrentForm(int currentStep);
    }
    
    public abstract class MdiContainerBase 
    {
    
        private IMdiContainer _container;
        protected int index;
    
        public MdiContainerBase(IMdiContainer container)
        {
            _container = container;
        }
    
        protected override void OnShow(EventArgs e)
        {
            base.OnShow(e);
    
            // Call mdiChild from mdiContainer
            ShowMdiChild(_container.GetCurrentForm(index));
        }
    
        protected override void NextForm()
        {
            ShowMdiChild(_container.GetCurrentForm(++index));
        }
    
        protected override void PrevForm()
        {
            ShowMdiChild(_container.GetCurrentForm(--index));
        }
    
        private void ShowMdiChild(Form form)
        {
            form.MdiParent = this;
            form.Show();
        }
    
        // and so on...
    }
    
    public class MyCustomMdiContainer : MdiContainerBase, IMdiContainer
    {
        public MyCustomMdiContainer() : base(this)
        {
        }
    
        // implementation forced by interface
        public Form GetForm(int index)
        {
            switch(index)
            {
                case 1:
                    return new Form1();
    
                case 2:
                    return new Form2();
    
                default:
                    return null;
            }
        }
    }
    

    So you can create everything you need in the interface ... You just need to create the switch ... I hope I have helped in some way:)

        
    13.03.2018 / 19:46
    0

    Well, I did not understand exactly what you want to do. The forms you will call will be explicit form ?, ie:

    FormCadastro novoform = new FormCadastro():
    

    Or do you still not know which form will be called? If this is the case, you can use CreateInstance. In this case, the forms must be in the DLL of the project. using System;

    class DynamicInstanceList
    {
        private static string instanceSpec = "System.EventArgs;System.Random;" +
            "System.Exception;System.Object;System.Version";
    
        public static void Main()
        {
            string[] instances = instanceSpec.Split(';');
            Array instlist = Array.CreateInstance(typeof(object), instances.Length);
            object item;
            for (int i = 0; i < instances.Length; i++)
            {
                // create the object from the specification string
                Console.WriteLine("Creating instance of: {0}", instances[i]);
                item = Activator.CreateInstance(Type.GetType(instances[i]));
                instlist.SetValue(item, i);
            }
            Console.WriteLine("\nObjects and their default values:\n");
            foreach (object o in instlist)
            {
                Console.WriteLine("Type:     {0}\nValue:    {1}\nHashCode: {2}\n",
                    o.GetType().FullName, o.ToString(), o.GetHashCode());
            }
        }
    }
    
        
    20.03.2018 / 15:43