Method to automate the call of Forms in C#

Asked

Viewed 460 times

2

Here’s what I’m trying to do

I am creating an application in C#, Mainframe is MDI Father, and I have other windows too. What I want is to use a class called "Formcaller" to call other Forms automatically, so I don’t have to always 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’m trying to do is call a new form, type MDI CHILD, using a custom method:

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

I don’t know if this is possible in C#, I’ve done the same in JAVA and it worked, can anyone tell me if there is any known way to do this?

2 answers

0

Well, if I understand you right, you want to call forms and put them as children of Mdiparent Formcaller. I did something similar, but before, let’s put some stitches...

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

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

  1. I created an interface that forces all Mdiparent know how to invoke your children (in my case, by index);
  2. I implemented the interface in an abstract class that would execute the desired patterns (in this case, invoke forms);
  3. 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... Just need to create the switch... I hope I’ve helped in some way :)

0

Well, I don’t 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 project’s DLL. 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());
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.