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

本文关键字:Form1 创建对象 另一个 | 更新日期: 2023-09-27 18:05:04

我正在尝试用c#编写程序来动态地从另一个类创建PictureBox'es。

操作如下:

在类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);
        }
    }
}

类表代码:

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]);
            }
        }
    }
}

然后代码被执行,它不做任何事情,也没有给出任何错误(我认为它在某处创建对象,但绝对不是在Form1窗口)。如果类表中的过程移动到Form1类,它可以完美地工作,但我想从其他类控制它,以减少Form1中的代码大小。我尝试在类表中创建Form1对象:

Form1 frm1 = new Form1();

它没有帮助。我做错了什么,或者用了不好的方法来解决这个问题?

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

注意:在您的代码中,您从Form1继承表。这意味着Table代表一个新表单,而不是对当前存在表单的引用,因此它不做任何事情。

在原始表单的Controls属性中,您只是添加了另一个表单,这绝对不是您需要的行为。

我已经把你的代码作为一个开始,并调整了一点。我认为这应该行得通。

创建你的类Table,但不要继承Form1。让你的类看起来像这样:

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;
    }
}
然后,在主窗体中,你可以调用你的方法:
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); }
    }
}

我相信这应该有效,但我还没有测试过。还要注意,这段代码没有经过优化。您应该将参数传递给该函数,以使其更通用。

//你需要添加picturebox控件到form1控件的循环中,//Form1.Control.Add(细胞[我]);

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

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]);
            }
        }

可以将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;
 }

然后添加到你的表单中:

 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)}
        }
 }