-1

I am trying to add two methods to the Controls class in C#, these classes being 'UpColor' and 'DownColor' so that I can set a buttons BackColor depending on whether the state of the button is 'up' or 'down'

I have tried using extension methods, however because extension methods are static is means that every time I select a different button it will use the previous buttons up/down color, instead of it's own.

Is there a way of adding to the Controls class so that I can get/set a Color?

I have decided to go down the route of adding a static class and to have a dictionary for each method. It's not what I wanted, but I should do the job..

    static class ButtonColors
{
    public static Dictionary<Control, Color> UpColor
    {
        get;
        set;
    }

    public static Dictionary<Control, Color> DownColor
    {
        get;
        set;
    }
}
4

2 に答える 2

3

それに応じてクラスを拡張する必要があります。

public ClassWithColors : Control {
  // extended implementation
  public string UpColor { get; set; }
  public string DownColor { get; set; }
  // assuming types, change as needed
}

ただし、どのコントロール クラスについて話しているのか明確ではないため、ここでは WinForms を想定しています。原則として、ASP.NET ユーザー コントロールなどについても同じです。

于 2013-05-30T10:40:51.167 に答える
0

ボタンのハンドルを取り、その色を変更する「メソッド」を作成してみてください。

于 2013-05-30T10:43:03.643 に答える