Click here to Skip to main content
15,943,148 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Afternoon Everyone,

I have started learning C# and I'm running into an issue with my project that uses a property grid. I have a directory with 16x16 png images. My end goal is to populate a combobox with the file name and image.

I have looked at several articles that a being populated off static values and list but i would need to read a directory to get the file names and images. IF someone could help get me on the right track that would be great. Below is the class i have written so far that will create an object to populate a property grid.

C#
using System;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using System.ComponentModel;
using System.Globalization;
using System.IO;

namespace PropertyGrid_Testing
{
    class PG_Program
    {
        public object Action(string DisplayName, string Type, int Index, string Icon, string File, string Arguments)
        {
            Action MyWorker = new Action();

            MyWorker.DisplayName = DisplayName;
            MyWorker.Type = Type;
            MyWorker.Index = Index;
            MyWorker.Icon = Icon;
            MyWorker.File = File;
            MyWorker.Arguments = Arguments;

            return MyWorker;
        }
    }

    internal class Action
    {

        [Description("This is the Display Name used in the Context Menu.")]
        [Category("Detials")]
        public string DisplayName { get; set; }

        [ReadOnly(true)]
        [Description("This is the Type of item listed in the context menu. There can be Group, Action, or SEP. This setting gets set duing the new item creation.")]
        [Category("Detials")]
        public string Type { get; set; }

        [Description("This is the index in wish you want the item to display.")]
        [Category("Detials")]
        public int Index { get; set; }

        [Description("This is icon used to reference the context menu item.")]
        [Category("Detials")]
        public string Icon { get; set; }

        [Description("This is item you wish to invoke. This can be anything, ex: exe, bat, png, xlsx...")]
        [Category("Action")]
        [BrowsableAttribute(true)]
        [EditorAttribute(typeof(SaveFileNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
        public string File { get; set; }

        [Description("(Optional) This references the arguments you wish to pass to the file you choose to invoke.")]
        [Category("Action")]
        public string Arguments { get; set; }
    }

    public class SaveFileNameEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (context == null || context.Instance == null || provider == null)
            {
                return base.EditValue(context, provider, value);
            }

            using (OpenFileDialog OFDialog = new OpenFileDialog())
            {
                if (value != null)
                {
                    OFDialog.InitialDirectory = System.IO.Path.GetDirectoryName(value.ToString());
                    OFDialog.FileName = value.ToString();
                }

                OFDialog.Title = "Select Action Item";
                OFDialog.Filter = "All files (*.*)|*.*";
                if (OFDialog.ShowDialog() == DialogResult.OK)
                {
                       value = OFDialog.FileName;
                }
            }

            return value;
        }
    }
}
Posted
Updated 1-Aug-15 6:42am
v2

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900