← 返回所有博客文章

ActiveReports 专业版提供无额外版权费用的、可定制的Windows Forms最终用户报表设计器控件。专业版用户可以把这个控件包含在应用程序中,使得最终用户可以创建和修改报表。同时还可以把工具箱、报表资源管理器和Visual Studio属性窗口(Property Grid)也包含到应用程序中,使最终用户可以具有完备的报表定制能力。ActiveReports 安装程序包含了一个这方面的例子,用户可以以此为起点进行开发。本文将介绍如何在工具栏中添加“注释”和“取消注释”功能。

image

首先,在工具栏中添加“注释”和“取消注释”两个按钮,对应代码如下:

    ToolStrip EditMenuStrip = reportdesigner.CreateToolStrips(DesignerToolStrips.Edit)[0];
    btnCom = new ToolStripButton("Comment", Image.FromFile(@"../../Images/Comment.jpg"));
    btnCom.Text = "";
    btnCom.ToolTipText = "注释";
    btnUnCom = new ToolStripButton("UnComment", Image.FromFile(@"../../Images/Uncomment.jpg"));
    btnUnCom.Text = "";
    btnUnCom.ToolTipText = "取消注释";
    btnCom.Click += new EventHandler(btnCom_Click);
    btnUnCom.Click += new EventHandler(btnUnCom_Click);
    EditMenuStrip.Items.Add(btnCom);
    EditMenuStrip.Items.Add(btnUnCom);
    btnCom.Enabled = false;
    btnUnCom.Enabled = false;
    AppendToolStrips(1, new ToolStrip[] { EditMenuStrip });

然后在按钮对应的单击事件中添加以下代码:

    void btnCom_Click(object sender, EventArgs e)
    {
        object obj = reportdesigner.Controls[0].Controls[1].Controls[0].Controls[4];
        string[] strings = (String[])(obj.GetType().GetProperty("Strings").GetValue(obj, null));
        object selectionScript = (object)obj.GetType().GetProperty("Selection").GetValue(obj, null);
        int selectionStart = Convert.ToInt32(selectionScript.GetType().GetProperty("SelectionStart").GetValue(selectionScript, null));
        int selectionLength = Convert.ToInt32(selectionScript.GetType().GetProperty("SelectionLength").GetValue(selectionScript, null));
        int selstartline = -1;
        int totalchars = 0;
        string commentstring = "";
        if (((SectionReport)reportdesigner.Report).ScriptLanguage.ToLower().Contains("vb"))
        {
            commentstring = "'";
        }
        else
        {
            commentstring = "//";
        }
        if (selectionLength > 0)
        {
            for (int i = 0; i <= strings.Length - 1; i++)
            {
                totalchars = totalchars + strings[i].Length;
                if (totalchars > selectionStart & selstartline == -1)
                {
                    selstartline = i;
                }
                totalchars = totalchars + 2;
                if (selstartline != -1)
                {
                    if (!string.IsNullOrEmpty(strings[i].Trim().Replace("  ", "")))
                    {
                        strings[i] = strings[i].Insert(0, commentstring);
                    }
                }
                if (totalchars > selectionStart + selectionLength - 1)
                {
                    break; // TODO: might not be correct. Was : Exit For
                }
            }
            obj.GetType().GetProperty("Strings").SetValue(obj, strings, null);
        }
    }

    void btnUnCom_Click(object sender, EventArgs e)
    {
        object obj = reportdesigner.Controls[0].Controls[1].Controls[0].Controls[4];
        string[] strings = (String[])(obj.GetType().GetProperty("Strings").GetValue(obj, null));
        object selectionScript = (object)obj.GetType().GetProperty("Selection").GetValue(obj, null);
        int selectionStart = Convert.ToInt32(selectionScript.GetType().GetProperty("SelectionStart").GetValue(selectionScript, null));
        int selectionLength = Convert.ToInt32(selectionScript.GetType().GetProperty("SelectionLength").GetValue(selectionScript, null));
        int selstartline = -1;
        int totalchars = 0;
        string commentstring = "";
        if (((SectionReport)reportdesigner.Report).ScriptLanguage.ToLower().Contains("vb"))
        {
            commentstring = "'";
        }
        else
        {
            commentstring = "//";
        }
        if (selectionLength > 0)
        {
            for (int i = 0; i <= strings.Length - 1; i++)
            {
                totalchars = totalchars + strings[i].Length;
                if (totalchars > selectionStart & selstartline == -1)
                {
                    selstartline = i;
                }
                totalchars = totalchars + 2;
                if (selstartline != -1)
                {
                    if (((SectionReport)reportdesigner.Report).ScriptLanguage.ToLower().Contains("vb"))
                    {
                        if (strings[i][0].ToString() == "'")
                        {
                            strings[i] = strings[i].Remove(0, 1);
                        }
                    }
                    else
                    {
                        if (strings[i].IndexOf("//") == 0)
                        {
                            strings[i] = strings[i].Remove(0, 2);
                        }
                        else
                        {
                            if (strings[i].IndexOf("/") == 0)
                            {
                                strings[i] = strings[i].Remove(0, 1);
                            }
                        }
                    }
                }
                if (totalchars > selectionStart + selectionLength - 1)
                {
                    break; // TODO: might not be correct. Was : Exit For
                }
            }
            obj.GetType().GetProperty("Strings").SetValue(obj, strings, null);
        }
    }

运行截图如下:

CustomEUD

源码下载: