3

Possible Duplicate:
Conditional “Browsable” Attribute

I define AppSettings class that have a few properties. In my form, When I click Button1, I want show property 1 and 2(1,2 is show, Other properties are hide or not displayed), When click Button2, I want show property 2 and 3(1 is hide, 2,3 are show, Other properties are hide or not displayed), How can I do it ?

public class AppSettings
{
    [BrowsableAttribute(true), CategoryAttribute("Document Settings"), DefaultValueAttribute(true)]
    public bool SaveOnClose{ get; set; }

    [BrowsableAttribute(true), CategoryAttribute("Global Settings"), ReadOnlyAttribute(true), DefaultValueAttribute("Welcome to AppDev!")]
    public string GreetingText { get; set; }

    [BrowsableAttribute(true), DescriptionAttribute("The rate in milliseconds that the text will repeat."), CategoryAttribute("Global Settings"), DefaultValueAttribute(10)]
    public int MaxRepeatRate { get; set; }

    [BrowsableAttribute(true), CategoryAttribute("Global Settings"), DefaultValueAttribute(4)]
    public int ItemsInMRUList { get; set; }

    [BrowsableAttribute(true), DefaultValueAttribute(false)]
    public bool SettingsChanged { get; set; }

    [BrowsableAttribute(true), CategoryAttribute("Version"), DefaultValueAttribute("1.0"), ReadOnlyAttribute(true)]
    public string AppVersion { get; set; }
}

I want change dynamically BrowseAttribute to true or false. How can I do it ?

Form code is :

AppSettings AppSet = new AppSettings();

AppSet.AppVersion = "2.3";
AppSet.SaveOnClose = true;
AppSet.GreetingText = "Welcome to Dar!";
AppSet.ItemsInMRUList = 4;
AppSet.MaxRepeatRate = 10;
AppSet.SettingsChanged = false;

...

propertyGrid1.SelectedObject = AppSet;

this change has error:

public static bool state = true;
BrowsableAttribute(state)

error:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

Community
  • 1
  • 1
Ali Ahmadi
  • 2,387
  • 4
  • 31
  • 48

1 Answers1

6

For filtering, I would just change the BrowsableAttributes of the PropertyGrid. In the following, I:

  • define a custom attribute, [DisplayMode(...)], which describes when something should be visible
    • override IsMatch to indicate when attributes should be considered equivalent
  • decorate some of the settings on your type, AppSettings, with the attribute
  • change the BrowsableAttributes on the grid, specifying a particular DisplayModeAttribute, and display
  • repeat with a different set enabled

Here's the code:

using System.ComponentModel;
using System;
using System.Windows.Forms;

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class DisplayModeAttribute : Attribute
{
    private readonly string mode;
    public DisplayModeAttribute(string mode)
    {
        this.mode = mode ?? "";
    }
    public override bool Match(object obj)
    {
        var other = obj as DisplayModeAttribute;
        if (other == null) return false;

        if (other.mode == mode) return true;

        // allow for a comma-separated match, in either direction
        if (mode.IndexOf(',') >= 0)
        {
            string[] tokens = mode.Split(',');
            if (Array.IndexOf(tokens, other.mode) >= 0) return true;
        }
        else if (other.mode.IndexOf(',') >= 0)
        {
            string[] tokens = other.mode.Split(',');
            if (Array.IndexOf(tokens, mode) >= 0) return true;
        }
        return false;
    }
}

public class AppSettings
{
    [DisplayMode("A"), CategoryAttribute("Document Settings"), DefaultValueAttribute(true)]
    public bool SaveOnClose { get; set; }

    [DisplayMode("A,B")]
    [CategoryAttribute("Global Settings"), ReadOnlyAttribute(true), DefaultValueAttribute("Welcome to AppDev!")]
    public string GreetingText { get; set; }

    [DisplayMode("B"), BrowsableAttribute(true), DescriptionAttribute("The rate in milliseconds that the text will repeat."), CategoryAttribute("Global Settings"), DefaultValueAttribute(10)]
    public int MaxRepeatRate { get; set; }

    [BrowsableAttribute(true), CategoryAttribute("Global Settings"), DefaultValueAttribute(4)]
    public int ItemsInMRUList { get; set; }

    [BrowsableAttribute(true), DefaultValueAttribute(false)]
    public bool SettingsChanged { get; set; }

    [BrowsableAttribute(true), CategoryAttribute("Version"), DefaultValueAttribute("1.0"), ReadOnlyAttribute(true)]
    public string AppVersion { get; set; }
}
static class Program
{
    [STAThread]
    static void Main()
    {
        using (var form = new Form())
        using (var grid = new PropertyGrid())
        {
            grid.Dock = DockStyle.Fill;
            grid.SelectedObject = new AppSettings();
            grid.BrowsableAttributes = new AttributeCollection(
                new DisplayModeAttribute("A"));
            form.Controls.Add(grid);
            form.ShowDialog();

            grid.BrowsableAttributes = new AttributeCollection(
                new DisplayModeAttribute("B"));
            form.ShowDialog();
        }
    }
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900