none
Winform user control does not create designer code after user's edit?WinForm自定义控件编辑结果不在窗体代码生成? RRS feed

  • 问题

  • 我创建了一个名称为MSTagServer的自定义控件,其中有个属性为SavedTagsList,给用户提供弹出窗体编辑需要确认的勾选项。为了能记住用户的勾选项,我想再窗体代码内进行生成,问题是编辑完毕后,窗体代码没有任何的变化。测试过程中,我发现只要移除[Editor(typeof(SavedTagSelectDropDownEditor)这段属性代码,用VS标准的方式编辑,每次关闭,均能在设计器中产生代码记忆编辑内容,但是为何用自定义的编辑窗体就不行呢?

    I create a user control named MSTagServer.the control have a property named SavedTagsList which show user the UITypeEditor window to check saved tags.the problem is the desiger does not generate any code like

    this.msTagServer1.SavedTagsList.Add(((Advantech.MSControl.SavedTag)(resources.GetObject("msTagServer1.SavedTagsList"))));

    if I remove the line [Editor(typeof(SavedTagSelectDropDownEditor), typeof(UITypeEditor))],the vs will generate designer code

    as soon as the edit windown close.so,what's the problem?why the user's UITypeEditor can not trigger the form deigner?

    控件代码
         public partial class MSTagServer : Component
         private List<SavedTag> _list = new List<SavedTag>();
            [Editor(typeof(SavedTagSelectDropDownEditor), typeof(UITypeEditor))]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            public List<SavedTag> SavedTagsList
            {
                set
                {
                    _list = value;
                    if (_list != null && Common.EditEnvironmentCheck() == false)//not edit mode,mark
                    {
                        if (TagList.TagsDic.Count > 0)
                        {
                            foreach (SavedTag item in _list)
                            {
                                TagGroup grp = TagList.TagsDic[item.TagGroupName];
                                Tag tag = grp.Tags.Find(x => x.TagName.Equals(item.TagName));
                                tag.SaveToFile = true;
                            }
                        }
                    }
                }
                get { return _list; }
            }
    弹出窗体编辑器
    public class SavedTagSelectDropDownEditor : System.Drawing.Design.UITypeEditor
       {
           public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
           {
               if (context != null && context.Instance != null)
               {
                   return UITypeEditorEditStyle.Modal;
               }
               return base.GetEditStyle(context);
           }
           public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
           {
               IWindowsFormsEditorService editorService = null;
    
               if (context != null && context.Instance != null && provider != null)
               {
                   editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                   if (editorService != null)
                   {
                       MSTagServer ts = (MSTagServer)context.Instance;
                       if (ts != null)
                       {
                           FrmTagSelectControl ctrl = new FrmTagSelectControl(ts.TagDic, ts.SavedTagsList);
                           DialogResult res = editorService.ShowDialog(ctrl);//show user's edit window
                           if (res == DialogResult.OK)
                           {
                               value = ctrl.SavedTagsList;
                               return value;
                           }
                       }
                   }
               }
               return value;
           }
       }
    需要存储在设计器中的类
    [Serializable]
        public class SavedTag
        {
            public string TagGroupName { set; get; }
            public string TagName { set; get; }
        }



    2017年12月6日 0:22

全部回复

  • 问题像是在UITypeEditor,UITypeEditor编辑完毕,窗体代码没有接收到值变化的通知,所以就没有进行代码生成
    2017年12月6日 2:00
  • Hi,

    欢迎在MSDN论坛发帖。

    也感谢你分享你的实验成果,如果你的问题已经解决,请及时标记有用的回复作为答案,这样的操作会帮助其他遇到相同问题的人。

    我测试你的代码,发现有许多变量都是找不到的。 如果你的问题没有解决,你可以上传一个可以重现问题的demo 到 one drive 上面,这样方便我们测试问题。

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2017年12月7日 7:26
    版主