【问题标题】:c# Dynamic textbox control with CheckBox and Listc# 带有 CheckBox 和 List 的动态文本框控件
【发布时间】:2011-10-07 01:09:44
【问题描述】:

昨天,我问了一个类似的问题,人们告诉我使用 List 和控制器对象。我相应地更改了我的代码,现在添加了一个 MyContols 类和一个列表,以便更好地控制我的项目。

这是我的类,其中包含我的复选框和文本框:

    private List<MyControls> _myControls = new List<MyControls>();

    class MyControls
      {
    int x=5;
    int y=30; 
    public CheckBox cb = new CheckBox();
    public TextBox tb1 = new TextBox();

    public TextBox tbSpecs = new TextBox();
    public TextBox tb3 = new TextBox();
    public TextBox tb4 = new TextBox();



    public void initElements(String name)
    {
        cb.Width = 10;
        cb.Height = 10;
        cb.Name = "cb_" + name;
        cb.Location = new Point(x, y+5);
        Form.ActiveForm.Controls.Add(cb);
        x += 15;

        tb1.Width = 50;
        tb1.Height = 20;
        tb1.Location = new Point(x, y);
        tb1.Name = "tb1_" + name;
        Form.ActiveForm.Controls.Add(tb1);
        x += 60;

        tbSpecs.Width = 150;
        tbSpecs.Height = 20;
        tbSpecs.Name = "tb2_" + name;
        tbSpecs.Location = new Point(x, y);
        Form.ActiveForm.Controls.Add(tbSpecs);
        x += 160;

        tb3.Width = 40;
        tb3.Height = 20;
        tb3.Name = "tb3_" + name;
        tb3.Location = new Point(x, y);
        Form.ActiveForm.Controls.Add(tb3);
        x += 50; 

        tb4.Width = 450;
        tb4.Height = 20;
        tb4.Name = "tb4_" + name;
        tb4.Location = new Point(x, y);
        Form.ActiveForm.Controls.Add(tb4);

        x = 0;
    }

    public int SetX(int i)
    {
        x = i;
        return x;
    }

    public int SetY(int Y)
    {
        y = Y;
        return y;
    }

}

我还有一个 ProductForm 类,它是我的应用程序中的第二个,它允许我查看文本框中的数据并编辑和删除这些数据。

我的问题是如何删除一行?

我的第二个问题是我必须为每一行创建一个 MyControl 类的新实例吗?可能我的问题很简单。抱歉耽搁了各位!

此功能仅用于测试目的,因为我仍在尝试添加删除行。

        public void CreateFormElements()
        {
        ProductForm form2 = new ProductForm();
        form2.Visible = true;
        form2.Activate();

        MyControls mc = new MyControls();

        _myControls.Add(mc);
        _myControls[0].initElements("1");
        mc = new MyControls();
        _myControls.Add(mc);

        mc.SetY(55);
        _myControls[1].initElements("2");



    }

如果对应的复选框被选中,如何删除整行?

你能告诉我一个可以做到这一点的代码吗?

【问题讨论】:

    标签: c# winforms checkbox textbox


    【解决方案1】:

    在您的 Init 中,为 cb.Checked 事件添加一个处理程序,然后删除该行。

    在初始化中:

     cb.CheckedChanged += cb_CheckedChanged;
    

    处理程序-不确定您的情况的确切实现,但这里有一些粗略的代码:

    private void cb_CheckedChanged(Object sender, EventArgs e) {
       string NameSet  = (sender as CheckBox).Name.Split(new char[]{'_'})[1];
       Form.ActiveForm.Controls.Remove("ch_" + NameSet);
       Form.ActiveForm.Controls.Remove("tb1_" + NameSet);
       Form.ActiveForm.Controls.Remove("tb2_" + NameSet);
       Form.ActiveForm.Controls.Remove("tb3_" + NameSet);
       Form.ActiveForm.Controls.Remove("tb4_" + NameSet);
    
    
    }
    

    【讨论】:

    • 你能给我写一个代码如何添加一个cb。检查和删除行代码??
    • 删除方法出现错误。它说无法将字符串转换为 System.Windows.Forms.Control
    • 您可能需要执行 Form.ActiveForm.Controls.Remove(Form.ActiveForm.Controls.FindControl("ch_" + NameSet));
    • Joe 终于准备好了我的代码 :) 非常感谢您的大力帮助和帮助。现在我正朝着代码的下一步迈进。
    猜你喜欢
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多