当前位置: 移动技术网 > IT编程>开发语言>.net > 在Winform系统界面中对进展阶段的动态展示和处理

在Winform系统界面中对进展阶段的动态展示和处理

2019年04月13日  | 移动技术网IT编程  | 我要评论

电动车控制板,滚雷台,秦国刚视频

在我们做客户关系管理系统的winform界面的时候,需要对进展阶段这个属性进行一个方便的动态切换和标记处理,如我们根据不同的进展阶段显示不同的相关信息,也可以随时保存当前的阶段信息。其实也是一个比较常见的功能,我们可以把字典列表扁平化动态展示在控件上,然后根据用户选择的阶段位置进行切换即可,本篇随笔就是在客户的需求基础上完善这个功能。

1、进展阶段的动态展示和处理

我们来看看界面的大致情况

其实这部分是根据字典列表进行动态展示的,也就是使用一个用户控件进行处理即可。

为了实现这个功能,我们先创建一个用户控件,如下界面所示,保留一个按钮,这个我们让它先占着位置,最后还是把它追加到最后的位置上即可。

为了展示所有阶段,并记录当前阶段,我们设置了两个变量,放在用户控件里面

        /// <summary>
        /// 阶段列表
        /// </summary>
        public list<clistitem> stagelist { get; set; }
        /// <summary>
        /// 当前阶段的值
        /// </summary>
        public double currentstage { get; set; }

然后为了在切换和保存两个事件触发外部处理,我们添加两个事件处理,如下所示

        /// <summary>
        /// 选中某个阶段的处理事件
        /// </summary>
        public eventhandler onselectedstagehandler { get; set; }
        /// <summary>
        /// 设置阶段完成的处理事件
        /// </summary>
        public eventhandler onsetcompletestage { get; set; }

这样用户控件看起来就像是这样子的代码了。

    /// <summary>
    /// 阶段控件显示
    /// </summary>
    public partial class stagecontrol : baseusercontrol
    {
        /// <summary>
        /// 阶段列表
        /// </summary>
        public list<clistitem> stagelist { get; set; }
        /// <summary>
        /// 当前阶段的值
        /// </summary>
        public double currentstage { get; set; }
        /// <summary>
        /// 选中某个阶段的处理事件
        /// </summary>
        public eventhandler onselectedstagehandler { get; set; }
        /// <summary>
        /// 设置阶段完成的处理事件
        /// </summary>
        public eventhandler onsetcompletestage { get; set; }

为了动态展示控件的信息,我们需要使用一个自定义函数来对控件按钮的位置进行判断并绘制,这样可以根据需要进行相关样式的定义,也可以动态变化阶段的列表内容。

        /// <summary>
        /// 初始化控件
        /// </summary>
        public void init()
        {
            this.controls.clear();//清空界面

            //根据阶段列表数量计算每个选项的大小
            if (stagelist != null && stagelist.count > 0)
            {
                var count = stagelist.count;
                //计算每项的宽度、高度
                var width = (this.width-150) * 0.8 / (count * 1.0);
                var height = this.height * 0.8;

                //计算间隔位置,默认为0,最大不超过20宽度
                double space = 0;
                if ((count - 1) > 0)
                {
                    space = (this.width * 0.2) / ((count - 1) * 1.0);
                }
                space = (space > 20) ? 20 : space; //限定最大间隔20

                //根据列表项目,动态构建按钮显示项目
                int i = 0;
                foreach (clistitem item in stagelist)
                {
                    double value = convert.todouble(item.value);
                    simplebutton button = new simplebutton();
                    button.text = value.tostring("p0");//显示百分比
                    button.tooltip = item.text;
                    button.tag = value;
                    button.buttonstyle = borderstyles.hotflat;
                    button.appearance.options.usebackcolor = true;
                    //根据所处阶段设置背景色
                    if(currentstage >= value)
                    {
                        button.appearance.backcolor = color.skyblue;
                    }
                    else
                    {
                        button.appearance.backcolor = color.transparent;
                    }

                    //按钮的单击事件,触发对外部的处理
                    button.click += (s,e)=>
                    {
                        var currentstep = (simplebutton)s;
                        currentstage = convert.todouble(currentstep.tag);
                        if(onselectedstagehandler != null)
                        {
                            onselectedstagehandler(s, e);
                        }
                        init();

                    };
                    //根据计算好的信息,设置按钮大小和位置
                    button.size = new size((int)width, (int)height);
                    button.location = new point(i * (int)(width + space), 0);
                    this.controls.add(button);

                    i++;
                }

                this.btnsetcomplete.location = new point(this.width-145, 4);
                this.controls.add(btnsetcomplete);
            }
        }

如果是要保存状态,也交由事件处理

        /// <summary>
        /// 完成操作,触发外部对状态的保存
        /// </summary>
        private void btnsetcomplete_click(object sender, eventargs e)
        {
            if(onsetcompletestage != null)
            {
                onsetcompletestage(sender, e);
            }
        }

 

2、外部窗体使用自定义控件

创建好用户控件后,在外部窗体使用这个用户控件的时候,我们把它拖到窗体界面里面,如下设计界面效果所示。

在这个窗体里面,初始化控件的事件处理,用来做选择的变化处理和保存状态处理。

            this.stagecontrol1.onselectedstagehandler += (s, e) =>
            {
                this.txtstage.setcomboboxitem(string.concat(this.stagecontrol1.currentstage));
            };
            this.stagecontrol1.onsetcompletestage += (s, e) =>
            {
                if (!string.isnullorempty(id))
                {
                    this.txtstage.setcomboboxitem(string.concat(this.stagecontrol1.currentstage));
                    var result = callerfactory<isalechanceservice>.instance.updatestage(tempinfo.id, this.stagecontrol1.currentstage);
                    showmessageautohide(result.success ? "设置成功" : "设置失败");
                    processdatasaved(null, null);
                }
            };

我们在调用窗体使用这个进展阶段的控件的时候,需要给它初始化数据,如下是对字典信息的绑定给它。

        /// <summary>
        /// 初始化数据字典
        /// </summary>
        private void initdictitem()
        {
            //初始化代码
            this.txtstatus.binddictitems("机会状态");
            this.txtsource.binddictitems("机会来源");
            this.txtchancetype.binddictitems("机会类别");

            this.txtcompetitiveindex.binddictitems("机会竞争指数");
            this.txtconfidenceindex.binddictitems("机会信心指数");
            this.txtstage.binddictitems("机会进展阶段");

            var listitem = dictitemutil.getdictbydicttype("机会进展阶段");
            this.stagecontrol1.stagelist = listitem;
        }   

然后在界面显示的时候,调用init函数即可,如下代码所示。

            //初始化显示控件
            this.stagecontrol1.init();

实际项目运行的整体效果如下所示。

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网