C # custom designer -- select the editor for file name,

Source: Internet
Author: User

C # custom designer -- select the editor for file name,
 C # custom designer-file name selection Editor

During the summer vacation, I used MCI to encapsulate a class for playing music. Recently I learned GDI + and want to make this class a custom control. Then there is an attribute in the process, as follows:

public string FileName{get { return fileName; }set { fileName = value; }}private string fileName;

After compilation, add it to the form, and adjust it to the property editing box of the control. You can see that this property is available in miscellaneous, And we can enter the file name in the box below, but this is a little unfriendly. I thought it would be impossible for me to create a file selection box somewhere. I searched through Google and found a design-time feature --System. ComponentModel.EditorIn fact, it is a defined class. One of the constructors is as follows:

//// Summary: // use the type and base type to initialize a new instance of the System. ComponentModel. EditorAttribute class. //// Parameter: // type: // a System. Type that represents the editor type. //// BaseType: // The System. Type of the base class or interface used as the editor's search key. This class must be System. Drawing. Design. UITypeEditor or derived from it. Public EditorAttribute (Type type, Type baseType );

The first parameter is the type of the desired editor. The second parameter is the base class type of the editor. The base class is generally UITypeEditor.

So, I first add the reference

Double-click the reference, open the object browser, and find a defined class in it. Someone else provided it (think about it too, even I have such a requirement ), you don't have to implement it yourself. Use it directly!

[System.ComponentModel.Editor(typeof(FileNameEditor), typeof(UITypeEditor))]public string FileName{get { return fileName; }set { fileName = value; }}private string fileName;

This Code only adds the Editor feature on the basis of the above, and then looks at the effect: There is a button added, when the button is clicked, it will pop up:

Isn't that what I want.

But people are always dissatisfied, and I immediately have a new desire-file filtering. Because this selection box Selects all files, I wonder if I can filter them out. If OpenFileDialog is used, it is so easy. You only need to set the Filter attribute, but the key is not. Just as I was confused, I found that the pop-up box is not only similar to OpenFileDialog, but also similar, so I guess they must have a relationship. Then I checked the FileNameEditor-related methods and hoped to find some clues. Huang Tianyan had no worries, and finally let me see the dawn.

protected virtual void InitializeDialog(System.Windows.Forms.OpenFileDialog openFileDialog)

Haha, it turns out that the parameter of a function is OpenFileDialog, and this function is still a virtual function. I just need to inherit the FileNameEditor class and then override this method, the effect is not for me!

Add a class AudioFileNameEditor to inherit the FileNameEditor:

Public class AudioFileNameEditor: FileNameEditor {protected override void InitializeDialog (System. windows. forms. openFileDialog openFileDialog) {base. initializeDialog (openFileDialog); openFileDialog. filter = "Audio file | *. mp3 ";}}

Modify the Property Code:

[System.ComponentModel.Editor(typeof(AudioFileNameEditor), typeof(UITypeEditor))]public string FileName{get { return fileName; }set { fileName = value; }}private string fileName;

Then let's look at the effect:

So far, we have basically implemented the desired functions.



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.