How do I create a Winform checklist box with multiple Items in it, pared with Textboxes for user Data Entry?

huangapple go评论35阅读模式
英文:

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!.

Example

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是实现这一目标的简单方法。在您的示例中,您可以向网格视图添加DataGridViewCheckBoxColumnDataGridViewTextBoxColumn。唯一的缺点是(至少在我所知道的范围内)没有简单的方法直接向复选框单元格添加文本,而是可以添加另一个DataGridViewTextBoxColumn来显示该信息。

它的外观如下所示:

How do I create a Winform checklist box with multiple Items in it, pared with Textboxes for user Data Entry?

只需在设计器中添加一个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:

How do I create a Winform checklist box with multiple Items in it, pared with Textboxes for user Data Entry?

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);
        }

huangapple
  • 本文由 发表于 2023年7月6日 17:38:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627473.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定