使用动态文本框填充锯齿状数组

时间:2015-05-17 23:04:32

标签: c#

我为学校制作了一个线性方程求解器程序,我已经找到了一个有效的算法,但它使用了锯齿状数组。

我需要你的帮助来制作充满锯齿状阵列的动态文本框。例如:1x+1y=32x+1y=4会进入{1,1,3}{2,1,4}数组。这是我的代码,它不起作用。

TextBox[][] tb = new TextBox[n][];
  for (int i = 0; i < n; i++)
    for (int j = 1; j < n+1; j++) {
      tb[i][j] = new TextBox();
      tb[i][j].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
      tb[i][j].Left = 36 * j + 10;
      tb[i][j].Top = 36 * i + 10;
      tb[i][j].Width = 35;
      tb[i][j].Font = new Font(tb[i][j].Font.FontFamily, 16);
      tb[i][j].BackColor = Color.Cyan;
      tb[i][j].TextAlign = HorizontalAlignment.Center;
    }

1 个答案:

答案 0 :(得分:0)

Dummy project preview

我创建了一个虚拟项目(如图所示)并添加了一个Panel。 Panel将包含您动态生成的文本框。我还更新了背后的代码。虽然您创建了文本框,但您从未将其添加到任何现有控件中。他们只是存在。 现在背后的代码如下所示:

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

    int numVars = 2;

    // You'll eventually need to obtain the values in the boxes
    TextBox[][] tb;

    private void button1_Click(object sender, EventArgs e)
    {
        // Remove existing controls
        this.panel1.Controls.Clear();

        // Obtain number of variables
        numVars = (int)numericUpDown1.Value;

        // Create the TextBoxes
        tb = new TextBox[numVars][];

        // Initialize each jagged array
        for(int i = 0; i < numVars; i++)
            tb[i] = new TextBox[numVars + 1];

        // Create the Textboxes
        int height = 20;
        int width = 50;
        int curX = 10;
        int curY = 10;
        for(int i = 0; i < numVars; i++)
        {
            for(int j = 0; j < numVars + 1; j++)
            {
                TextBox txtbox = new TextBox();
                txtbox = new TextBox();
                txtbox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                txtbox.Left = curX;
                txtbox.Top = curY;
                txtbox.Width = width;
                txtbox.Height = height;
                txtbox.Font = new Font(txtbox.Font.FontFamily, 16);
                txtbox.BackColor = Color.Cyan;
                txtbox.TextAlign = HorizontalAlignment.Center;

                tb[i][j] = txtbox;
                this.panel1.Controls.Add(tb[i][j]); // Add as a child of panel

                curX += width + 15;
            }

            curX = 10;
            curY = curY + height + 20;
        }
    }
}

使用值6运行它会产生:enter image description here