Windows窗体类构造函数问题 [英] windows form class constructor issue

查看:69
本文介绍了Windows窗体类构造函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,

我有两个Windows窗体1)form1 2)form2.我想以两种方式打开Form 2.第一种方法是通过没有参数的class2构造函数单独打开form2名称(form2 frm2 = new form2()).2种方式是form1具有id值,该id值将通过一个参数构造函数传递给form2.like(form2 frm2 = new form2(id)).问题是每当加载每个构造器时,form2中的控件就被初始化.问题在内存中由于此过程,管理所有控件已被初始化两次,因此可以为单个控件分配两次内存.我想通过一次初始化两个构造器的组件来避免这种情况,有什么办法可以解决这个问题吗? ="h2_lin">解决方案



如果要创建Form2的新实例,则将单独分配内存.如果只想创建Form2的单个实例,则需要使用singleton类.

检查简单Singleton表单 [使用System.Data; 使用使用System.Drawing; 使用 System.Linq; 使用 System.Text; 使用使用System.Windows.Forms; 命名空间 WindowsFormsApplication2 { 公共 部分 class Form1:表单 { 公共 Form1() { InitializeComponent(); } 私有 无效 Form1_Load(对象发​​件人,EventArgs e) { } 私有 无效 button1_Click(对象发​​件人,EventArgs e) { Form2 f = Form2(); f.Show(); } } }



//表格2//

 使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用使用System.Drawing;
使用 System.Linq;
使用 System.Text;
使用使用System.Windows.Forms;

命名空间 WindowsFormsApplication2
{
    公共 部分  class  Form2:Form1
    {
        公共 Form2()
        {
            InitializeComponent();
        }

        私有 无效 Form2_Load(对象发​​件人,EventArgs e)
        {
            .Text = " ;
  
        }
    }
} 


Hi friends,

i have two windows form 1)form1 2)form2.I want to open form 2 in two ways.1st way is independently open throgh the form2 name with no argument pass class constructor(form2 frm2=new form2()).2 nd way is form1 has id value that will pass to form2 through one argument constructor.like( form2 frm2=new form2(id)).the issue is whenever the each constrctor is loaded, the controls in the form2 is intialized.the question is in memory management all the controls has been initialized twice due to this process.so that memory could be allcoated twice for a single controls.i want to avoid it by one time intializecomponent for both constrctors.Is there any way to solve this issue?

Hi,

if you are creating new instance of the Form2 then memory will be allocated separately. if you want to create only single instance of the Form2 then you need to use singleton class.

Check
Simple Singleton Forms[^] code project article.

Hope this helps you,

Thanks
-Amit Gajjar


Try This

//Form 1//

sing System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2(); f.Show();
        }
    }
}



//Form 2//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form2 : Form1
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
           this.Text="I am Form 2- You don't need to design me";
  
        }
    }
}


这篇关于Windows窗体类构造函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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