Jeff
Jeff

Reputation: 36573

Windows Forms Custom Design Properties

This is a question that's been nagging me for several years.

We use derived Windows Forms components and controls in our project. For example, we derived from Button, UserControl, etc.

Our derived Button has some custom properties on it. For example, it has an enum Property called Severity on it. The setter for this property looks at the enum value and sets some things like text color and border and such on the base Button.

This works great...BUT our User Controls that place this button on their designer and set the value for Severity in the Properties window end up actually writing the code that the Severity property executes in the setter to the User Control's button itself.

We don't want that...it defeats the purpose of having the shared Severity property, because we can no longer change the implementation of Severity and it won't globally apply the changes, because the User Controls themselves set the Severity property AND the code that it contained when it was dragged onto the control...

I've tried adding attributes like DesignerSerializationVisiblity and such, but this never seems to have the desired effect.

How do I prevent the Windows Forms designer from generating the code INSIDE the implementation of the Severity property?

Thanks.

Upvotes: 1

Views: 760

Answers (1)

Gene Merlin
Gene Merlin

Reputation: 902

I’ve had a similar problem with WinForms where a form was running code in the designer. To get around this, I created a static method that checked to see if Visual Studio was the host process and wrap it around the code that the designer should ignore.

VB.Net

Imports System.Diagnostics

Public Shared Function IsVisualStudioHostProcess() As Boolean
    Return (Process.GetCurrentProcess().ProcessName.ToUpper() = "DEVENV")
End Function

Public Sub MethodName()
    If Not (IsVisualStudioHostProcess()) Then
        ' Code here
    End If
End Sub

C#

Using System.Diagnostics;

public static bool IsVisualStudioHostProcess()
{
    return (Process.GetCurrentProcess().ProcessName.ToUpper() == "DEVENV");
}

public void MethodName()
{
    if (!IsVisualStudioHostProcess())
    {
        // Code Here
    }
}

Upvotes: 2

Related Questions