-3

Can anyone help me to create dynamically, multidimensional array of textboxes on button click.Thanks!

mando
  • 1
  • 2
    Have you tried I don't know... anything at all? Because it might be the case (correct if I am wrong) that actually creating the multidimensional arrays might be the solution you are looking for? I mean rather than asking about how such a thing can be done, actually doing it? – varocarbas Nov 16 '15 at 19:35
  • why can't you create a `List` can you explain why you need a multi dimensional array – MethodMan Nov 16 '15 at 19:35
  • 4
    I'm voting to close this question as off-topic because SO is not a code-writing service. – xxbbcc Nov 16 '15 at 19:36
  • `Textbox[,] mdaTxt = new Textbox[dimension1,dimension2]` ? – sab669 Nov 16 '15 at 19:37
  • Possible duplicate of [How do I create a multidimensional array of objects in c#](http://stackoverflow.com/questions/11907069/how-do-i-create-a-multidimensional-array-of-objects-in-c-sharp) – sab669 Nov 16 '15 at 19:38
  • TextBox[,] textBox = new TextBox[3,3]; for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ textBox[i,j] = new TextBox(); } } what else i should add to create matrix of textboxes ....is there something with setPosition of textBox or something? – mando Nov 16 '15 at 20:30

1 Answers1

1

Try this

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

        const int ROW_TEXTBOX = 5;
        const int COL_TEXTBOX = 6;
        const int TEXTBOX_WIDTH = 100;
        const int TEXTBOX_HEIGHT = 30;
        const int SPACING = 20;
        List<List<TextBox>> textboxes = new List<List<TextBox>>();

        private void button1_Click(object sender, EventArgs e)
        {
            for (int row = 0; row < ROW_TEXTBOX; row++)
            {
                List<TextBox> newRow = new List<TextBox>();
                textboxes.Add(newRow);
                for (int col = 0; col < COL_TEXTBOX; col++)
                {
                    TextBox newbox = new TextBox();
                    newbox.Width = TEXTBOX_WIDTH;
                    newbox.Height = TEXTBOX_HEIGHT;
                    newbox.Top = (row * (TEXTBOX_HEIGHT + SPACING)) + SPACING;
                    newbox.Left = (col * (TEXTBOX_WIDTH + SPACING)) + SPACING;
                    newRow.Add(newbox);
                    this.Controls.Add(newbox);
                }
            }
        }
    }
}
​
jdweng
  • 33,250
  • 2
  • 15
  • 20