Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
GeneralRe: Can we only store Hours and minutes in DateTime Pin
Garth J Lancaster6-Nov-14 17:47
professionalGarth J Lancaster6-Nov-14 17:47 
AnswerRe: Can we only store Hours and minutes in DateTime Pin
OriginalGriff6-Nov-14 22:44
mveOriginalGriff6-Nov-14 22:44 
GeneralRe: Can we only store Hours and minutes in DateTime Pin
Garth J Lancaster6-Nov-14 22:58
professionalGarth J Lancaster6-Nov-14 22:58 
GeneralRe: Can we only store Hours and minutes in DateTime Pin
OriginalGriff6-Nov-14 23:30
mveOriginalGriff6-Nov-14 23:30 
AnswerRe: Can we only store Hours and minutes in DateTime Pin
OriginalGriff6-Nov-14 22:48
mveOriginalGriff6-Nov-14 22:48 
AnswerRe: Can we only store Hours and minutes in DateTime Pin
V.6-Nov-14 23:00
professionalV.6-Nov-14 23:00 
AnswerRe: Can we only store Hours and minutes in DateTime Pin
jschell7-Nov-14 12:43
jschell7-Nov-14 12:43 
QuestionChange TypeDescriptor of property in runtime Pin
Mc_Topaz6-Nov-14 3:49
Mc_Topaz6-Nov-14 3:49 
Hello!

I have a PropertyGrid in an application displaying some properties in a class. One property has it's own TypeConverter to display an array of strings.

Issue
I would like to change the TypeConverter of the property in runtime.

Example
I have included a simple example code. You should be able to just paste the code in a Windows Forms Application, compile and run. Remember to create a PropertyGrid (propertyGrid1) and a button (button1) in the Form and add the button's click event.

Notice the two static string arrays in the Form1 class. These arrays are used with the TypeConverters NameList and SentenceList.

When the application start you will see the PropertyGrid displaying the name list in the property called Item. When you click the button I want the list of names to change to the list of sentences instead. I imagined the SwopList() method in the Props class should handle this. I call the SwopList() method with the button's click event.

In the SwopList() method I fetch the PropertyDescriptor object of the Item property. Then I can see what TypeConvert is used with the property PropertyDescriptor.Converter. From that point I just want to swop the TypeConverter (like the code I have commented out). But this don't work as PropertyDescriptor.Converter is read only Frown | :(

Any solution?

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public static string[] Names = new string[] {"Jon Smith", "Fred Perry", "Foo Bar" };
        public static string[] Sentences = new string[] { "Hi hello!", "Bye", "Peace and long life", "Live long and prosper" };

        Props properties = new Props();

        public Form1()
        {
            InitializeComponent();

            propertyGrid1.SelectedObject = properties;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            properties.SwopList();
            propertyGrid1.Refresh();
        }
    }

    public class Props
    {
        string name = Form1.Names[0];
        string sentence = Form1.Sentences[0];
        string item;

        public Props()
        {
            item = name;
        }

        public void SwopList()
        {
            PropertyDescriptorCollection pdc = System.ComponentModel.TypeDescriptor.GetProperties(this);
            PropertyDescriptor pd = pdc["Item"];

            if (pd.Converter is NameList)
            {
                item = sentence;
                //pd.Converter = typeof(SentenceList);  // Don't work
            }
            else if (pd.Converter is SentenceList)
            {
                item = name;
                //pd.Converter = typeof(NameList);  // Don't work
            }
        }

        [TypeConverter(typeof(NameList))]
        public string Item
        {
            get { return item; }
            set { item = value; }
        }
    }

    public class NameList : TypeConverter
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return new StandardValuesCollection(Form1.Names);
        }
    }

    public class SentenceList : TypeConverter
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;    // Display Drop-down menu
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return new StandardValuesCollection(Form1.Sentences);
        }
    }
}


Best regards,
/Steffe
AnswerRe: Change TypeDescriptor of property in runtime Pin
Eddy Vluggen6-Nov-14 9:45
professionalEddy Vluggen6-Nov-14 9:45 
GeneralRe: Change TypeDescriptor of property in runtime Pin
Mc_Topaz7-Nov-14 2:59
Mc_Topaz7-Nov-14 2:59 
GeneralRe: Change TypeDescriptor of property in runtime Pin
Eddy Vluggen7-Nov-14 3:00
professionalEddy Vluggen7-Nov-14 3:00 
QuestionImage Based Automation Pin
kunteshptechnosoft6-Nov-14 1:07
professionalkunteshptechnosoft6-Nov-14 1:07 
AnswerRe: Image Based Automation Pin
Eddy Vluggen6-Nov-14 9:48
professionalEddy Vluggen6-Nov-14 9:48 
GeneralRe: Image Based Automation Pin
kunteshptechnosoft6-Nov-14 17:15
professionalkunteshptechnosoft6-Nov-14 17:15 
GeneralRe: Image Based Automation Pin
kunteshptechnosoft6-Nov-14 17:46
professionalkunteshptechnosoft6-Nov-14 17:46 
GeneralRe: Image Based Automation Pin
Eddy Vluggen7-Nov-14 3:00
professionalEddy Vluggen7-Nov-14 3:00 
GeneralRe: Image Based Automation Pin
kunteshptechnosoft9-Nov-14 17:21
professionalkunteshptechnosoft9-Nov-14 17:21 
QuestionMicrosoft certification Pin
Mukesh Ghosh6-Nov-14 1:07
Mukesh Ghosh6-Nov-14 1:07 
AnswerRe: Microsoft certification Pin
Richard MacCutchan6-Nov-14 1:28
mveRichard MacCutchan6-Nov-14 1:28 
GeneralRe: Microsoft certification Pin
Mukesh Ghosh6-Nov-14 17:11
Mukesh Ghosh6-Nov-14 17:11 
GeneralRe: Microsoft certification Pin
Richard MacCutchan6-Nov-14 22:05
mveRichard MacCutchan6-Nov-14 22:05 
Questionfile based username and pass Pin
techker25-Nov-14 11:16
techker25-Nov-14 11:16 
AnswerRe: file based username and pass Pin
Mycroft Holmes5-Nov-14 12:05
professionalMycroft Holmes5-Nov-14 12:05 
AnswerRe: file based username and pass Pin
Richard MacCutchan5-Nov-14 22:31
mveRichard MacCutchan5-Nov-14 22:31 
GeneralRe: file based username and pass Pin
techker26-Nov-14 0:34
techker26-Nov-14 0:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.