I wrote a TextBox which checks its text for validity. The method to check the text is provided by the caller as a System.Func<string, bool>
. The simplified code looks like this:
public class ValidTextBox : System.Windows.Forms.TextBox
{
public System.Func<string, bool> IsValid { get; set; }
}
I used it successfully for several things, but then I tried to provide a nonstatic method. The code compiles fine but Visual Studio's Designer throws an lengthy exception whenever I try to drag it from the Toolbox into a Form (see below).
public class SpecialTextBox : ValidTextBox
{
public SpecialTextBox()
{
base.IsValid = s => true; // works
base.IsValid = StaticReturnTrue; // works
base.IsValid = s => StaticReturnTrue(s); // works
base.IsValid = this.ReturnTrue; // doesn't work
base.IsValid = s => this.ReturnTrue(s); // doesn't work
}
private static bool StaticReturnTrue(string s)
{
return true;
}
private bool ReturnTrue(string s)
{
return true;
}
}
I can't copy and paste the text of a MessageBox so please excuse typos. The language is german:
Fehler beim Erstellen der Komponente SpecialTextBox. Die Fehlermeldung: System.Runtime.Serialization.SerializationException: Der Typ "GuiTest.SpecialTextBox" in Assembly "GuiTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" ist nicht als serialisierbar gekennzeichnet.
bei System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
bei System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContent content)
bei System.Runtime.Serialization.FormatterServices.Binary.WriteObjectInfo.InitMemberInfo()
bei System.Runtime.Serialization.FormatterServices.Binary.WriteObjectInfo.InitSerialze([...]) bei System.Runtime.Serialization.FormatterServices.Binary.ObjectWriter.Write([...])
If I mark SpecialTextBox
as [System.Serializable]
the designer complains that ValidTextBox
is not marked as serializable. If I then mark ValidTextBox
as [System.Serializable]
the designer complains that System.Windows.Forms.TextBox
is not marked as serializable, which I can't change.
Why does the designer get an exception?
How can I provide a nonstatic method at runtime?