WinForm技巧之自定义条件


场景

在这里插入图片描述

在这里插入图片描述

代码


主要通过修改控件的大小和位置来实现的

 private List<Panel> conditionPanels = new List<Panel>();
        int cN = 1;//控件的名字序号
        //自定义多条件查询
        private void button_condition_Click(object sender, EventArgs e)
        {
            if (cN == 3)
            {
                new WarningDialogForm("空间有限只能添加两个条件!").ShowDialog();
                return;
            }
            Panel newPanel = CreateConditionPanel();
            flowLayoutPanel1.Controls.Add(newPanel);
            cN++;
            conditionPanels.Add(newPanel);
        }
        private Panel CreateConditionPanel()
        {
            Panel panel = new Panel();
            panel.Size = new Size(370, 50);

            ComboBox condition = new ComboBox();
            condition.Name = $"condition{cN}";
            condition.DropDownStyle = ComboBoxStyle.DropDownList;
            condition.Items.AddRange(new object[] { "and", "or" });
            condition.Size = new Size(60, 20);
            condition.Location = new Point(1, 5);
            panel.Controls.Add(condition);

            TextBox valueTextBox1 = new TextBox();
            valueTextBox1.Name = $"valueTextBox{cN}";
            valueTextBox1.Size = new Size(95, 20);
            valueTextBox1.Location = new Point(62, 5);
            panel.Controls.Add(valueTextBox1);

            ComboBox conditionComboBox = new ComboBox();
            conditionComboBox.Name = $"conditionComboBox{cN}";
            conditionComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
            conditionComboBox.Items.AddRange(new object[] { "=", "!=", ">", "<", "like" });
            conditionComboBox.Size = new Size(60, 20);
            conditionComboBox.Location = new Point(158, 5);
            panel.Controls.Add(conditionComboBox);

            TextBox valueTextBox2 = new TextBox();
            valueTextBox2.Name = $"valueTextBox{cN}";
            valueTextBox2.Size = new Size(95, 20);
            valueTextBox2.Location = new Point(219, 5);
            panel.Controls.Add(valueTextBox2);

            Button removeButton = new Button();
            removeButton.Name = $"removeButton{cN}";
            removeButton.Text = "-";
            removeButton.Size = new Size(20, 20);
            removeButton.Location = new Point(314, 5);
            removeButton.Click += (s, e) => RemovePanel(panel);
            panel.Controls.Add(removeButton);



            return panel;
        }

        private void RemovePanel(Panel panel)
        {
            flowLayoutPanel1.Controls.Remove(panel);
            conditionPanels.Remove(panel);
            cN--;
            panel.Dispose();
        }

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部