如何制作TextBox的2D列表?

时间:2014-01-13 04:30:20

标签: c# list

private List<TextBox>[,] textBoxesList = new List<TextBox>[6,3]; // would be my list soon
private TextBox[,] textBoxes = new TextBox[6,3]; // my array of text boxes

public Form1()
{
    InitializeComponent();

    for (int j = 0; j < textBoxes.Length(0); j++)
    {
        for (int i = 0; i < textBoxesList.GetLength(1); i++)
        {
            textBoxes[j,i] = new TextBox();
            textBoxes[j,i].Size = new Size(35, 20);
            textBoxes[j,i].Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
            textBoxes[i].Location = new System.Drawing.Point(90 + j * 50, 50 + i * 30);

            textBoxes[j,i].Parent = this;
            this.Controls.Add(textBoxes[j,i]);
        }
    }
}

所以这就是我用数组做的。现在我对名单缺乏经验。事实上,我之前从未学习过列表,但是他们简单地听说过它们可以与数组相比增长/缩小。使用列表对我来说很有用,这样用户可以稍后添加一行。


测试了这段代码,但它显示为空白。

public partial class Form1 : Form
{
    //List<List<TextBox>> li = new List<List<TextBox>>();  
    List<TextBox> litxt = new List<TextBox>();

    public Form1()
    {
        InitializeComponent();
        TextBox txt = new TextBox();
        txt.Size = new Size(35, 20);
        litxt.Add(txt);

    }

    private void button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 3; i++)
        {
            TextBox txt = new TextBox();
            litxt.Add(txt);
        }
    }
}

4 个答案:

答案 0 :(得分:1)

对于2D列表,请执行此操作

List<List<TextBox>> li = new List<List<TextBox>>();    

由于您需要制作一个文本框表格,因此使用固定宽度等于3个文本框的FlowLayoutPanel

列出清单

List<TextBox> litxt = new List<TextBox>();

将3个文本框添加到litxt

for(int i = 0;i<3;i++)
{
TextBox txt = new TextBox();
txt.Size = new Size(35,20);
litxt.Add(txt);
}  

将此代码置于您要添加3个TextBox

的条件下
foreach(TextBox txt in litxt)
{
FlowLayoutPanel.Controls.Add(txt);
}    

What it should look like

放置此代码

向表单添加flowlayout面板并执行此代码

        public Rough()
        {
            InitializeComponent();
        }

        private void Rough_Load(object sender, EventArgs e)
        {

        }
        static int i = 0;
        private void button1_Click(object sender, EventArgs e)
        {
           for(int j = 0; j < 3 ; i++,j++)
           {
               TextBox txt = new TextBox();
               txt.Size = new Size(35, 20);
               txt.Name = i.ToString();
               flowLayoutPanel1.Controls.Add(txt);
            }   
        }

答案 1 :(得分:1)

您可以使用通用Dictionary并创建自己的密钥结构来访问元素。

关键结构MyKey

public struct MyKey
{
    private ushort row;
    private ushort col;

    public int Row { get { return row; } }
    public int Col { get { return col; } }

    public MyKey(int row, int col)
    {
        // check if keys are in range between 0 and ushort.MaxValue
        if(row < 0 || row > ushort.MaxValue || col < 0 || col > ushort.MaxValue)
            throw new ArgumentOutOfRangeException(string.Format("Arguments row and col cannot be less than 0 or greater than {0}.", ushort.MaxValue));
        this.row = (ushort) row;
        this.col = (ushort) col;
    }

    // Overriden GetHashCode() that's used by the Dictionary to search through the keys.
    public override int GetHashCode()
    {
        // we just shift Row by 16 Bits to left and make a bitwise or with Col to generate the Hashcode
        return ((int) row << 16) | col;
    }
}

现在您可以将此密钥用于Dictionary

var textBoxesList = new Dictionary<MyKey, TextBox>();
var rowCount = 6;
var colCount = 3;

for (int row = 0; row < rowCount; row++)
{
    for (int col = 0; col < colCount; col++)
    {
        var key = new MyKey(row, col);
        var textBox = new TextBox();

        textBox.Size = new Size(35, 20);
        textBox.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
        textBox.Location = new System.Drawing.Point(90 + row * 50, 50 + col * 30);
        textBox.Parent = this;

        // store the key in the Tag property of the TextBox
        // so you can get the row and the column of your textbox with
        // var key = (MyKey) textBox.Tag
        textBox.Tag = key; 

        textBoxesList.Add(key, textBox);

        this.Controls.Add(textBox);
    }
}

如果你想通过第2行和第4列获得TextBox,你可以写:

var textbox = textBoxesList[new MyKey(row: 2, col: 4)];

你也可以走过这个系列:

foreach (var item in textBoxesList)
{
    var row = item.Key.Row;
    var col = item.Key.Col;
    var textbox = item.Value;
    // ...
}

例如,您可以在textBoxesList中搜索特定行中的TextBox。假设我们想要按行列号排序第2行中的所有文本框:

var textBoxesInRow2 = (from item in textBoxesList
                        where item.Key.Row == 2
                        orderby item.Key.Col
                        select item.Value)
                        .ToArray();

答案 2 :(得分:0)

您需要使用

List<List<TextBoxes>>

您可以使用TextBox[,]来实现此目的:

private TextBox[,] textboxes;

public YourClass() {
    // Add this after the text boxes have actually been set up...

    textboxes = new TextBox[,] {
        {textbox00, textbox01, textbox02, ...},
        {textbox10, textbox11, textbox12, ...},
        ,,,
    };
}

然后,您可以textbox00访问textboxes[0,0]textbox56访问textboxes[5,6]等。

答案 3 :(得分:0)

假设每行有相同数量的复选框(例如3),那么你可以这样做:

List<TextBox[3]> textboxes = new List<TextBox[3]>
{
    new TextBox[3] { textbox1, textbox2, textbox3 };
    new TextBox[3] { textbox4, textbox5, textbox6 };
};

然后当你想添加一个新的复选框行

textboxes.Add(new TextBox[3] { textbox7, textbox8, textbox9 }; );

注意:DataGridView控件可能是比TextBoxes更好的选项。 它为您提供了类似Excel的单元格结构,您可以轻松地将List作为数据源连接到它。