简体   繁体   中英

winUserControl in VS2010 - properties are not visible in designer

I have a problem with (I suppose) my Visual Studio 2010 Express environment: when I design my own UserControl, in Properties grid I can't see public properties of that control. They are however visible in the project, that reference this control.
As it's Express Edition, I create new empty project, then add new UserControl to it.
Then, for a test, I put following code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Project1
{
    public partial class UserControl1 : UserControl
    {
        private int myNumber;

        [Browsable(true)]
        public int MyNumber
        {
            get
            {
                return myNumber;
            }
            set
            {
                myNumber = value;
            }
        }


        public UserControl1()
        {
            InitializeComponent();
        }
    }
}  

In VS 2008, as I remember, that should be enogh to show MyNumber property in Properties grid, even without [Browsable(true)] attribute. In VS 2010 however, when I double click UserControl1.cs in Solution Explorer and look in Properties, I don't see MyNumber.
When I reference and use this control in another project, there is an access to it's properties.

I've tried to competly reinstall VS 2010 environment, including SP1, but with no success. Do you have any idea what can be wrong?

By the way: none of these attributes are working, either:

[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Bindable(true)]

Best regards,
Marcin

I believe this the normal behavior of VS2010 and assume it's by design. It behaves the same for me in 2010 Ultimate. When you place UserControl1 on a form, you'll see its custom properties.

My guess is this is because when you're designing the control, there is no instance of your control yet (it may not have even been compiled). What you're looking at is an instance of UserControl . When you compile your control and then add it to a form, the designer creates an instance of your control, so its properties can be seen and manipulated.

I haven't used the [Browsable] tag before. However below is an example of what I'm using in one of my projects.

[Description("The length used to display the dimensions")]
[Category("Custom")]
public double DisplayLength { get; set; }

I'm guessing you need to add a category.

This won't work due to the way VS handles ascx'es in Designer. For details, see this excellent answer on SO.

If the answer is not what you expected, you can still migrate the.ascx'es to a User Control library as I described in my blog .

If I had the choice, I would start over all my ascx code as Custom Web Server Controls .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM