简体   繁体   English

如何向UserControl添加一些属性?

[英]How can i add some properties to UserControl?

I have this UserControl of a listBox: 我有一个ListBox的UserControl:

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 Lightnings_Extractor
{
    public partial class ListBoxControl : UserControl
    {

        private List<int> m_itemIndexes = new List<int>();

        public ListBoxControl()
        {
            InitializeComponent();

            this.listBox1.SelectedIndex = 0;
        }

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            int index = listBox1.IndexFromPoint(e.X, e.Y);
            listBox1.SelectedIndex = index;

            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                if (m_itemIndexes.Contains(index))
                    return;

                m_itemIndexes.Add(index);
                DrawItem(index);
            }
            else if (e.Button == MouseButtons.Left)
            {
                if (!m_itemIndexes.Contains(index))
                    return;

                m_itemIndexes.Remove(index);
                DrawItem(index);
            }
        }

        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            bool coloring = m_itemIndexes.Contains(e.Index);
            bool selected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;

            if (coloring)
            {
                using (var brush = new SolidBrush(Color.Red))
                {
                    e.Graphics.FillRectangle(brush, e.Bounds);
                }
            }
            else
            {
                e.DrawBackground();
            }

            string item = listBox1.Items[e.Index].ToString();
            e.Graphics.DrawString(item, e.Font, selected || coloring ? Brushes.White : Brushes.Black, e.Bounds, StringFormat.GenericDefault);

            if (selected)
                e.DrawFocusRectangle();
        }

        private void DrawItem(int index)
        {
            Rectangle rectItem = listBox1.GetItemRectangle(index);
            listBox1.Invalidate(rectItem);
        }
    }
}

For example this line: 例如以下行:

using (var brush = new SolidBrush(Color.Red))

Now it set to Red. 现在将其设置为红色。 But i want the user to be able to change it to any color in any place in his code or in another form or class. 但是我希望用户能够在其代码中的任何位置或以其他形式或类将其更改为任何颜色。 And not only in this UserControl code. 不仅在此UserControl代码中。

How can i add properties like this ? 我如何添加这样的属性?

Just declare a property as you normally would: 只需像通常那样声明一个属性:

[Browsable(true)] //so it appears in object explorer
public Color MyListColor
{
  get { return m_MyListColor;}
  set 
  {
    m_MyListColor = value;
    Refresh();//or Update
  }
}

Once you set property to new value, it calls Refresh() that repaints control including your brushes. 将属性设置为新值后,它将调用Refresh()来重新绘制包括画笔在内的控件。

只需添加一个新属性。

public Brush Fill {get;set;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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