这就是我想要的:
从组合框中选择表格的大小n x m(最大10x10),接下来你会得到许多能够填充数据的文本框。
我开始使用隐藏的10x10进行操作,我使用的代码如下:
if (ComboBox1.Text.Comntains("3") = true)
{
TextBox1.Visibility = true;
TextBox1.Visibility = true;
TextBox1.Visibility = true;
}
但它不是解决方案,因为我需要10x10。
也许解决方案很简单,但我刚开始使用c#。
编辑:
我试图调试我的程序,问题是当我运行时,我可以设置组合框的值并点击“确定”。按钮。我想在点击“确定”后生成文本框。之后,出现错误:"未处理的类型' System.NullReferenceException'发生在simplex_method.exe中。 附加信息:对象引用未设置为对象的实例。"
提示是检查对象是否为“空”'在调用方法之前。
以下代码:
private void button1_Click(object sender, EventArgs e)
{
int verticalCount = 0;
int horizontalValue = 0;
verticalCount = (int)comboBox1.SelectedValue;
horizontalValue = (int)comboBox1.SelectedValue;
for(var i = 0; i < verticalCount; i++)
{
for(var j = 0; j < horizontalValue; j++)
{
var newTextBox = new TextBox();
var x = 10 * verticalCount;
var y = 10 * horizontalValue;
newTextBox.Location = new Point(x, y);
this.Controls.Add(newTextBox);
}
}
}
代码的其余部分是2个组合框和一些初始化条件(相当大的形式)。
答案 0 :(得分:0)
您需要执行以下操作:
// read values from comboboxes (10 is default value)
var verticalCount = Convert.ToInt32(ComboBox1.SelectedValue ?? 10);
var horizontalValue = Convert.ToInt32(ComboBox2.SelectedValue ?? 10);
// create m*n textboxes
for(var i = 0; i < verticalCount; i++)
{
for(var j = 0; j < horizontalValue; j++)
{
var newTextBox = new TextBox()
{
Name = string.Format("TextBox_{0}_{1}", i, j); // put the name here
}
var x = //calculate x location for textbox
var y = //calculate y location for textbox
newTextBox.Location = new Point(x, y);
// add new created textbox to parent control (form)
this.Controls.Add(newTextBox)
}
}