Define how attributes are edited in the designer

Source: Internet
Author: User
Problem 1: I wrote a class myself. One of the attributes is a string type, which is used to save a file name. This class requires the user to modify the content runtime in PropertyGrid during use, the friendly method is, of course, to allow the customer to have "... "button, click it to open an openfiledialog, select a file and return, the full path of the file is placed in the Value box. how to achieve this goal?

For example, this class is as follows: public class class1
{
Public class1 ()
{
}

//

Private string _ s;
Public string s
{
Get {return this. _ s ;}
Set {this. _ s = value ;}
}
}

In this case, there is only a simple edit box behind the s attribute in PropertyGrid, so that "... "button, and you can select a file in the open file dialog box. You can use the EditorAttribute attribute to mark this attribute:
[Editor (typeof (System. Windows. Forms. Design. FileNameEditor ),
Typeof (System. Drawing. Design. UITypeEditor)]
Public string S
{
Get {return this. _ s ;}
Set {this. _ s = value ;}
}

The FileNameEditor class provides a dialog box for opening a file to edit the property value. This class is derived from the UITypeEditor class. UITypeEditor is the base class of all interfaces during design.

In this way, you can achieve the purpose of a file dialog box, but if you want to specify only one file type, such as "WAV" file, you have to derive FileNameEditor and override the relevant methods.
Public class SoundFileEditor: System. Windows. Forms. Design. FileNameEditor
{
Protected override void InitializeDialog (OpenFileDialog openFileDialog)
{
Base. InitializeDialog (openFileDialog );
// After the base class initial generation dialog box is complete, you can perform some operations on this dialog box.
OpenFileDialog. filter = "wav and vox file (*. wav ,*. vox) | *. wav ;*. vox | wav files (*. wav) | *. wav | vox files (*. vox) | *. vox | All files (*. *) | *. *";
}
}

Change the EditorAttribute of S in Class1 to the derived subclass. Now the file filter in the dialog box is changed to what you want.

You can even derive a subclass from UITypeEditor, and write a UI editing class that can pop up the "save file" dialog box. note that the public virtual new System of UITypeEditor is rewritten. object EditValue (System. componentModel. ITypeDescriptorContext context, System. IServiceProvider provider, System. object value)

Method. You can return the object you need.

Question 2: How to set a "Default Editor" for a class "?
For example, a class named SoundFileName is used to process audio file names. If the FileName attribute in Class1 is of the SoundFileName type, this attribute cannot be edited by default in the PropertyGrid editor, to enable the SoundFileName class to be edited in propertyGrid, a default editor must be specified for the SoundFileName attribute flag. if we still want to generate a SoundFileName object by specifying a file in the "open file" dialog box, we can write a subclass that is directly or indirectly derived from UITypeEditor, to edit the object content.

As shown in the following code, the class for editing the SoundFileName object is called SoundfileNameEditor. We will implement it later. Now let's first look at the implementation of SoundFileName. Pay attention to the mark before the class:

[EditorAttribute (typeof (SoundFileNameEitor ),
Typeof (System. Drawing. Design. UITypeEditor)]
Public class SoundFileName
{
Private string _ filename;
Public string FileName
{
Get {return _ filename ;}
}

Public SoundFileName (string s)
{
This. _ filename = s;
}
}

The value editor of this class is defined as the SoundFileNameEditor class. This class needs to be derived from UITypeEditor or its subclass, and its EditValue method should be rewritten to return a SoundFileName object.

To save trouble, I no longer derive from UITypeEditor, but from SoundFileEditor. SoundFileEditor has modified the file name filter, but the EditValue of SoundFileEditor returns a string, this time, you only need to modify the EditValue of SoundFileEditor.
Public class SoundFileNameEditor: SoundFileEditor
{
Public override object EditValue (ITypeDescriptorContext context, IServiceProvider provider, object value)
{
SoundFileName sfn = value as SoundFileName;

Return new SoundFileName (base. EditValue (context, provider, sfn = null? "": Sfn. FileName) as string );
}
}

Note: This EditValue always returns a new object, throwing away the previous object. You can determine whether to create a new object by checking whether the value is null, or just correct the file name, I will not talk about it here.

Now you can edit the SoundFileNmae object in PropertyGrid. In the editor, an open file dialog box is displayed. After selecting a file, a SoundFileName object is created based on the file name.

The last prompt is as follows: I don't know if you have noticed it. After selecting a file name and creating a SoundFileName object, the content in the edit box is "testanything. soundFileName ", the user does not look so happy or intuitive. You can rewrite the ToString () method of SoundFileName to display the content in any way.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.