0

I'm trying to override the View property in a derived class from Listview:

public class CustListview : System.Windows.Forms.ListView
{
     private CustView mView = new CustView();

     public enum CustView : int
     {
         Tile = 0,
         Details = 1,
         List = 2,
     }

     public new CustView View
     {
         get
         {
             return mView;
         }
         set
         {
             mView = value;
         }
     }
}

In code it all works well but in VS designer, when I add a CustListview to a form, the View property causes an error

Object reference not set to an instance of an object

in the Properties window.

When I override any other property this way like Alignment or TabStop it works as expected and I see a dropdownlist with the 3 items Tile, Details and list.

Why isn't it working with the View property?

0

2 Answers 2

4

You're not overriding - if you were, you'd use the keyword override. Instead, you're using the keyword new, which hides the base class property in the derived class. Of course, you can't override the property here, given that the type of ListView.View is just View, not CustView... and the property isn't even virtual in the first place.

It's not clear that you should be overriding anything though. Any code using just ListView will expect to be able to use View.LargeIcon etc. You've effectively got two properties here - if that's what you want, you should give them different names to make it absolutely clear that you do have two properties. But you should also consider how you expect them to interact.

You haven't told us what you're really trying to achieve here - but it feels like your current approach is going to cause more problems than solutions. If the point is really just to avoid seeing options you don't want in a drop-down, I'd probably add some sort of validation instead. (I don't know which event it would be best to attach that to, admittedly. It's been a while since I've done WinForms.)

(As an aside, I'd also recommend using automatically implemented properties rather than writing 12 lines of code where 1 line would do exactly the same thing.)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your insights but why doesn't my implementation works with the View property and works with other Listview properties?
@DennisV: When you go outside the normal way of doing things, it's possible that they'll work in some situations but not others - and even where they do work now, there's no guarantee they'll keep working in the future.
4

The ListView component has a custom designer: ListViewDesigner, and the ListViewDesigner class needs to create a PropertyDescriptor based on the ListView.View property descriptor, but because you overwrote the View property, the creation will fail due to type mismatch.

To fix this error, you can use another name:

public CustView CustView

Or use another designer:

[Designer(typeof(ControlDesigner))]
public class CustListview : ListView

Or extend the property using the IExtenderProvider interface:

[ProvideProperty("View", typeof(CustListview))]
public class CustListview : ListView, IExtenderProvider
{
    ...
    public bool CanExtend(object extendee) => extendee is CustListview;
    public CustView GetView(CustListview v) => v.mView;
    public void SetView(CustListview v, CustView value) => v.mView = value;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.