开发者

C# call MDI child from child form

开发者 https://www.devze.com 2023-02-20 07:19 出处:网络
I have these forms: MainScreen - MDI container DataBaseOutput - child NewAnime - child DataBaseOutput has a tab control that holds datagrids, each for different tables. I use an access database.

I have these forms:

MainScreen - MDI container

DataBaseOutput - child

NewAnime - child

DataBaseOutput has a tab control that holds datagrids, each for different tables. I use an access database.

In those tabs, there is a menustrip, where the functions "New", "Edit", "Delete" etc. will be called from. Now when I'm on the first tab's menustrip and click on "New" I want to open the form "NewAnime", inside the MDI container. This however, is not working as I planned. At first I tried to just call it from the childform (DataBaseOutput). This resulted in opening a new form instead of a child. when I made it a child it was not showing up.

Then I triend lots of things, but I still haven't figured it out.

This is the current code for calling the form. It calls the form 开发者_运维技巧with a method in the main form:

private void NewAnime_Click(object sender, EventArgs e)
{
    MainScreen main = new MainScreen();
    main.mShowForm(2);

    this.Close();
}

Method in the main form:

// Forms for MDI Parent
DataBaseOutput OutputForm = new DataBaseOutput();
NewAnime AddAnime = new NewAnime();

// How i made them childs (this is at the InitializeComponent(); part)
OutputForm.MdiParent = this;
AddAnime.MdiParent = this;

public void mShowForm(int formnumber)
{
    switch (formnumber)
    {
        case 1: OutputForm.Show(); break;
        case 2: AddAnime.Show(); break;
    }
}

Does anyone have a clue of what i'm doing wrong and maybe has a better idea? This might be a bit too much working around, but as I said, it's my first time using MDI forms and i'm just trying to get it to work.


Have you set the MainForm to be an MDIContainer? To do this set its IsMdiContainer property to true; Also check it has File and Window top-level menu items and New and Close menu items. (The tutorial suggests this, I know it should have a Window menu item at least).

Have a look at this tutorial for more guidance: Creating MDI Child Forms (MSDN)

EDIT: Looking at it more closely, it seems you are creating a new instance of MainForm, and trying to show the form as a child of that instance, as opposed to showing it in the existing MainForm. I assume you already have an instance of MainForm open at this point? And assuming the OutputForm and AddAnime forms are children of MainForm you could call the existing instance's method like this:

private void NewAnime_Click(object sender, EventArgs e)
{
    this.ParentForm.mShowForm(2);
    this.Close();
}

but ideally you should have an event on DataBaseOutput that MainForm listens to, and shows the new Form when the event is raised. See here for more info (it talks about user controls and not child forms, but the principle is the same):

Calling parent form functions from a user control

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号