简体   繁体   English

如何从另一个类在Form1中创建对象?

[英]How to create object in Form1 from another class?

I am trying to write program with C# to dynamicaly create PictureBox'es from another class. 我试图用C#编写程序来动态地从另一个类创建PictureBox。

Here what is done: 完成以下操作:

In class Form1 creating object of another class and call procedure in that class which creates objects(many PictureBox'es) I want 在类Form1中创建另一个类的对象,并在该类中创建我想要的对象(许多PictureBox'es)的调用过程

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 Lines_online
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //trying to draw NxM table 
            //where N is horizontal cell count M is vertical cell count
            Table tbl = new Table();
            tbl.Tablenm(9, 9);
        }
    }
}

The class Table code: 类表代码:

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

namespace Lines_online
{
   public class Table : Form1
    {
        List<PictureBox> cell = new List<PictureBox>(); 
        public void Tablenm(int n, int m)
        {
            for (int i = 0; i < n*m; i++)
            {
                PictureBox pict = new PictureBox();
                cell.Add(pict);
                cell[i].Size = new Size(20, 20);
                cell[i].Location = new Point(30 + (i % n) * 19, 30 + (i / m) * 19);
                cell[i].Image = Properties.Resources.lang20x20;
                Controls.Add(cell[i]);
            }
        }
    }
}

Then code is executed it does nothing and gives no errors (I think it creates the objects somethere, but definetaly not in Form1 window). 然后执行代码,它什么也不做,也不给出任何错误(我认为它会在某处创建对象,但不是在Form1窗口中定义)。 If procedure in the class Table moved to Form1 class it works perfectly, but I want to control it from other class to reduce code size in Form1. 如果将类Table中的过程移至Form1类,则它可以正常工作,但我想从其他类进行控制以减小Form1中的代码大小。 I tried to create Form1 object in Class Table: 我试图在类表中创建Form1对象:

Form1 frm1 = new Form1();

It did not help. 它没有帮助。 What I am doing wrong, or maybe using bad approach to this problam? 我在做错什么,或者对这个问题使用不好的方法?

Note: in your code you are inheriting table from Form1. 注意:在您的代码中,您将从Form1继承表。 This does mean that Table represents a new form, instead of a reference to the current existing form, and therefore it doesn't do anything. 这确实意味着Table代表了一种新的形式,而不是对当前现有形式的引用,因此它什么也不做。

In the Controls property of your original form, you are just adding another form, which definitely isn't the behaviour that you need. 在原始表单的Controls属性中,您只是添加了另一种表单,这绝对不是您需要的行为。

I've tooked your code as a start and adapted it a bit. 我以您的代码为起点并对其进行了一些修改。 I do think that this should work. 我确实认为这应该起作用。

Create your class Table, but DON'T inherit from Form1. 创建您的类Table,但不要继承Form1。 Make your class that it looks like the following: 使您的课程看起来像以下内容:

public class Table
{
    public List<PictureBox> Render(int n, int m)
    {
        List<PictureBox> returnList = new List<PictureBox>();

        for (int i = 0; i < n*m; i++)
        {
            PictureBox pict = new PictureBox();

            pict.Size = new Size(20, 20);
            pict.Location = new Point(30 + (i % n) * 19, 30 + (i / m) * 19);
            pict.Image = Properties.Resources.lang20x20;

            returnList.Add(pict);
        }

        return returnList;
    }
}

Then, in your main form, you can call your method: 然后,以您的主要形式,可以调用您的方法:

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

    private void Form1_Load(object sender, EventArgs e)
    {
        var table = new Table();
        IEnumerable<Control> controls = table.Render(10, 10);

        foreach (var control in controls)
        { this.Controls.Add(control); }
    }
}

I believe that should work, but I haven't tested it. 我认为应该可以,但是我尚未对其进行测试。 Also note that this code is not optimized. 另请注意,此代码未优化。 You should have parameters passed to the function in order to make it more generic. 您应该将参数传递给函数,以使其更通用。

You can return the List<PictureBox> to Form1 您可以将List<PictureBox>返回到Form1

 public List<PictureBox> Tablenm(int n, int m)
 {
        for (int i = 0; i < n*m; i++)
        {
            PictureBox pict = new PictureBox();
            cell.Add(pict);
            cell[i].Size = new Size(20, 20);
            cell[i].Location = new Point(30 + (i % n) * 19, 30 + (i / m) * 19);
            cell[i].Image = Properties.Resources.lang20x20;
        }

        return cell;
 }

Then add it inside you form: 然后将其添加到表单中:

 private void Form1_Load(object sender, EventArgs e)
 {
        //trying to draw NxM table 
        //where N is horizontal cell count M is vertical cell count
        Table tbl = new Table();
        var pictureBoxes = tbl.Tablenm(9, 9)
        foreach (var pictureBox in pictureBoxes)
        {
          Controls.Add(picturebox)}
        }
 }

//You need to add picturebox control to form1 control in loop, //Form1.Control.Add(cell[i]); //需要将图片框控件添加到循环中的form1控件中,//Form1.Control.Add(cell[i]);

//------------------- // -------------------

public void Tablenm(int n, int m, Form Form1)
        {
            for (int i = 0; i < n*m; i++)
            {
                PictureBox pict = new PictureBox();
                cell.Add(pict);
                cell[i].Size = new Size(20, 20);
                cell[i].Location = new Point(30 + (i % n) * 19, 30 + (i / m) * 19);
                cell[i].Image = Properties.Resources.lang20x20;                
                Form1.Control.Add(cell[i]);
            }
        }

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

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