only 1 instance of a form to exist while it has a parent form?

Joined
Sep 26, 2022
Messages
16
Programming Experience
1-3
I have a form application that when clicking a menustrip button, it opens an instance of a form. I made this newly opened form a child of other form to make the newly opened form(child) to stay in the borders of this(parent) form.

I used form.MdiParent so newly opened form has a parent. If I try to use form.ShowDialog(), rather than form.Show(), it gives an error and says that

"Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog. "
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ODEVCollectionListWinFormsUygulama
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        public List<string> musteriler1 = new List<string>();
        private void müşteriGirişiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form3 form3 = new Form3(musteriler1);
            form3.MdiParent = this;
            form3.Show();
        }

        private void müşteriListelemeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form4 form4 = new Form4(musteriler1);          
            form4.MdiParent = this;
            form4.Show();
        }
    }
}
How can I solve this?
 
Last edited:
You should rename your controls/forms after you make them. By the time you've got to the teens, it'll be hard to keep track of which form does what

Rather than blindly constantly making a new form, query how many of that form you have open already and don't open another if there is an existing one, for example:

C#:
private void müşteriGirişiToolStripMenuItem_Click(object sender, EventArgs e)
{
  if (Application.OpenForms.OfType<Form3>().Any()) return;
 
Last edited:
Yeah, but i like to debug code that I can understand, and filling code with names like Form3, TextBox56 is essentially obfuscating it
 
This is very irrelevant to what I ask here.
If we wait until you ask a question to which that advice is relevant, we'll be waiting forever and you'll never understand that you're doing the wrong thing and why. If we think that we have information that will help you be a better developer then we're going to provide it. If you don't want to follow our advice then you don't have to but complain about it seems a bit much. Also, if you keep posting code that we find difficult to read, we may eventually just give up and you won't get the help you need. If you want us to help you then maybe you could help us do that.
 
As for your question, this is a case of ignoring the logic that you use every day. If you only wanted one bottle of milk in the fridge at a time, what would you do? You would look in the fridge first to see whether there was already a bottle of milk there, then only get a new one if there wasn't, right? Why should this be any different? Look at the group of forms already open and see if there's one of that type and only create a new one if there isn't. You may not know exactly how to do that but you should be able to determine that logic at least, so your question should then be much more specific.
 
That said, there is another way. You can actually make that form class a singleton, so it won't all more than one instance to exist at a time.
C#:
public partial class SingletonForm : Form
{
    private SingletonForm()
    {
        InitializeComponent();
    }

    private static SingletonForm _instance;

    public static SingletonForm Instance
    {
        get
        {
            if (_instance?.IsDisposed != false)
            {
                _instance = new SingletonForm();
            }

            return _instance;
        }
    }
}
Now you can refer to SingletonForm.Instance everywhere and it will always refer to the existing instance if there is one and it hasn't been disposed, otherwise it will create a new one and refer to that.
 
Back
Top Bottom