1

I want to design a panel like properties window of visual studio in my winform application. Infact i want to have a grid that can accept a combobox as one of its cells,so user could select one of its items instead of typing.

I know it is possible in wpf but i want to know if there is any way to do this in winform app?

Edit: I'm looking for a way to show the result of FooForm in this example just in front of FooProperty.

Note: I can not place screen shot of my real application here! but i have something like the following picture in my property grid and i want to show the name of the selected Background Image as value of BackgroudImage property that specified in the red rectangle instead of (none) value.

enter image description here

please tell me if there is a way to do this?

Community
  • 1
  • 1
M_Mogharrabi
  • 1,369
  • 5
  • 29
  • 57
  • what have you tried? Did you ever make a goole search http://stackoverflow.com/questions/9226896/using-a-propertygrid-in-c-sharp-winforms-to-store-information-in-a-grid ? – Steve B Sep 23 '13 at 07:58
  • 1
    I think what you want is the `PropertyGrid` control. It will read all the properties of an object displaying and allowing user to change the properties at runtime like as you do with `Properties Window` at design time. – King King Sep 23 '13 at 07:58
  • 1
    I have no idea why this question was placed "on hold" - the question is entirely self-explanatory and fully contained, IMO. It certainly is not "off-topic", which was the selected reason. – Marc Gravell Sep 23 '13 at 09:37

3 Answers3

3

You can use System.Windows.Forms.PropertyGrid control. You'll find plenty of examples on the internet.

Ovidiu
  • 1,407
  • 12
  • 11
3

A grid can be provided via PropertyGrid; this uses the System.ComponentModel implementation, which is very flexible but quite complex. However, to provide suggested values - you use the GetStandardValues method of TypeConverter. Full example:

(edit: needs CanConvertFrom / ConvertFrom to work as combo)

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

namespace WindowsFormsApplication1
{

    public class MyType
    {
        [TypeConverter(typeof(GiveMeOptionsConverter))]
        public string SomeProperty {get;set;}


        private class GiveMeOptionsConverter : TypeConverter
        {
            public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
            {
                return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
            }
            public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
            {
                if (value is string) return value;
                return base.ConvertFrom(context, culture, value);
            }
            public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
            {
                return true;
            }
            public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
            {
                return false; // true is drop-down; false is combo
            }
            public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
            {
                // this gives you the `MyType` instance if you need it
                var obj = (MyType)context.Instance;

                return new StandardValuesCollection(
                    new[] { "abc", "def", "ghi" });
            }
        }
    }
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            using (var grid = new PropertyGrid {
                Dock = DockStyle.Fill,
                SelectedObject = new MyType()
            })
            using (var form = new Form { Controls = { grid } })
            {
                Application.Run(form);
            }

        }
    }
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thanks a lot, That was worked well.But now i have another question; How can i add something like "Items" property of ComboBox control to my propertygrid? I mean i want to have a textbox with a browse button next to it as a property of propertygrid, that a form opens when the button will be clicked. – M_Mogharrabi Sep 30 '13 at 08:47
  • 1
    @M_Mogharrabi that would require a custom `UITypeEditor` implementation associated via `[TypeEditor(...)]`, with `GetEditStyle()` returning `UITypeEditorEditStyle.Modal` – Marc Gravell Sep 30 '13 at 08:59
  • Thanks again,I have tried your solution in http://stackoverflow.com/questions/1016239/how-to-create-custom-propertygrid-editor-item-which-opens-a-form , But the problem is that i do not want to have expandableObject.Infact i want to show the result of FooForm just in front of FooProperty not as the value of Bar property. – M_Mogharrabi Sep 30 '13 at 10:21
  • @M_Mogharrabi you don't need to use expandable object converter at all; the type-editor is entirely separate – Marc Gravell Sep 30 '13 at 10:28
  • But i could not show the result of FooForm as the value of FooProperty! I'm looking for a way to do this! (Note : I am too new with TypeEditor) – M_Mogharrabi Sep 30 '13 at 10:44
  • OK Marc, may i ask another question? How can i set the value of a combo griditem? I do NOT mean the default value! but also i mean the value that was selected in previous time. – M_Mogharrabi Oct 02 '13 at 07:05
  • @M_Mogharrabi sorry, can you rephrase that? I couldn't follow exactly what you were trying to do. Maybe a screenshot? – Marc Gravell Oct 02 '13 at 07:06
  • Your welcome Marc,I have found the solution of my last question but i have my Penultimate question yet.I have updated my question. – M_Mogharrabi Oct 02 '13 at 11:11
  • My question has been answered in http://www.codeproject.com/Questions/661406/Problem-with-propertygrid?cmt=520428#cmt2_661406 by johannesnestler. – M_Mogharrabi Oct 05 '13 at 12:53
1

You can use a PropertyGrid and a custom type editor for doing that.

Here you have a detailed explanation of what you can do with a property grid, and how do a custom UI type editor.

http://msdn.microsoft.com/en-us/library/aa302326.aspx

Blau
  • 5,742
  • 1
  • 18
  • 27
  • Actually, a custom type editor (`UITypeEditor` etc) won't directly help with this... this is done in `TypeConverter` – Marc Gravell Sep 23 '13 at 08:14