WindowsFormsComponentEditor 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为编辑器提供基类,这些编辑器使用模式对话框来显示与 ActiveX 控件的属性页相似的属性页。
public ref class WindowsFormsComponentEditor abstract : System::ComponentModel::ComponentEditor
public abstract class WindowsFormsComponentEditor : System.ComponentModel.ComponentEditor
type WindowsFormsComponentEditor = class
inherit ComponentEditor
Public MustInherit Class WindowsFormsComponentEditor
Inherits ComponentEditor
- 继承
- 派生
示例
下面的代码示例演示了一个示例 WindowsFormsComponentEditor 实现。
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
#using <System.dll>
using namespace System;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::Collections;
using namespace System::Drawing;
using namespace System::IO;
using namespace System::Runtime::Serialization;
using namespace System::Runtime::Serialization::Formatters::Binary;
using namespace System::Windows::Forms;
using namespace 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.
ref class ExampleComponentEditorPage;
// The ExampleComponentEditor displays two ExampleComponentEditorPage pages.
public ref class ExampleComponentEditor: public System::Windows::Forms::Design::WindowsFormsComponentEditor
{
protected:
// This method override returns an type array containing the type of
// each component editor page to display.
virtual array<Type^>^ GetComponentEditorPages() override
{
array<Type^>^temp0 = {ExampleComponentEditorPage::typeid,ExampleComponentEditorPage::typeid};
return temp0;
}
// This method override returns the index of the page to display when the
// component editor is first displayed.
protected:
virtual int GetInitialComponentEditorPageIndex() override
{
return 1;
}
};
// This example component editor page type provides an example
// ComponentEditorPage implementation.
private ref class ExampleComponentEditorPage: public System::Windows::Forms::Design::ComponentEditorPage
{
private:
Label^ l1;
Button^ b1;
PropertyGrid^ pg1;
// Base64-encoded serialized image data for the required component editor page icon.
String^ icon;
public:
ExampleComponentEditorPage()
{
String^ temp = "AAEAAAD/////AQAAAAAAAAAMAgAAAFRTeXN0ZW0uRHJhd2luZywgVmVyc2lvbj0xLjAuNTAwMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWIwM2Y1ZjdmMTFkNTBhM2EFAQAAABNTeXN0ZW0uRHJhd2luZy5JY29uAgAAAAhJY29uRGF0Y"
"QhJY29uU2l6ZQcEAhNTeXN0ZW0uRHJhd2luZy5TaXplAgAAAAIAAAAJAwAAAAX8////E1N5c3RlbS5EcmF3aW5nLlNpemUCAAAABXdpZHRoBmhlaWdodAAACAgCAAAAAAAAAAAAAAAPAwAAAD4BAAACAAABAAEAEBAQAAAAAAAoAQAAFgAAACgAAAAQAAAAIA"
"AAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAABAAAAAAAAAAAACAAACAAAAAgIAAgAAAAIAAgADExAAAgICAAMDAwAA+iPcAY77gACh9kwD/AAAAndPoADpw6wD///8AAAAAAAAAAAAHd3d3d3d3d8IiIiIiIiLHKIiIiIiIiCco///////4Jyj5mfIvIvgnKPn"
"p////+Cco+en7u7v4Jyj56f////gnKPmZ8i8i+Cco///////4JyiIiIiIiIgnJmZmZmZmZifCIiIiIiIiwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACw==";
icon = temp;
// Initialize the page, which inherits from Panel, and its controls.
this->Size = System::Drawing::Size( 400, 250 );
this->Icon = DeserializeIconFromBase64Text( icon );
this->Text = "Example Page";
b1 = gcnew Button;
b1->Size = System::Drawing::Size( 200, 20 );
b1->Location = Point(200,0);
b1->Text = "Set a random background color";
b1->Click += gcnew EventHandler( this, &ExampleComponentEditorPage::randomBackColor );
this->Controls->Add( b1 );
l1 = gcnew Label;
l1->Size = System::Drawing::Size( 190, 20 );
l1->Location = Point(4,2);
l1->Text = "Example Component Editor Page";
this->Controls->Add( l1 );
pg1 = gcnew PropertyGrid;
pg1->Size = System::Drawing::Size( 400, 280 );
pg1->Location = Point(0,30);
this->Controls->Add( pg1 );
}
// This method indicates that the Help button should be enabled for this
// component editor page.
virtual bool SupportsHelp() override
{
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:
virtual void ShowHelp() override
{
// 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 == nullptr )
return;
// Acquire the IHelpService to display a help topic using a indexed keyword lookup.
IHelpService^ helpService = dynamic_cast<IHelpService^>(componentSite->GetService( IHelpService::typeid ));
if ( helpService != nullptr )
helpService->ShowHelpFromKeyword( "System.Windows.Forms.ComboBox" );
}
protected:
// The LoadComponent method is raised when the ComponentEditorPage is displayed.
virtual void LoadComponent() override
{
this->pg1->SelectedObject = this->Component;
}
// The SaveComponent method is raised when the WindowsFormsComponentEditor is closing
// or the current ComponentEditorPage is closing.
virtual void SaveComponent() override {}
private:
// 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.
void randomBackColor( Object^ /*sender*/, EventArgs^ /*e*/ )
{
if ( System::Windows::Forms::Control::typeid->IsAssignableFrom( this->::Component::GetType() ) )
{
// Sets the background color of the Control associated with the
// WindowsFormsComponentEditor to a random color.
Random^ rnd = gcnew Random;
(dynamic_cast<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.
System::Drawing::Icon^ DeserializeIconFromBase64Text( String^ text )
{
System::Drawing::Icon^ img = nullptr;
array<Byte>^memBytes = Convert::FromBase64String( text );
IFormatter^ formatter = gcnew BinaryFormatter;
MemoryStream^ stream = gcnew MemoryStream( memBytes );
img = dynamic_cast<System::Drawing::Icon^>(formatter->Deserialize( stream ));
stream->Close();
return img;
}
};
// This example control is associated with the ExampleComponentEditor
// through the following EditorAttribute.
[EditorAttribute(ExampleComponentEditor::typeid,ComponentEditor::typeid)]
public ref class ExampleUserControl: public System::Windows::Forms::UserControl{};
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( 400, 250 );
this.Icon = DeserializeIconFromBase64Text(icon);
this.Text = "Example Page";
b1 = new Button();
b1.Size = new Size(200, 20);
b1.Location = new Point(200, 0);
b1.Text = "Set a random background color";
b1.Click += new EventHandler(this.randomBackColor);
this.Controls.Add( b1 );
l1 = new Label();
l1.Size = new Size(190, 20);
l1.Location = new Point(4, 2);
l1.Text = "Example Component Editor Page";
this.Controls.Add( l1 );
pg1 = new PropertyGrid();
pg1.Size = new Size(400, 280);
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)
{
if( typeof(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
{
}
}
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Collections
Imports System.Drawing
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Windows.Forms
Imports 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.
' The ExampleComponentEditor displays two ExampleComponentEditorPage pages.
Public Class ExampleComponentEditor
Inherits System.Windows.Forms.Design.WindowsFormsComponentEditor
' This method override returns an type array containing the type of
' each component editor page to display.
Protected Overrides Function GetComponentEditorPages() As Type()
Return New Type() {GetType(ExampleComponentEditorPage), GetType(ExampleComponentEditorPage)}
End Function
' This method override returns the index of the page to display when the
' component editor is first displayed.
Protected Overrides Function GetInitialComponentEditorPageIndex() As Integer
Return 1
End Function
Public Overloads Overrides Function EditComponent(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal component As Object) As Boolean
End Function
End Class
' This example component editor page type provides an example
' ComponentEditorPage implementation.
Friend Class ExampleComponentEditorPage
Inherits System.Windows.Forms.Design.ComponentEditorPage
Private l1 As Label
Private b1 As Button
Private pg1 As PropertyGrid
' Base64-encoded serialized image data for the required component editor page icon.
Private icondata As String = "AAEAAAD/////AQAAAAAAAAAMAgAAAFRTeXN0ZW0uRHJhd2luZywgVmVyc2lvbj0xLjAuNTAwMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWIwM2Y1ZjdmMTFkNTBhM2EFAQAAABNTeXN0ZW0uRHJhd2luZy5JY29uAgAAAAhJY29uRGF0YQhJY29uU2l6ZQcEAhNTeXN0ZW0uRHJhd2luZy5TaXplAgAAAAIAAAAJAwAAAAX8////E1N5c3RlbS5EcmF3aW5nLlNpemUCAAAABXdpZHRoBmhlaWdodAAACAgCAAAAAAAAAAAAAAAPAwAAAD4BAAACAAABAAEAEBAQAAAAAAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAABAAAAAAAAAAAACAAACAAAAAgIAAgAAAAIAAgADExAAAgICAAMDAwAA+iPcAY77gACh9kwD/AAAAndPoADpw6wD///8AAAAAAAAAAAAHd3d3d3d3d8IiIiIiIiLHKIiIiIiIiCco///////4Jyj5mfIvIvgnKPnp////+Cco+en7u7v4Jyj56f////gnKPmZ8i8i+Cco///////4JyiIiIiIiIgnJmZmZmZmZifCIiIiIiIiwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACw=="
Public Sub New()
' Initialize the page, which inherits from Panel, and its controls.
Me.Size = New Size(400, 250)
Me.Icon = DeserializeIconFromBase64Text(icondata)
Me.Text = "Example Page"
b1 = New Button
b1.Size = New Size(200, 20)
b1.Location = New Point(200, 0)
b1.Text = "Set a random background color"
AddHandler b1.Click, AddressOf Me.randomBackColor
Me.Controls.Add(b1)
l1 = New Label
l1.Size = New Size(190, 20)
l1.Location = New Point(4, 2)
l1.Text = "Example Component Editor Page"
Me.Controls.Add(l1)
pg1 = New PropertyGrid
pg1.Size = New Size(400, 280)
pg1.Location = New Point(0, 30)
Me.Controls.Add(pg1)
End Sub
' This method indicates that the Help button should be enabled for this
' component editor page.
Public Overrides Function SupportsHelp() As Boolean
Return True
End Function
' 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 Overrides Sub ShowHelp()
' The GetSelectedComponent method of a ComponentEditorPage retrieves the
' IComponent associated with the WindowsFormsComponentEditor.
Dim selectedComponent As IComponent = Me.GetSelectedComponent()
' Retrieve the Site of the component, and return if null.
Dim componentSite As ISite = selectedComponent.Site
If componentSite Is Nothing Then
Return
End If
' Acquire the IHelpService to display a help topic using a indexed keyword lookup.
Dim helpService As IHelpService = CType(componentSite.GetService(GetType(IHelpService)), IHelpService)
If (helpService IsNot Nothing) Then
helpService.ShowHelpFromKeyword("System.Windows.Forms.ComboBox")
End If
End Sub
' The LoadComponent method is raised when the ComponentEditorPage is displayed.
Protected Overrides Sub LoadComponent()
Me.pg1.SelectedObject = Me.Component
End Sub
' The SaveComponent method is raised when the WindowsFormsComponentEditor is closing
' or the current ComponentEditorPage is closing.
Protected Overrides Sub SaveComponent()
End Sub
' 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 Sub randomBackColor(ByVal sender As Object, ByVal e As EventArgs)
If GetType(System.Windows.Forms.Control).IsAssignableFrom(CType(Me.Component, Object).GetType()) Then
' Sets the background color of the Control associated with the
' WindowsFormsComponentEditor to a random color.
Dim rnd As New Random
CType(Me.Component, System.Windows.Forms.Control).BackColor = Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))
pg1.Refresh()
End If
End Sub
' This method can be used to retrieve an Icon from a block
' of Base64-encoded text.
Private Function DeserializeIconFromBase64Text(ByVal [text] As String) As icon
Dim img As Icon = Nothing
Dim memBytes As Byte() = Convert.FromBase64String([text])
Dim formatter As New BinaryFormatter
Dim stream As New MemoryStream(memBytes)
img = CType(formatter.Deserialize(stream), Icon)
stream.Close()
Return img
End Function
End Class
' This example control is associated with the ExampleComponentEditor
' through the following EditorAttribute.
<EditorAttribute(GetType(ExampleComponentEditor), GetType(ComponentEditor))> _
Public Class ExampleUserControl
Inherits System.Windows.Forms.UserControl
End Class
构造函数
WindowsFormsComponentEditor() |
初始化 WindowsFormsComponentEditor 类的新实例。 |
方法
EditComponent(ITypeDescriptorContext, Object) |
使用指定的上下文信息创建编辑器窗口,该窗口允许用户编辑指定的组件。 |
EditComponent(ITypeDescriptorContext, Object, IWin32Window) |
创建允许用户编辑指定组件的编辑器窗口。 |
EditComponent(Object) |
编辑该组件并返回一个表示是否修改了该组件的值。 (继承自 ComponentEditor) |
EditComponent(Object, IWin32Window) |
使用拥有指定组件的指定窗口来创建编辑器窗口,该窗口允许用户编辑该指定组件。 |
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetComponentEditorPages() |
获取与组件编辑器关联的组件编辑器页。 |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetInitialComponentEditorPageIndex() |
获取要显示的组件编辑器的初始组件编辑器页的索引。 |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |