英文:
How do I create a Winform checklist box with multiple Items in it, pared with Textboxes for user Data Entry?
问题
我需要一个控件,我可以通过迭代来更改其中的项目,基于在单独的组合框中选择了哪个选项。任何指导都将有所帮助,谢谢!。
我可以分别放置每个元素,但然后我不确定如何在不进行大量工作的情况下进行迭代。我想知道是否有更好的控件供我使用。
英文:
I need a control which I can iterate through and change the items in it based on which option is selected in a separate combobox. Any direction would be helpful, thank you!.
I can place each element separately, but then I'm not sure how to do that iteratively without a lot of work. I'm wondering if there is a better control for me to use.
答案1
得分: 0
以下是翻译好的部分:
使用DataGridView
控件并添加足够的DataGridViewColumn
是实现这一目标的简单方法。在您的示例中,您可以向网格视图添加DataGridViewCheckBoxColumn
和DataGridViewTextBoxColumn
。唯一的缺点是(至少在我所知道的范围内)没有简单的方法直接向复选框单元格添加文本,而是可以添加另一个DataGridViewTextBoxColumn
来显示该信息。
它的外观如下所示:
只需在设计器中添加一个DataGridView
,然后显式地向其添加三列。在我的示例中,第一列和第三列是DataGridViewTextBoxColumn
类型,而中间列是DataGridViewCheckBoxColumn
类型。
代码
private void button1_Click(object sender, EventArgs e)
{
addRow("Pavement", -0.25);
addRow("End of way", 0);
addRow("ELER", 0);
addRow("ELEL", 0);
addRow("ELEN", 0);
addRow("ELEB", 0);
}
private void addRow(string pointCode, double offset)
{
DataGridViewTextBoxCell pcc = new DataGridViewTextBoxCell();
DataGridViewCheckBoxCell cbc = new DataGridViewCheckBoxCell();
DataGridViewTextBoxCell ofc = new DataGridViewTextBoxCell();
pcc.Value = pointCode;
ofc.Value = offset.ToString();
cbc.Value = true; //复选框已标记
DataGridViewRow row = new DataGridViewRow();
row.Cells.Add(pcc);
row.Cells.Add(cbc);
row.Cells.Add(ofc);
dataGridView1.Rows.Add(row);
}
英文:
An easy way to achieve this is to use the DataGridView
control and add sufficient DataGridViewColumn
. In your example you could add DataGridViewCheckBoxColumn
and DataGridViewTextBoxColumn
to your grid view. The only downside is that (at least to my knowledge) there is no easy way to add text directly to the checkbox cell but instead one could add another DataGridViewTextBoxColumn
to display that information.
How it would look like:
Just add a DataGridView
in your designer then explicitly add three columns to it. In my example the first and third column is of type DataGridViewTextBoxColumn
whilst the middle column is of type DataGridViewCheckBoxColumn
.
Code
private void button1_Click(object sender, EventArgs e)
{
addRow("Pavement", -0.25);
addRow("End of way", 0);
addRow("ELER", 0);
addRow("ELEL", 0);
addRow("ELEN", 0);
addRow("ELEB", 0);
}
private void addRow(string pointCode, double offset)
{
DataGridViewTextBoxCell pcc = new DataGridViewTextBoxCell();
DataGridViewCheckBoxCell cbc = new DataGridViewCheckBoxCell();
DataGridViewTextBoxCell ofc = new DataGridViewTextBoxCell();
pcc.Value = pointCode;
ofc.Value = offset.ToString();
cbc.Value = true; //Checkbox marked
DataGridViewRow row = new DataGridViewRow();
row.Cells.Add(pcc);
row.Cells.Add(cbc);
row.Cells.Add(ofc);
dataGridView1.Rows.Add(row);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论