如何在几个表单的单独 class 中动态显示定义数量的文本框?

How to display the defined amount of textboxes dynamically in separate class for few forms?

我有 SQLite 数据库,其中有一个 table 有一定数量的行。我的任务是在考虑这些控件的坐标的情况下以几种形式显示相同数量的文本框。这些文本框的名称应取自此 table 的其中一列。标签也必须存在。他们的名字应该相似。考虑到 table 这一列的值是西里尔文,我决定在 class 解密器中将其翻译成拉丁文符号。

我找到了以一种形式执行显示必要文本框的重点的解决方案,并且我能够将此代码复制到其他形式。但这似乎不是个好主意,尽管它在这种情况下会起作用。

我创建了新的 class - CommunicationCanalsDrawing。这个class的想法是减少代码量,增加代码的使用方便性。我的特定代码 class:

class CommunicationCanalsDrawing
{
    public int Tx { get; set; }
    public int Ty { get; set; }
    public int Lx { get; set; }
    public int Ly { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }

    public string[] Labels;
    private string[] Textboxes;

    public TextBox txt = new TextBox();
    public Label l = new Label();

    public int i { get; set; }

    public void DrawThat()
    {
        Ly += 32;
        Ty += 32;
        Decryptor decr = new Decryptor();
        decr.Word = Labels[i];
        Textboxes[i] = decr.Decrypt();
        txt = new TextBox();
        txt.Name = Textboxes[i];
        txt.Text = "";
        txt.Width = Width;
        txt.Height = Height;
        txt.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
            System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
            ((byte)(204)));
        txt.Location = new System.Drawing.Point(Tx, Ty);

        l = new Label();
        l.Name = "l_" + Textboxes[i];
        l.Text = Labels[i];
        l.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
            System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
            ((byte)(204)));
        l.Location = new System.Drawing.Point(Lx, Ly);
    }

    public void ListOfColumns()
    {
        SQLiteConnection m_dbConn = new SQLiteConnection();
        SQLiteCommand m_sqlCmd = new SQLiteCommand();
        int AmountTextbox = 0;
        try
        {
            InceptDb cl = new InceptDb();
            m_dbConn = new SQLiteConnection("Data Source=" +
                cl.dbFileName + ";Version=3;");
            m_dbConn.Open();
            m_sqlCmd.Connection = m_dbConn;

            m_sqlCmd.CommandText = "SELECT COUNT(*) FROM communication_remedies";
            AmountTextbox = Convert.ToInt32(m_sqlCmd.ExecuteScalar().ToString());
        }
        catch (SQLiteException ex)
        {
            MessageBox.Show("Error count*: " + ex.Message);
        }
        m_dbConn.Close();

        if (AmountTextbox == 0)
        {
            MessageBox.Show("Довідник каналів зв'язку пустий.");
            Organizations o = new Organizations();
            o.Close();
        }

        m_dbConn = new SQLiteConnection();
        m_sqlCmd = new SQLiteCommand();

        Labels = new string[AmountTextbox];
        Textboxes = new string[AmountTextbox];

        try
        {
            InceptDb cl = new InceptDb();
            m_dbConn = new SQLiteConnection("Data Source=" +
                cl.dbFileName + ";Version=3;");
            m_dbConn.Open();
            m_sqlCmd.Connection = m_dbConn;

            m_sqlCmd.CommandText = "SELECT name FROM communication_remedies";

            DataTable dt = new DataTable();
            SQLiteDataAdapter adapter = new SQLiteDataAdapter(m_sqlCmd.CommandText,
                m_dbConn);
            adapter.Fill(dt);

            Labels =
                dt.AsEnumerable().Select(r => r.Field<string>("name")).ToArray();
        }
        catch (SQLiteException ex)
        {
            MessageBox.Show("Error count*: " + ex.Message);
        }
        m_dbConn.Close();
        }
        }

我的某种形式的代码:

private void Organizations_Load(object sender, EventArgs e)
    {
        CommunicationCanalsDrawing comm = new CommunicationCanalsDrawing();

        comm.ListOfColumns();

        comm.Lx = 12;
        comm.Tx = 243;
        comm.Ty = 173;
        comm.Ly = 207;
        comm.Height = 26;
        comm.Width = 228;
        for (int i = 0; i < comm.Labels.Length; i++ )
        {
            comm.i = i;
            comm.DrawThat();
            this.Controls.Add(comm.txt);
            this.Controls.Add(comm.l);
        }
    }

我知道控件的坐标可能不正确,但现在没关系了。很容易纠正。我只看到最后一个文本框和最后一个标签。其他不显示。

您的问题是您只创建了一次 CommunicationCanalsDrawing 实例,然后在其属性上执行 for 循环。这样你最终只会得到最后一个。您需要在 for 循环的每个循环中创建一个 CommunicationCanalsDrawing 的新实例:

private void Organizations_Load(object sender, EventArgs e)
    {
     for (int i = 0; i < comm.Labels.Length; i++ )
     {
        CommunicationCanalsDrawing comm = new CommunicationCanalsDrawing();

        comm.ListOfColumns();

        comm.Lx = 12;
        comm.Tx = 243;
        comm.Ty = 173;
        comm.Ly = 207;
        comm.Height = 26;
        comm.Width = 228;
        
            comm.i = i;
            comm.DrawThat();
            this.Controls.Add(comm.txt);
            this.Controls.Add(comm.l);
        }
    }