【问题标题】:How to display complex result along with a check box in windows forms appl如何在 Windows 窗体应用程序中显示复杂结果以及复选框
【发布时间】:2019-12-23 20:00:04
【问题描述】:

您好,我是 Windows 窗体应用程序的新手。 我有一个场景,我得到名称,某些文件的路径(动态)并希望让用户选择他们不需要的文件并删除它们。 我希望它以表格格式显示:-

在删除我想要用户可以单击的复选框下,我将在底部创建一个按钮,该按钮将获取所有选定的文件并调用删除逻辑。 我如何从 UI 中实现这一点,我应该使用哪种布局以及如何在其中再创建一个元素。

【问题讨论】:

  • ListView 控件包含CheckBoxes 布尔值,它允许您在列表视图中拥有一列复选框;相关:stackoverflow.com/a/45171704/1481699
  • 为 thosde 单元定义 ColumType。有效选项为 Button、Checkbox、ComboBox、Image、Link 和 TextBox
  • 您还拥有 DataGridView 控件。如果您将文件详细信息添加到 List<class> 或 DataTable,则处理起来也更容易(更直接)。

标签: c# winforms


【解决方案1】:

尝试以下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        static DataTable dt = new DataTable();
        public Form1()
        {
            InitializeComponent();

            dt.Columns.Add("SI No", typeof(int));
            dt.Columns.Add("Delete", typeof(string));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Path", typeof(string));

            dt.Rows.Add(new object[] { 1, "[]", "a", "C:\\a" });
            dt.Rows.Add(new object[] { 2, "[]", "b", "C:\\b" });
            dt.Rows.Add(new object[] { 3, "[]", "c", "C:\\c" });
            dt.Rows.Add(new object[] { 4, "[]", "d", "C:\\d" });
            dt.Rows.Add(new object[] { 5, "[]", "e", "C:\\e" });
            dt.Rows.Add(new object[] { 6, "[]", "f", "C:\\f" });

            dataGridView1.DataSource = dt;

            dataGridView1.CellClick +=new DataGridViewCellEventHandler(dataGridView1_CellClick);

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            int col = e.ColumnIndex;

            if ((col == 1) && (dt.Rows.Count > 0))
            {
                dt.Rows[row].Delete();
                dataGridView1.DataSource = null;
                dataGridView1.DataSource = dt;
            }

        }
    }
}

使用按钮

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        static DataTable dt = new DataTable();
        public Form1()
        {
            InitializeComponent();
            dt.Columns.Add("SI No", typeof(int));
            dt.Columns.Add("Delete", typeof(Boolean));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Path", typeof(string));

            dt.Rows.Add(new object[] { 1, false, "a", "C:\\a" });
            dt.Rows.Add(new object[] { 2, false, "b", "C:\\b" });
            dt.Rows.Add(new object[] { 3, false, "c", "C:\\c" });
            dt.Rows.Add(new object[] { 4, false, "d", "C:\\d" });
            dt.Rows.Add(new object[] { 5, false, "e", "C:\\e" });
            dt.Rows.Add(new object[] { 6, false, "f", "C:\\f" });

            dataGridView1.DataSource = dt;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = dt.Rows.Count - 1; i >= 0; i--)
            {
                if ((Boolean)(dt.Rows[i][1]) == true)
                {
                    dt.Rows[i].Delete();
                }
            }
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = dt;
        }
    }
}

【讨论】:

  • 可能是dt.Columns.Add("Delete", typeof(bool));?所以 OP 得到一个 CheckBox 列。
  • 我认为这可能有点令人困惑。我想了一会儿。
  • 我不确定 confusing 指的是什么。 OP 正在寻找一个允许选择稍后将被删除的文件的 UI。如果您删除该行,那可能会造成混淆。此外,由于您要从 DataTable 中删除数据,因此无需重置 DataSource。这可能适用于对象列表。
  • 是的,答案解决了我面临的设计问题。一旦我点击了我想要删除的行的复选框并按下按钮,我怎样才能只获取点击了复选框的行?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-23
  • 1970-01-01
  • 2014-09-07
  • 2014-03-26
  • 1970-01-01
  • 2022-11-18
相关资源
最近更新 更多