欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#依参数自动生成控件

程序员文章站 2023-04-08 08:34:06
很多系统都带有自定义报表的功能,而此功能都需依参数自动生成控件,举例如下: 如上图,一条查询语句当中,包含了3个参数,其中两个是日期型(使用:DATE!进行标识),一个是字符型(使用:进行标识),要生成的效果图如下所示: 代码如下: 好了,分享就到这里,希望对大家有一些帮助。 ......

  很多系统都带有自定义报表的功能,而此功能都需依参数自动生成控件,举例如下:

  c#依参数自动生成控件

  如上图,一条查询语句当中,包含了3个参数,其中两个是日期型(使用:date!进行标识),一个是字符型(使用:进行标识),要生成的效果图如下所示:

  c#依参数自动生成控件

  代码如下:

private void frmdefine_myeventclose(string strid, string strname, string strsql)
{
    //值传递
    returnid = strid;
    returnname = strname;
    returnsql = strsql;
    //空格清除
    while (returnsql.indexof(": ") != -1)
    {
        returnsql = returnsql.replace(": ", ":");
    }
    while (returnsql.indexof(":date! ") != -1)
    {
        returnsql = returnsql.replace(":date! ", ":date!");
    }
    //产生标题
    rtxtname.text = "[" + strid + "]" + strname;
    rtxtname.selectionalignment = system.windows.forms.horizontalalignment.center;
    //清除控件
    splitcontainer2.panel1.controls.clear();
    //添加控件
    if (strsql.indexof(":") != -1)
    {
        int paramindex = 100;   //参数序号,从100往前倒数。
        paramsql = string.empty;
        list<string> list = new list<string>(returnsql.split(' '));
        for (int i = list.count - 1; i > -1; i--)
        {
            if (list[i].indexof(":") == -1)
            {
                paramsql = list[i] + " " + paramsql;
                list.removeat(i);
            }
            else
            {
                if (list[i].indexof(":date!") != -1)
                {
                    if (list[i].indexof(":date!") == 0)
                    {
                        paramsql = "params[" + paramindex.tostring() + "] " + paramsql;
                        paramindex--;
                    }
                    else
                    {
                        paramsql = list[i].substring(0, list[i].indexof(":date!")) + "params[" + paramindex.tostring() + "] " + paramsql;
                        paramindex--;
                    }
                }
                else
                {
                    if (list[i].indexof(":") == 0)
                    {
                        paramsql = "params[" + paramindex.tostring() + "] " + paramsql;
                        paramindex--;
                    }
                    else
                    {
                        paramsql = list[i].substring(0, list[i].indexof(":")) + "params[" + paramindex.tostring() + "] " + paramsql;
                        paramindex--;
                    }
                }
            }
        }
        //添加控件
        if (list.count > 0)
        {
            //坐标初始化
            point point = new point(6, 6);
            //控件行数
            int introw = 0;
            //以6个参数为一行,初始化控件面板的高度。
            splitcontainer2.splitterdistance = 25 * convert.toint32(math.ceiling(list.count / 6.0));
            //控件添加
            paramindex = 100 - list.count + 1;
            for (int i = 0; i < list.count; i++)
            {
                string strlabeltext = string.empty;
                if (i % 6 == 0 && i != 0)
                {
                    introw++;
                    point.x = 6;
                    point.y = 6 + 25 * introw;
                }
                //日期及文本
                if (list[i].indexof(":date!") != -1)
                {
                    //label
                    strlabeltext = list[i].substring(list[i].indexof(":date!") + 6, list[i].length - list[i].indexof(":date!") - 6);
                    label label = new label();
                    label.name = "label" + i.tostring();
                    label.text = strlabeltext;
                    label.autosize = true;
                    label.location = point;
                    splitcontainer2.panel1.controls.add(label);
                    //datetimepicker
                    datetimepicker datetimepicker = new datetimepicker();
                    datetimepicker.name = "params[" + paramindex.tostring() + "]";
                    datetimepicker.format = datetimepickerformat.custom;
                    datetimepicker.customformat = "yyyy-mm-dd";
                    datetimepicker.width = 100;
                    point.x = point.x + label.width + 4;
                    point.y = label.location.y - 4;
                    datetimepicker.location = point;
                    splitcontainer2.panel1.controls.add(datetimepicker);
                    //坐标初始化
                    point.x += 112;
                    point.y = label.location.y;
                }
                else
                {
                    //label
                    strlabeltext = list[i].substring(list[i].indexof(":") + 1, list[i].length - list[i].indexof(":") - 1);
                    label label = new label();
                    label.name = "label" + i.tostring();
                    label.text = strlabeltext;
                    label.autosize = true;
                    label.location = point;
                    splitcontainer2.panel1.controls.add(label);
                    //textbox
                    textbox textbox = new textbox();
                    textbox.name = "params[" + paramindex.tostring() + "]";
                    textbox.text = string.empty;
                    textbox.width = 100;
                    point.x = point.x + label.width + 4;
                    point.y = label.location.y - 4;
                    textbox.location = point;
                    splitcontainer2.panel1.controls.add(textbox);
                    //坐标初始化
                    point.x += 112;
                    point.y = label.location.y;
                }
                //参数序号赋值
                paramindex++;
            }
        }
    }
    //数据源初始化
    datagridview1.datasource = null;
}

  好了,分享就到这里,希望对大家有一些帮助。