在运行时创建对象 [英] create the object at runtime

查看:124
本文介绍了在运行时创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在创建一个MDI Windows应用程序,我所要创建的方法如下所示:

I am creating a MDI windows application now what I wnat is to create a method as shown below :

public void openChildForm(string s)
        {
            int flag = 0;
            foreach (Form childForm in MdiChildren)
            {
                if (childForm.Name == s)
                { flag = 1; }
            }
            if (flag == 0)
            {
                Form ob=new
                s ob = new s();
                ob.MdiParent = this;
                ob.Show();
            }
        }



单击menuItem时打开子窗体..它不起作用..

由于s是一个字符串对象.现在请在运行时帮助我创建特定的表单对象.



to open the child forms when a menuItem is clicked..its not working..

as s is a string object ..now please help me to create the specific form object at run time.

推荐答案

您需要清理一些代码方法:

There is some code you need to clean up in your method:

public void openChildForm(string s)
{
    foreach (Form childForm in MdiChildren)
    {
        if (childForm.Name == s)
        {
            childForm.MdiParent = this;
            childForm.Show();
            break;
        }
    }
    /*
    if (flag == 0)
    {
        Form ob=new           // This is complete gibberish
        s ob = new s();       // if s isn't a type this wont work, if by s you mean the string variable this is absolute nonsense.
        ob.MdiParent = this;
        ob.Show();
    }
    */
}




问候,

Manfred




Regards,

Manfred


此处提供的代码中,MdiChildren将包含具有名称属性的表单对象.如果名称属性等于"S",则显示该对象.这是我的理解.如果是这样,

Here from the code that you have provided, MdiChildren will contain form object which has name attribute. If the name attribute is equal to "S", then show that object. This is what I understood. If so then

public void openChildForm(string s)
        {
            int flag = 0;
            foreach (Form childForm in MdiChildren)
            {
                if (childForm.Name == s)
                {
                  //childForm.Show() will suffice               
                  
                   //OR
                  Form ob=new
                  s ob = new s();
                  ob.MdiParent = this;
                  ob.Show();
                  break;
                }
            }
         }


这篇关于在运行时创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆