4

I want to make a custom property for a windows form browsable during design-time but none of my efforts have panned out to success. The obvious solution would seem to be to set the browsable attribute to true:

[Browsable(true),
EditorBrowsable(EditorBrowsableState.Always),
Description("Custom Border Colour"),
Category("Custom")]
public Color BorderColour
{
    get
    {
        return bCol;
    }
    set
    {
        bCol = value;
    }
}

But this doesn't work. I have done it numerous time for custom controls and it works like a charm, in fact, I don't even need to add the attributes because the default is true. This codeproject article seems to do what I want, which is what I described above. MSDN is also a dead end, or I don't know what to search for.

I have tried to add the code to Form1.cs and From1.Designer.cs but nothing works.

Is there something I am missing, like some property I need to set for the form to allow for this, or is it just impossible?

I am using Visual Studio Express 2013, if this would influence the outcomes in any way.


EDIT: Attempts after Reza's answer: A more detailed question on this topic is asked in this question as per Reza's suggestion.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
ChP
  • 466
  • 1
  • 4
  • 17
  • 1
    **Answer to EDIT:** The designer edits the first class in your file (As I mentioned in first linked post). You have put `BaseForm` as first class in your `.cs` file. Make it in a separate file or move the codes for `BaseForm` after `Form1` codes. – Reza Aghaei Apr 20 '16 at 15:10
  • In general it's better to keep code of `BaseForm` separate, so it's better to add new form to the project and name it `BaseForm` and add assitional properties to the form, then for other forms, add `Inherited Form` or add new `Form` and change the base class name manually. – Reza Aghaei Apr 20 '16 at 15:10

1 Answers1

8

Short answer

You should add the property to base class of your form, then you can see it in designer when you open the child form:

public class Form1 : BaseForm
{
    public Form1()
    {
        InitializeComponent();
    }
}
public class BaseForm : Form
{
    //The property is not visible in designer of BaseForm
    //But you can see it in designer of Form1

    public string SomeProperty {get;set;}
}

The reason behind this behavior

The reason is in the way that designer works. When the designer shows a form at design time, in fact it creates an instance of the base class of the form and shows its properties. So having public class Form1:Form in designer, what you see in designer is actually an instance of Form class and the instances of controls which values of properties has been set using using InitializeComponent method of Form1 and also controls which are added using InitializeComponent method of Form1.

Also for user controls, you can not see your custom properties in the designer of your user control, because the properties that you can see in designer of your user control, is properties of it's base class. But when you put an instance of your user control on a form, you will see properties of that instance which is properties of your UserControl1.

Properties of the root element of your designer are properties of base class of the root element. But the values are exactly those which are set in InitializeComponent.

To find more information and see an interesting example of how the designer works, you can take a look at this post or this one.

Community
  • 1
  • 1
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Code examples and descriptions in linked posts are interesting you will find them useful. I recommend you to take a look at linked posts. Let me know if you have any question about the answer :) – Reza Aghaei Apr 19 '16 at 13:31
  • Thank for the advice. I tried implementing your answer after reading your links and the links in the links. I think I grasp what Visual Studio is doing and it's quite clever, but some strange things are now happening, which I added to an edit in my question. Am I doing something wrong? – ChP Apr 20 '16 at 12:57
  • @CharlPretorius You are welcome. While I also answered your edited question, but I believe this would be more useful if you remove the edit part and ask it as a new question. This way the current and the new question and answers will be more useful for future readers. – Reza Aghaei Apr 20 '16 at 15:16
  • Great! I finally got this to work. I created the separate file for `BaseForm` and inherited `BaseForm` by `Form1` and everything works now. Should I still ask the edit as a new question? – ChP Apr 21 '16 at 06:59
  • It's not compulsory at all, since both of questions and answers are really useful for future readers I recommended posting a new question to make them more useful. I you posted the new question, notify me and I'll post my comment **Answer to EDIT** as answer for that question. – Reza Aghaei Apr 21 '16 at 07:03
  • I've posted the new question [here](http://stackoverflow.com/questions/36762789/visual-studio-changes-designer-generated-code-when-i-create-a-new-class-inheriti). – ChP Apr 21 '16 at 07:38
  • Good job :) I answered it and voted to your question. I also remove the edit part of your question here to make this question and answer more clean. – Reza Aghaei Apr 21 '16 at 07:40