ActiveReports 最终用户设计器之自定义工具栏

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

发布于 2013/10/09 00:00

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

源码下载:

 

关于葡萄城

葡萄城是专业的软件开发技术和低代码平台提供商,以“赋能开发者”为使命,致力于通过表格控件、低代码和BI等各类软件开发工具和服务,一站式满足开发者需求,帮助企业提升开发效率并创新开发模式。葡萄城开发技术始于1980年,40余年来始终聚焦软件开发技术,有深厚的技术积累和丰富的产品线。是业界能够同时赋能软件开发和低代码开发的企业。凭借过硬的产品能力、活跃的用户社区和丰富的伙伴生态,与超过3000家合作伙伴紧密合作,产品广泛应用于信息和软件服务、制造、交通运输、建筑、金融、能源、教育、公共管理等支柱产业。

推荐相关案例
推荐相关资源
关注微信
葡萄城社区二维码

关注“葡萄城社区”

加微信获取技术资讯

加微信获取技术资讯

想了解更多信息,请联系我们, 随时掌握技术资源和产品动态