パフォーマンスを向上し、ほとんどの変更内容を元に戻せるようにするために、デザイン時の一連の操作をグループ化する手段を提供します。
名前空間: System.ComponentModel.Design
アセンブリ: System (system.dll 内)
構文
'宣言
Public MustInherit Class DesignerTransaction
Implements IDisposable
'使用
Dim instance As DesignerTransaction
public abstract class DesignerTransaction : IDisposable
public ref class DesignerTransaction abstract : IDisposable
public abstract class DesignerTransaction implements IDisposable
public abstract class DesignerTransaction implements IDisposable
解説
トランザクションを使用すると、後で元に戻せるように操作を追跡できます。トランザクションの実行中に行った変更は、トランザクションをキャンセルすることによって元に戻すことができます。この操作では、変更された各プロパティに変更前の値を設定することにより、それぞれの変更を自動的に元に戻す処理が試行されます。また、トランザクションを使用すると、画面への更新をトランザクションの完了まで遅らせることによって、一連の操作を実行している間のパフォーマンスを向上することもできます。
トランザクションの進行中に、TransactionOpening イベントと TransactionClosed イベントを待機することで、そのトランザクションが完了するまで処理を延期するコンポーネントもあります。たとえば、[プロパティ] ウィンドウの表示は、トランザクションが開始されてから終了するまでは更新されません。
トランザクションを使用して操作を元に戻せるようにしたり、複数の操作を実行したりするには、元に戻せるようにする各操作または一連の操作ごとに、デザイナで DesignerTransaction を作成します。一連の "元に戻す" イベントが正常に機能しなくなる可能性があるアクションをトランザクションの外部で実行しないように注意してください。
IDesignerHost の CreateTransaction メソッドを呼び出すと、新しい DesignerTransaction を取得できます。デザイナのトランザクション処理機構に正しく統合するためには、新しい DesignerTransaction を直接作成するのではなく、アクティブな IDesignerHost から各 DesignerTransaction を取得してください。
トランザクション内で操作を実行するには、最初にトランザクションを作成する必要があります。次に、それぞれの変更または変更のセットが起こる前に OnComponentChanging メソッドを呼び出し、それぞれの変更または変更のセットが起こった後に OnComponentChanged メソッドを呼び出す必要があります。最後に、Commit メソッドを呼び出して、トランザクションを完了し、閉じてください。
注意
プロパティの値を変更する場合は、PropertyDescriptor の SetValue メソッドを使用します。このメソッドは、IComponentChangeService のコンポーネント変更メソッドを呼び出し、変更を表す DesignerTransaction を自動的に作成します。
トランザクションを実行するには、次の手順を実行します。
CreateTransaction を呼び出し、トランザクションを制御するために使用できる DesignerTransaction を取得します。
try ブロック内で、DesignerTransaction で追跡するアクションごとに OnComponentChanging メソッドを呼び出し、変更を行った後で、OnComponentChanged メソッドを呼び出して変更が行われたことを通知します。
トランザクションを完了するには、finally ブロック内から Commit を呼び出します。
C# では、次の例で示すように、try/finally ブロックではなく using ステートメントを使用できます。
using (host.CreateTransaction() {
// Insert your code here.
}
トランザクションがコミットされる前に、キャンセルしてロールバックを試行するには、Cancel メソッドを呼び出します。Cancel メソッドが呼び出されると、DesignerTransaction で追跡していたアクションが元に戻され、変更内容をすべて元に戻す操作が試行されます。以前のトランザクションの一部として実行された操作を元に戻すには、開発環境で用意されている元に戻すコマンドを使用する必要があります。
注意
このクラスに適用される HostProtectionAttribute 属性の Resources プロパティの値は、SharedState です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的には、アイコンをダブルクリック、コマンドを入力、またはブラウザに URL を入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラスのトピックまたは「SQL Server プログラミングとホスト保護属性」を参照してください。
使用例
デザイナから DesignerTransaction を作成するコード例を次に示します。デザイナは、必要に応じて、デザイナのトランザクション イベントに関する通知を表示できます。このサンプルを実行するには、ソース コードをクラス ライブラリにコンパイルし、コンパイル済みの DLL への参照をプロジェクトに追加して、ライブラリ内のコンポーネントを [ツールボックス] に追加します。
Visual Studio には、この機能の拡張サポートがあります。
チュートリアル : ツールボックスへのカスタム コンポーネントの自動設定
チュートリアル : ツールボックスへのカスタム コンポーネントの自動設定
チュートリアル : ツールボックスへのカスタム コンポーネントの自動設定
チュートリアル : ツールボックスへのカスタム コンポーネントの自動設定
デザイン モードで DTComponent
のインスタンスをフォームに追加すると、デザイナのトランザクション イベント通知を受け受け取るかどうかを確認するメッセージ ボックスが表示されます。DTComponent
のインスタンスを右クリックすると表示されるショートカット メニューを使用して、これらの通知を切り替えることができます。[プロパティ] ウィンドウを使用して値を変更すると、トランザクションが作成されます。コンポーネントのショートカット メニューの [トランザクション例を実行する] をクリックすることで、デザイナでトランザクションを実行することもできます。
Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
' This sample demonstrates how to perform a series of actions in a designer
' transaction, how to change values of properties of a component from a
' designer, and how to complete transactions without being interrupted
' by other activities.
' To run this sample, add this code to a class library project and compile.
' Create a new Windows Forms project or load a form in the designer. Add a
' reference to the class library that was compiled in the first step.
' Right-click the Toolbox in design mode and click Customize Toolbox.
' Browse to the class library that was compiled in the first step and
' select OK until the DTComponent item appears in the Toolbox. Add an
' instance of this component to the form.
' When the component is created and added to the component tray for your
' design project, the Initialize method of the designer is called.
' This method displays a message box informing you that designer transaction
' event handlers are being registered unless you click Cancel. When you set
' properties in the properties window, each change will be encapsulated in
' a designer transaction, allowing the change to be undone later.
' When you right-click the component, the shortcut menu for the component
' is displayed. The designer constructs this menu according to whether
' designer transaction notifications are enabled, and offers the option
' of enabling or disabling the notifications, depending on the current
' mode. The shortcut menu also presents a Perform Example Transaction
' item which will set the values of the component's StringProperty and
' CountProperty properties. You can undo the last designer transaction using
' the Undo command provided by the Visual Studio development environment.
Namespace DesignerTransactionSample
' Associate the DTDesigner with this component
<DesignerAttribute(GetType(DTDesigner))> _
Public Class DTComponent
Inherits System.ComponentModel.Component
Private m_String As String
Private m_Count As Integer
Public Property StringProperty() As String
Get
Return m_String
End Get
Set(ByVal Value As String)
m_String = Value
End Set
End Property
Public Property CountProperty() As Integer
Get
Return m_Count
End Get
Set(ByVal Value As Integer)
m_Count = Value
End Set
End Property
Private Sub InitializeComponent()
m_String = "Initial Value"
m_Count = 0
End Sub 'InitializeComponent
End Class 'DTComponent
Friend Class DTDesigner
Inherits ComponentDesigner
Private notification_mode As Boolean = False
Private count As Integer = 10
' The Verbs property is overridden from ComponentDesigner
Public Overrides ReadOnly Property Verbs() As DesignerVerbCollection
Get
Dim dvc As New DesignerVerbCollection()
dvc.Add(New DesignerVerb("Perform Example Transaction", AddressOf Me.DoTransaction))
If notification_mode Then
dvc.Add(New DesignerVerb("End Designer Transaction Notifications", AddressOf Me.UnlinkDTNotifications))
Else
dvc.Add(New DesignerVerb("Show Designer Transaction Notifications", AddressOf Me.LinkDTNotifications))
End If
Return dvc
End Get
End Property
Public Overrides Sub Initialize(ByVal component As System.ComponentModel.IComponent)
MyBase.Initialize(component)
Dim host As IDesignerHost = CType(GetService(GetType(IDesignerHost)), IDesignerHost)
If host Is Nothing Then
MessageBox.Show("The IDesignerHost service interface could not be obtained.")
Return
End If
If MessageBox.Show("Press the Yes button to display notification message boxes for the designer transaction opened and closed notifications.", "Link DesignerTransaction Notifications?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign) = DialogResult.Yes Then
AddHandler host.TransactionOpened, AddressOf OnDesignerTransactionOpened
AddHandler host.TransactionClosed, AddressOf OnDesignerTransactionClosed
notification_mode = True
End If
End Sub 'Initialize
Private Sub LinkDTNotifications(ByVal sender As Object, ByVal e As EventArgs)
If notification_mode = False Then
Dim host As IDesignerHost = CType(GetService(GetType(IDesignerHost)), IDesignerHost)
If Not (host Is Nothing) Then
notification_mode = True
AddHandler host.TransactionOpened, AddressOf OnDesignerTransactionOpened
AddHandler host.TransactionClosed, AddressOf OnDesignerTransactionClosed
End If
End If
End Sub 'LinkDTNotifications
Private Sub UnlinkDTNotifications(ByVal sender As Object, ByVal e As EventArgs)
If notification_mode Then
Dim host As IDesignerHost = CType(GetService(GetType(IDesignerHost)), IDesignerHost)
If Not (host Is Nothing) Then
notification_mode = False
RemoveHandler host.TransactionOpened, AddressOf Me.OnDesignerTransactionOpened
RemoveHandler host.TransactionClosed, AddressOf Me.OnDesignerTransactionClosed
End If
End If
End Sub 'UnlinkDTNotifications
Private Sub OnDesignerTransactionOpened(ByVal sender As Object, ByVal e As EventArgs)
System.Windows.Forms.MessageBox.Show("A Designer Transaction was started. (TransactionOpened)")
End Sub 'OnDesignerTransactionOpened
Private Sub OnDesignerTransactionClosed(ByVal sender As Object, ByVal e As DesignerTransactionCloseEventArgs)
System.Windows.Forms.MessageBox.Show("A Designer Transaction was completed. (TransactionClosed)")
End Sub 'OnDesignerTransactionClosed
Private Sub DoTransaction(ByVal sender As Object, ByVal e As EventArgs)
Dim host As IDesignerHost = CType(GetService(GetType(IDesignerHost)), IDesignerHost)
Dim t As DesignerTransaction = host.CreateTransaction("Change Text and Size")
' The code within the using statement is considered to be a single transaction.
' When the user selects Undo, the system will undo everything executed in this code block.
Try
If (notification_mode) Then
System.Windows.Forms.MessageBox.Show("Entering a Designer-Initiated Designer Transaction")
End If
Dim someText As PropertyDescriptor = TypeDescriptor.GetProperties(Component)("StringProperty")
someText.SetValue(Component, "This text was set by the designer for this component.")
Dim anInteger As PropertyDescriptor = TypeDescriptor.GetProperties(Component)("CountProperty")
anInteger.SetValue(Component, count)
count = count + 1
Exit Try
Finally
t.Commit()
End Try
If (notification_mode) Then
System.Windows.Forms.MessageBox.Show("Designer-Initiated Designer Transaction Completed")
End If
End Sub 'DoTransaction
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
UnlinkDTNotifications(Me, New EventArgs())
MyBase.Dispose(disposing)
End Sub 'Dispose
End Class 'DTDesigner
End Namespace 'DesignerTransactionSample
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
/*
This sample demonstrates how to perform a series of actions in a designer
transaction, how to change values of properties of a component from a
designer, and how to complete transactions without being interrupted
by other activities.
To run this sample, add this code to a class library project and compile.
Create a new Windows Forms project or load a form in the designer. Add a
reference to the class library that was compiled in the first step.
Right-click the Toolbox in design mode and click Customize Toolbox.
Browse to the class library that was compiled in the first step and
select OK until the DTComponent item appears in the Toolbox. Add an
instance of this component to the form.
When the component is created and added to the component tray for your
design project, the Initialize method of the designer is called.
This method displays a message box informing you that designer transaction
event handlers will be registered unless you click Cancel. When you set
properties in the properties window, each change will be encapsulated in
a designer transaction, allowing the change to be undone later.
When you right-click the component, the shortcut menu for the component
is displayed. The designer constructs this menu according to whether
designer transaction notifications are enabled, and offers the option
of enabling or disabling the notifications, depending on the current
mode. The shortcut menu also presents a Perform Example Transaction
item, which will set the values of the component's StringProperty and
CountProperty properties. You can undo the last designer transaction using
the Undo command provided by the Visual Studio development environment.
*/
namespace DesignerTransactionSample
{
// Associate the DTDesigner with this component
[DesignerAttribute(typeof(DTDesigner))]
public class DTComponent : System.ComponentModel.Component
{
private string m_String;
private int m_Count;
public string StringProperty
{
get
{ return m_String; }
set
{ m_String = value; }
}
public int CountProperty
{
get
{ return m_Count; }
set
{ m_Count = value; }
}
private void InitializeComponent()
{
m_String = "Initial Value";
m_Count = 0;
}
}
internal class DTDesigner : ComponentDesigner
{
private bool notification_mode = false;
private int count = 10;
// The Verbs property is overridden from ComponentDesigner
public override DesignerVerbCollection Verbs
{
get
{
DesignerVerbCollection dvc = new DesignerVerbCollection();
dvc.Add( new DesignerVerb("Perform Example Transaction", new EventHandler(this.DoTransaction)) );
if(notification_mode)
dvc.Add(new DesignerVerb("End Designer Transaction Notifications", new EventHandler(this.UnlinkDTNotifications)));
else
dvc.Add(new DesignerVerb("Show Designer Transaction Notifications", new EventHandler(this.LinkDTNotifications))); return dvc;
}
}
public override void Initialize(System.ComponentModel.IComponent component)
{
base.Initialize(component);
IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
if(host == null)
{
MessageBox.Show("The IDesignerHost service interface could not be obtained.");
return;
}
if( MessageBox.Show("Press the Yes button to display notification message boxes for the designer transaction opened and closed notifications.","Link DesignerTransaction Notifications?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign) == DialogResult.Yes )
{
host.TransactionOpened += new EventHandler(OnDesignerTransactionOpened);
host.TransactionClosed += new DesignerTransactionCloseEventHandler(OnDesignerTransactionClosed);
notification_mode = true;
}
}
private void LinkDTNotifications(object sender, EventArgs e)
{
if(notification_mode == false)
{
IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
if(host != null)
{
notification_mode = true;
host.TransactionOpened += new EventHandler(OnDesignerTransactionOpened);
host.TransactionClosed += new DesignerTransactionCloseEventHandler(OnDesignerTransactionClosed);
}
}
}
private void UnlinkDTNotifications(object sender, EventArgs e)
{
if(notification_mode)
{
IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
if(host != null)
{
notification_mode = false;
host.TransactionOpened -= new EventHandler(OnDesignerTransactionOpened);
host.TransactionClosed -= new DesignerTransactionCloseEventHandler(OnDesignerTransactionClosed);
}
}
}
private void OnDesignerTransactionOpened(object sender, EventArgs e)
{
System.Windows.Forms.MessageBox.Show("A Designer Transaction was started. (TransactionOpened)");
}
private void OnDesignerTransactionClosed(object sender, DesignerTransactionCloseEventArgs e)
{
System.Windows.Forms.MessageBox.Show("A Designer Transaction was completed. (TransactionClosed)");
}
private void DoTransaction(object sender, EventArgs e)
{
IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
DesignerTransaction t = host.CreateTransaction("Change Text and Size");
/* The code within the using statement is considered to be a single transaction.
When the user selects Undo, the system will undo everything executed in this code block. */
using (t)
{
if(notification_mode)
System.Windows.Forms.MessageBox.Show("Entering a Designer-Initiated Designer Transaction");
// The .NET Framework automatically associates the TypeDescriptor with the correct component
PropertyDescriptor someText = TypeDescriptor.GetProperties(Component)["StringProperty"];
someText.SetValue(Component, "This text was set by the designer for this component.");
PropertyDescriptor anInteger = TypeDescriptor.GetProperties(Component)["CountProperty"];
anInteger.SetValue(Component, count);
count++;
// Complete the designer transaction.
t.Commit();
if(notification_mode)
System.Windows.Forms.MessageBox.Show("Designer-Initiated Designer Transaction Completed");
}
}
protected override void Dispose(bool disposing)
{
UnlinkDTNotifications(this, new EventArgs());
base.Dispose(disposing);
}
}
}
#using <system.dll>
#using <system.design.dll>
#using <system.windows.forms.dll>
using namespace System;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::Windows::Forms;
using namespace System::Windows::Forms::Design;
/*
This sample demonstrates how to perform a series of actions in a designer
transaction, how to change values of properties of a component from a
designer, and how to complete transactions without being interrupted
by other activities.
To run this sample, add this code to a class library project and compile.
Create a new Windows Forms project or load a form in the designer. Add a
reference to the class library that was compiled in the first step.
Right-click the Toolbox in design mode and click Customize Toolbox.
Browse to the class library that was compiled in the first step and
select OK until the DTComponent item appears in the Toolbox. Add an
instance of this component to the form.
When the component is created and added to the component tray for your
design project, the Initialize method of the designer is called.
This method displays a message box informing you that designer transaction
event handlers will be registered unless you click Cancel. When you set
properties in the properties window, each change will be encapsulated in
a designer transaction, allowing the change to be undone later.
When you right-click the component, the shortcut menu for the component
is displayed. The designer constructs this menu according to whether
designer transaction notifications are enabled, and offers the option
of enabling or disabling the notifications, depending on the current
mode. The shortcut menu also presents a Perform Example Transaction
item, which will set the values of the component's StringProperty and
CountProperty properties. You can undo the last designer transaction using
the Undo command provided by the Visual Studio development environment.
*/
private ref class DTDesigner: public ComponentDesigner
{
private:
bool notification_mode;
int count;
void LinkDTNotifications( Object^ /*sender*/, EventArgs^ /*e*/ )
{
if ( !notification_mode )
{
IDesignerHost^ host = dynamic_cast<IDesignerHost^>(GetService( IDesignerHost::typeid ));
if ( host != nullptr )
{
notification_mode = true;
host->TransactionOpened += gcnew EventHandler( this, &DTDesigner::OnDesignerTransactionOpened );
host->TransactionClosed += gcnew DesignerTransactionCloseEventHandler( this, &DTDesigner::OnDesignerTransactionClosed );
}
}
}
void UnlinkDTNotifications( Object^ /*sender*/, EventArgs^ /*e*/ )
{
if ( notification_mode )
{
IDesignerHost^ host = dynamic_cast<IDesignerHost^>(GetService( IDesignerHost::typeid ));
if ( host != nullptr )
{
notification_mode = false;
host->TransactionOpened -= gcnew EventHandler( this, &DTDesigner::OnDesignerTransactionOpened );
host->TransactionClosed -= gcnew DesignerTransactionCloseEventHandler( this, &DTDesigner::OnDesignerTransactionClosed );
}
}
}
void OnDesignerTransactionOpened( Object^ /*sender*/, EventArgs^ /*e*/ )
{
MessageBox::Show( "A Designer Transaction was started. (TransactionOpened)" );
}
void OnDesignerTransactionClosed( Object^ /*sender*/, DesignerTransactionCloseEventArgs^ /*e*/ )
{
MessageBox::Show( "A Designer Transaction was completed. (TransactionClosed)" );
}
void DoTransaction( Object^ /*sender*/, EventArgs^ /*e*/ )
{
IDesignerHost^ host = static_cast<IDesignerHost^>(GetService( IDesignerHost::typeid ));
DesignerTransaction^ t = host->CreateTransaction( "Change Text and Size" );
/* The code within the using statement is considered to be a single transaction.
When the user selects Undo, the system will undo everything executed in this code block.
*/
if ( notification_mode )
MessageBox::Show( "Entering a Designer-Initiated Designer Transaction" );
// The .NET Framework automatically associates the TypeDescriptor with the correct component
PropertyDescriptor^ someText = TypeDescriptor::GetProperties( Component )[ "StringProperty" ];
someText->SetValue( Component, "This text was set by the designer for this component." );
PropertyDescriptor^ anInteger = TypeDescriptor::GetProperties( Component )[ "CountProperty" ];
anInteger->SetValue( Component, count );
count++;
// Complete the designer transaction.
t->Commit();
if ( notification_mode )
MessageBox::Show( "Designer-Initiated Designer Transaction Completed" );
}
public:
property DesignerVerbCollection^ Verbs
{
// The Verbs property is overridden from ComponentDesigner
virtual DesignerVerbCollection^ get() override
{
DesignerVerbCollection^ dvc = gcnew DesignerVerbCollection;
dvc->Add( gcnew DesignerVerb( "Perform Example Transaction",gcnew EventHandler( this, &DTDesigner::DoTransaction ) ) );
if ( notification_mode )
dvc->Add( gcnew DesignerVerb( "End Designer Transaction Notifications",
gcnew EventHandler( this, &DTDesigner::UnlinkDTNotifications ) ) );
else
dvc->Add( gcnew DesignerVerb( "Show Designer Transaction Notifications",
gcnew EventHandler( this, &DTDesigner::LinkDTNotifications ) ) );
return dvc;
}
}
virtual void Initialize( IComponent^ component ) override
{
ComponentDesigner::Initialize( component );
notification_mode = false;
count = 10;
IDesignerHost^ host = dynamic_cast<IDesignerHost^>(GetService( IDesignerHost::typeid ));
if ( host == nullptr )
{
MessageBox::Show( "The IDesignerHost service interface could not be obtained." );
return;
}
if ( MessageBox::Show( "Press the Yes button to display notification message boxes for the designer transaction opened and closed notifications.", "Link DesignerTransaction Notifications?", MessageBoxButtons::YesNo, MessageBoxIcon::Question, MessageBoxDefaultButton::Button1, MessageBoxOptions::RightAlign ) == DialogResult::Yes )
{
host->TransactionOpened += gcnew EventHandler( this, &DTDesigner::OnDesignerTransactionOpened );
host->TransactionClosed += gcnew DesignerTransactionCloseEventHandler( this, &DTDesigner::OnDesignerTransactionClosed );
notification_mode = true;
}
}
public:
~DTDesigner()
{
UnlinkDTNotifications( this, gcnew EventArgs );
}
};
// Associate the DTDesigner with this component
[DesignerAttribute(DTDesigner::typeid)]
public ref class DTComponent: public System::ComponentModel::Component
{
private:
String^ m_String;
int m_Count;
void InitializeComponent()
{
m_String = "Initial Value";
m_Count = 0;
}
public:
property String^ StringProperty
{
String^ get()
{
return m_String;
}
void set( String^ value )
{
m_String = value;
}
}
property int CountProperty
{
int get()
{
return m_Count;
}
void set( int value )
{
m_Count = value;
}
}
};
.NET Framework のセキュリティ
- NamedPermissionSet (システム リソースへのフル アクセス許可)。要求値 : InheritanceDemand。FullTrust (関連する状態)。
継承階層
System.Object
System.ComponentModel.Design.DesignerTransaction
スレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォーム
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
.NET Framework
サポート対象 : 2.0、1.1、1.0
参照
関連項目
DesignerTransaction メンバ
System.ComponentModel.Design 名前空間
IDesignerHost
IComponentChangeService
PropertyDescriptor クラス