简体   繁体   中英

Create custom properties at runtime

I am trying to create a custom property (like Name, BackgroundImage etc.) for my dynamically created PictureBox called "angle". And use it like that: PictureBox.angle = 20; Is there a way to do that? Any help is appreciated.

Create a custom component (Right click Project -> Add -> Component). Change it to inherit from PictureBox , and add the property.

using System.ComponentModel;

namespace Playground.WinForms
{
    public partial class MyCustomPictureBox : PictureBox
    {
        public int Angle { get; set; }

        public MyCustomPictureBox()
        {
            InitializeComponent();
        }

        public MyCustomPictureBox(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }
    }
}

When you build your WinForms solution, it will appear in your Toolbox, at which point your can drag it onto your form, or create one dynamically as you mentioned above.

在此处输入图像描述

var v = new { Amount = 108, Message = "Hello" };

// Rest the mouse pointer over v.Amount and v.Message in the following // statement to verify that their inferred types are int and string. 
Console.WriteLine(v.Amount + v.Message);

Try use anonynous types

https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/types/anonymous-types

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