如何根据组合框选择的值创建多个文本框?
How to create multiple textboxes based on combobox selected value?
这是我想要的:
从组合框中,您 select 的大小为 table n x m(最大 10x10),接下来您将获得许多能够填充数据的文本框。
我开始用隐藏的 10x10 来做,我使用了如下代码:
if (ComboBox1.Text.Comntains("3") = true)
{
TextBox1.Visibility = true;
TextBox1.Visibility = true;
TextBox1.Visibility = true;
}
但这不是解决方案,因为我需要 10x10。
也许解决方案很简单,但我刚刚开始使用 C#。
编辑:
我尝试调试我的程序,问题是当我 运行 时,我可以设置组合框的值并单击 'OK' 按钮。我想在单击 'OK' 后生成文本框。之后,出现错误:“simplex_method.exe 中发生了 'System.NullReferenceException' 类型的未处理异常。
附加信息:未将对象引用设置为对象的实例。"
提示是在调用方法之前检查对象是否'null'。
代码如下:
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 个组合框和一些初始化条件(相当大的形式)。
您需要执行以下操作:
// 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)
}
}
这是我想要的:
从组合框中,您 select 的大小为 table n x m(最大 10x10),接下来您将获得许多能够填充数据的文本框。
我开始用隐藏的 10x10 来做,我使用了如下代码:
if (ComboBox1.Text.Comntains("3") = true)
{
TextBox1.Visibility = true;
TextBox1.Visibility = true;
TextBox1.Visibility = true;
}
但这不是解决方案,因为我需要 10x10。
也许解决方案很简单,但我刚刚开始使用 C#。
编辑:
我尝试调试我的程序,问题是当我 运行 时,我可以设置组合框的值并单击 'OK' 按钮。我想在单击 'OK' 后生成文本框。之后,出现错误:“simplex_method.exe 中发生了 'System.NullReferenceException' 类型的未处理异常。 附加信息:未将对象引用设置为对象的实例。"
提示是在调用方法之前检查对象是否'null'。
代码如下:
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 个组合框和一些初始化条件(相当大的形式)。
您需要执行以下操作:
// 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)
}
}