Home > OS >  Switch between multiple Forms with different solutions in C#
Switch between multiple Forms with different solutions in C#

Time:03-29

I have a main function, which shows a Windows Form like this

myForm = new Form800x600();

myForm is declared in the head of my class:

private Form800x600 myForm;

But I have to implement a switch based on the resolution for the from, so that I can have multiple resolutions and forms: Form1024x768, Form1920x1080 and so on. I cannot work with anchors in there and I don´t want to resize it at runtime.

How can I do this, for using different functions in my forms like that: myFrom.DoThings(); when myForm can change?

Thanks

CodePudding user response:

I'm not sure I get what you mean. But I think you have to detect re resolution of screen. to do so, use Screen.PrimaryScreen.Bounds. And implement an interface in all of your forms to access to DoThings() method in all.

CodePudding user response:

If you absolutely have to, then use tuples (it's easier):

    private Form form800x600; // replace Form with Form800x600
    private Form form1024x768; // replace Form with Form1024x768
    private Form form1920x1080; // replace Form with Form1920x1000
    private Form form3840x2160; // replace Form with Form3840x2160

    public Form1()
    {
        InitializeComponent();
        SizeAtStartup();
    }

    public void SizeAtStartup()
    {
        (int width, int height) = (Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);

        switch (width, height)
        {
            case (800, 600):
                form800x600 = new Form(); // replace Form with Form800x600
                form800x600.Show();
                break;
            case (1024, 768):
                form1024x768 = new Form(); // replace Form with Form1024x768
                form1024x768.Show();
                break;
            case (1920, 1080):
                form1920x1080 = new Form(); // replace Form with Form1920x1080
                form1920x1080.Show();
                break;
            case (3840, 2160):
                form3840x2160 = new Form(); // replace Form with Form3840x2160
                form3840x2160.Show();
                break;
            default:
                break;
        }
    }
  • Related