为编辑器提供基类,这些编辑器使用模式对话框来显示与 ActiveX 控件的属性页相似的属性页。
有关此类型所有成员的列表,请参阅 WindowsFormsComponentEditor 成员。
System.Object
   System.ComponentModel.ComponentEditor
      System.Windows.Forms.Design.WindowsFormsComponentEditor
         System.Web.UI.Design.WebControls.BaseDataListComponentEditor
[C#]
public abstract class WindowsFormsComponentEditor : ComponentEditor

线程安全

此类型的所有公共静态(Visual Basic 中为 Shared)成员是线程安全的。但不保证任何实例成员是线程安全的。

示例

[Visual Basic, C#, C++] 以下代码示例阐释了一个 WindowsFormsComponentEditor 实现示例。

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
using System.Windows.Forms.Design;

// This example demonstrates how to implement a component editor that hosts 
// component pages and associate it with a component. This example also 
// demonstrates how to implement a component page that provides a panel-based 
// control system and Help keyword support.
namespace ComponentEditorExample
{
    
// The ExampleComponentEditor displays two ExampleComponentEditorPage pages.
    public class ExampleComponentEditor : System.Windows.Forms.Design.WindowsFormsComponentEditor
    
{
        
// This method override returns an type array containing the type of 
        
// each component editor page to display.
        protected override Type[] GetComponentEditorPages()
        

            
return new Type[] typeof(ExampleComponentEditorPage), 
                                
typeof(ExampleComponentEditorPage) }

        }

    
        
// This method override returns the index of the page to display when the 
        
// component editor is first displayed.
        protected override int GetInitialComponentEditorPageIndex()
        

            
return 1
        }

    }

    
    
// This example component editor page type provides an example 
    
// ComponentEditorPage implementation.
    internal class ExampleComponentEditorPage : System.Windows.Forms.Design.ComponentEditorPage
    
{
        Label l1; 
        Button b1; 
        PropertyGrid pg1;

        
// Base64-encoded serialized image data for the required component editor page icon.
        string icon = "AAEAAAD/////AQAAAAAAAAAMAgAAAFRTeXN0ZW0uRHJhd2luZywgVmVyc2lvbj0xLjAuNTAwMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWIwM2Y1ZjdmMTFkNTBhM2EFAQAAABNTeXN0ZW0uRHJhd2luZy5JY29uAgAAAAhJY29uRGF0YQhJY29uU2l6ZQcEAhNTeXN0ZW0uRHJhd2luZy5TaXplAgAAAAIAAAAJAwAAAAX8////E1N5c3RlbS5EcmF3aW5nLlNpemUCAAAABXdpZHRoBmhlaWdodAAACAgCAAAAAAAAAAAAAAAPAwAAAD4BAAACAAABAAEAEBAQAAAAAAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAABAAAAAAAAAAAACAAACAAAAAgIAAgAAAAIAAgADExAAAgICAAMDAwAA+iPcAY77gACh9kwD/AAAAndPoADpw6wD///8AAAAAAAAAAAAHd3d3d3d3d8IiIiIiIiLHKIiIiIiIiCco///////4Jyj5mfIvIvgnKPnp////+Cco+en7u7v4Jyj56f////gnKPmZ8i8i+Cco///////4JyiIiIiIiIgnJmZmZmZmZifCIiIiIiIiwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACw==";

        
public ExampleComponentEditorPage()
        
{
            
// Initialize the page, which inherits from Panel, and its controls.
            this.Size = new Size( 400250 );            
            
this.Icon = DeserializeIconFromBase64Text(icon);
            
this.Text = "Example Page";
            
            b1 
= new Button();
            b1.Size 
= new Size(20020);
            b1.Location 
= new Point(2000);
            b1.Text 
= "Set a random background color";
            b1.Click 
+= new EventHandler(this.randomBackColor);
            
this.Controls.Add( b1 );

            l1 
= new Label();
            l1.Size 
= new Size(19020);
            l1.Location 
= new Point(42);
            l1.Text 
= "Example Component Editor Page";
            
this.Controls.Add( l1 );

            pg1 
= new PropertyGrid();
            pg1.Size 
= new Size(400280);
            pg1.Location 
= new Point(0,30);
            
this.Controls.Add( pg1 );
        }

        
        
// This method indicates that the Help button should be enabled for this 
        
// component editor page.
        public override bool SupportsHelp()
        

            
return true
        }


        
// This method is called when the Help button for this component editor page is pressed.
        
// This implementation uses the IHelpService to show the Help topic for a sample keyword.
        public override void ShowHelp()
        
{
            
// The GetSelectedComponent method of a ComponentEditorPage retrieves the
            
// IComponent associated with the WindowsFormsComponentEditor.
            IComponent selectedComponent = this.GetSelectedComponent();

            
// Retrieve the Site of the component, and return if null.
            ISite componentSite = selectedComponent.Site;
            
if(componentSite == null)
                
return;
 
            
// Acquire the IHelpService to display a help topic using a indexed keyword lookup.
            IHelpService helpService = (IHelpService)componentSite.GetService(typeof(IHelpService));
            
if (helpService != null)
                helpService.ShowHelpFromKeyword(
"System.Windows.Forms.ComboBox");
        }


        
// The LoadComponent method is raised when the ComponentEditorPage is displayed.
        protected override void LoadComponent()
        

            
this.pg1.SelectedObject = this.Component; 
        }

    
        
// The SaveComponent method is raised when the WindowsFormsComponentEditor is closing 
        
// or the current ComponentEditorPage is closing.
        protected override void SaveComponent()
        
{
        }


        
// If the associated component is a Control, this method sets the BackColor to a random color.
        
// This method is invoked by the button on this ComponentEditorPage.
        private void randomBackColor(object sender, EventArgs e)
        
{
            
iftypeof(System.Windows.Forms.Control).IsAssignableFrom( this.Component.GetType() ) )
            
{
                
// Sets the background color of the Control associated with the
                
// WindowsFormsComponentEditor to a random color.
                Random rnd = new Random();
                ((System.Windows.Forms.Control)
this.Component).BackColor = 
                    Color.FromArgb(rnd.Next(
255), rnd.Next(255), rnd.Next(255));
                pg1.Refresh();
            }

        }


        
// This method can be used to retrieve an Icon from a block 
        
// of Base64-encoded text.
        private Icon DeserializeIconFromBase64Text(string text)
        
{
            Icon img 
= null;
            
byte[] memBytes = Convert.FromBase64String(text);
            IFormatter formatter 
= new BinaryFormatter();
            MemoryStream stream 
= new MemoryStream(memBytes);
            img 
= (Icon)formatter.Deserialize(stream);
            stream.Close();
            
return img;
        }

    }

    
    
// This example control is associated with the ExampleComponentEditor 
    
// through the following EditorAttribute.
    [EditorAttribute(typeof(ExampleComponentEditor), typeof(ComponentEditor))]
    
public class ExampleUserControl : System.Windows.Forms.UserControl
    
{
    }

}




posted on 2005-07-07 09:20  蚂蚁  阅读(520)  评论(1)    收藏  举报