miroxlav
miroxlav

Reputation: 12194

How to fix standard control property at desired value?

I want to have TabControl.TabStop property always set to false. I have made it read-only but designer seems to be adding it automatically into initializations what causes error

Property 'TabStop' is ReadOnly

In inherited control, how can I fix property value at constant value without these issues? Should I just use empty setter instead of making the property read-only?

What I have:

public class SpecialTabControl : TabControl
{
    public bool TabStop {
        get { return false; }
    }

    public SpecialTabControl() : base()
    {
        base.TabStop = false;
    }
}

(C# or VB, whatever you prefer.)

Upvotes: 3

Views: 78

Answers (1)

Hans Passant
Hans Passant

Reputation: 941505

Do note the warning that your code produces, first thing you have to fix. And yes, the TabControl designer completely expects the property to have a setter and will spit bullets when it doesn't. So you have to provide a setter.

You'll also want to prevent the property from being visible in the Properties window and be explicit that the value must never be serialized. Which all adds up to:

[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new bool TabStop {
    get { return false; }
    set { base.TabStop = false; }
}

Give the TabIndex property the same treatment.

Upvotes: 3

Related Questions