c#依參數自動生成控件

  不少系統都帶有自定義報表的功能,而此功能都需依參數自動生成控件,舉例以下:spa

  

  如上圖,一條查詢語句當中,包含了3個參數,其中兩個是日期型(使用:DATE!進行標識),一個是字符型(使用:進行標識),要生成的效果圖以下所示:code

  

  代碼以下:orm

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;
}

  好了,分享就到這裏,但願對你們有一些幫助。blog

相關文章
相關標籤/搜索