无法将控件添加到表单

Cannot Add control to form(无法将控件添加到表单)

本文介绍了无法将控件添加到表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm trying to add a Usercontrol to a form. The UserControl is in a separate project than the form, but is in the same solution. I have added this control to other forms in the past, however, something has changed and I get the following error:

"Failed to create the component MessageDisplayListControl. The error message follows: 'System.Runtime.Serialization.SerializationException: Type AceXtremeNET.Utilities.Message' in Assymbly AceXtremeNET, Version=10.0.0.273,...... is not marked as serializable. at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMember(RuntimeType type) at ...."

The control does get added to the .Designer.cs, however, it is not displayed in the visual GUI. Everytime I try to build, I get multiple errors that give the same basic error as above, that the 'AceXtremeNET.Utilities.Message' is not Serializable.

--------- Edit ------------------

My control has the following property which appears to the problem.

public IList<Message> MessageList {get{return _getList();} {set {_lostList(value);}}

No code in the control is dependant on this property as it was meant purley as a get/set accessor. Whenever I comment out the code, Everything appears to work correctly. Otherwise I can the error I mentioned above. I have receieved another error on build as I mentioned before, and it appears this is the only Property that is trying to be Serialized.

--------- Edit (Stack Trace)------------------

at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
at System.Runtime.Serialzation.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Type objectType, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, SerializationBinder binder)
at System.Runtime.SerializationFormatters.Binary.ObjectWriter.WriteArray(WriteObjectInfo objectInfo, NameInfo memberNameInfo, WriteObjectInfo memberObjectInfo)
...

--------- Edit (Control Properties)------------------

public MessageControl MessageDisplay {get{return messageControl1;}} // This is another user control I created.  I've not had any problems with this control.
public MessageListBox {get { return listBox1; } }
public int MessageCount { get { return MessageListBox.Items.Count; }}
public bool ValidSelection { get { return (SelectedIndex >= 0 && SelectedIndex < MessageCount); } }
public Message SelectedMessage { get { return listBox1.SelectedItem as Message; } set { MessageDisplay.Message = Value; } }
public int SelectedIndex { get { return listBox1.SelectedIndex; } set { listBox1.SelectedIndex = value; } }

解决方案

read about DesignerSerializationVisibility Enumeration

put this attribute  on MessageList property

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

in this situation designer will lost user change in MessageList but if you set it to

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

Designer will save the Property Content in Designer.cs of control host

这篇关于无法将控件添加到表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:无法将控件添加到表单

基础教程推荐