简体   繁体   English

C#从子表格调用MDI孩子

[英]C# call MDI child from child form

I have these forms: 我有这些形式:

MainScreen - MDI container MainScreen - MDI容器
DataBaseOutput - child DataBaseOutput - child
NewAnime - child NewAnime - 孩子

DataBaseOutput has a tab control that holds datagrids, each for different tables. DataBaseOutput有一个tab控件,用于保存datagrids,每个datagrids用于不同的表。 I use an access database. 我使用访问数据库。

In those tabs, there is a menustrip, where the functions "New", "Edit", "Delete" etc. will be called from. 在这些标签中,有一个menustrip,其中将调用“新”,“编辑”,“删除”等功能。 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. 现在,当我在第一个选项卡的menustrip上并单击“新建”时,我想在MDI容器内打开“NewAnime”表单。 This however, is not working as I planned. 然而,这不符合我的计划。 At first I tried to just call it from the childform (DataBaseOutput). 起初我试图从子窗体(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. 这可能有点太麻烦,但正如我所说,这是我第一次使用MDI表单而我只是想让它运行起来。

Have you set the MainForm to be an MDIContainer ? 您是否将MainForm设置为MDIContainer To do this set its IsMdiContainer property to true; 为此,请将其IsMdiContainer属性设置为true; Also check it has File and Window top-level menu items and New and Close menu items. 还要检查它是否包含FileWindow顶级菜单项以及NewClose菜单项。 (The tutorial suggests this, I know it should have a Window menu item at least). (教程建议这一点,我知道它至少应该有一个Window菜单项)。

Have a look at this tutorial for more guidance: Creating MDI Child Forms (MSDN) 有关更多指导,请查看本教程: 创建MDI子窗体(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. 编辑:更仔细地看,它似乎是你正在创建一个新的MainForm实例,并试图将该表单显示为该实例的子项,而不是在现有的MainForm中显示它。 I assume you already have an instance of MainForm open at this point? 我假设你已经有一个MainForm实例打开了吗? And assuming the OutputForm and AddAnime forms are children of MainForm you could call the existing instance's method like this: 假设OutputForm和AddAnime表单是MainForm的子表单,您可以像这样调用现有实例的方法:

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. 但理想情况下,您应该在MainForm侦听的DataBaseOutput上有一个事件,并在引发事件时显示新的Form。 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 从用户控件调用父表单函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM