How to enable/disable the proerty by other property value changed in property page of user control in design time

ShengChi Lo 41 Reputation points
2023-06-01T11:20:13.0966667+00:00

I have two custom property of user control which shown in proerty page.

How to enable/disable the property by changed the value of other property in design page.

For example,

when I set EnableTabClose to False in desig page,

then disable the property: DisableCloseBtnName, otherwise enable it.

PropertyBox_2

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,918 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,216 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 48,776 Reputation points Microsoft Vendor
    2023-06-02T06:38:15.46+00:00

    Hi @ShengChi Lo , Welcome to Microsoft Q&A. Updated: From a design point of view you cannot modify this [ReadOnly(true)] attribute like this. Is there an easy alternative you can live with? You can choose to disable disableCloseBtnName when enableTabClose is false。

    private bool enableTabClose = true;
    private bool disableCloseBtnName = true;
    public bool EnableTabClose
    {
        get { return enableTabClose; }
        set
        {
            if (enableTabClose != value)
            {
                enableTabClose = value;
                Invalidate();
            }
        }
    }
    
    public bool DisableCloseBtnName
    {
        get { return disableCloseBtnName; }
        set
        {
            if (enableTabClose)
            {
                if (disableCloseBtnName != value)
                {
                    disableCloseBtnName = value;
                    Invalidate();
                }
            }
        }
    }
    

    enter image description here


    This is perfectly fine.

    You can set two properties like this:

    private bool enableTabClose = true;
    private bool disableCloseBtnName = true;
    public bool EnableTabClose
    {
        get { return enableTabClose; }
        set
        {
            if (enableTabClose != value)
            {
                enableTabClose = value;
                Invalidate();
                DisableCloseBtnName = enableTabClose;
            }
        }
    }
    
    public bool DisableCloseBtnName
    {
        get { return disableCloseBtnName; }
        set
        {
            if (disableCloseBtnName != value)
            {
                disableCloseBtnName = value;
                Invalidate();
            }
        }
    }
    

    enter image description here

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.