Question : Registry key property value

Hi.

I'm writing an app that gives the user the ability to create dummy controls at runtime and alter properties that are attached to those controls.  I've got a property grid for editing the properties and this all works great.

However, I'm having a little trouble with the next part.

The controls will be shown as part of an application that runs on a different PC, and each control will be "linked" to a registry value.  I've created a treeview where the user can build a trimmed version of the target machine's registry tree, in the hope that I can then use a dropdown property to select registry keys for each of the controls.

So, in a nutshell, I need a dropdown list for a property grid item, and I need to be able to modify this dropdown list at runtime.

Advice is welcome, even alternative suggestions are welcome, but I'd particularly like a code example.

Thanks in advance.

Answer : Registry key property value

Okay, so I've answered it myself.  The code example shows a class with a property that has the values aaa, bbb, ccc available in a dropdown in a property grid, but the code dynamically adds ddd & eee to the list and they can also be selected in the property grid.

I'm posting the solution in case anyone ever finds this thread whilst searching for the same thing.

Just create a new project, add a property grid to the form and paste the code in.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Dummy_PropertyGrid
{
    class MyConverter : TypeConverter
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            List<string> list = (context.Instance as Form1.myProp).MyList;
            StandardValuesCollection cols = new
            StandardValuesCollection(list);
            return cols;
        }
    }


    public partial class Form1 : Form
    {
        public class myProp
        {
            string myStr;

            [TypeConverter(typeof(MyConverter))]
            public string MyItem
            {
                get { return myStr; }
                set { myStr = value; }
            }

            List<string> list;

            [Browsable(false)]
            public List<string> MyList
            {
                get
                {
                    if (list == null)
                    {
                        list = new List<string>();
                        list.Add("aaa");
                        list.Add("bbb");
                        list.Add("ccc");
                    }
                    return list;
                }
            }
        }

        public Form1()
        {
            InitializeComponent();

            myProp obj = new myProp();

            obj.MyList.Add("ddd");
            obj.MyList.Add("eee");

            this.propertyGrid1.SelectedObject = obj;
        }
    }
}
Random Solutions  
 
programming4us programming4us