WinForm技巧之自定义条件

WinForm技巧之自定义条件

文章目录


场景

代码


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

csharp 复制代码
 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();
        }
相关推荐
plmm烟酒僧1 小时前
Windows下QT调用MinGW编译的OpenCV
开发语言·windows·qt·opencv
假装我不帅2 小时前
asp.net framework从webform开始创建mvc项目
后端·asp.net·mvc
神仙别闹2 小时前
基于ASP.NET+SQL Server实现简单小说网站(包括PC版本和移动版本)
后端·asp.net
IT技术分享社区3 小时前
C#实战:使用腾讯云识别服务轻松提取火车票信息
开发语言·c#·云计算·腾讯云·共识算法
Jtti4 小时前
Windows系统服务器怎么设置远程连接?详细步骤
运维·服务器·windows
小奥超人4 小时前
PPT文件设置了修改权限,如何取消权?
windows·经验分享·microsoft·ppt·办公技巧
△曉風殘月〆10 小时前
WPF MVVM入门系列教程(二、依赖属性)
c#·wpf·mvvm
逐·風12 小时前
unity关于自定义渲染、内存管理、性能调优、复杂物理模拟、并行计算以及插件开发
前端·unity·c#
hairenjing112314 小时前
使用 Mac 数据恢复从 iPhoto 图库中恢复照片
windows·stm32·嵌入式硬件·macos·word
m0_6569747415 小时前
C#中的集合类及其使用
开发语言·c#