英語で読む

次の方法で共有


DesignerTransaction クラス

定義

パフォーマンスを向上し、ほとんどの変更内容を元に戻せるようにするために、デザイン時の一連の操作をグループ化する手段を提供します。

C#
public abstract class DesignerTransaction : IDisposable
継承
DesignerTransaction
実装

次のコード例プログラムは、デザイナーから を DesignerTransaction 作成する方法を示しています。 このサンプルを実行するには、ソース コードをクラス ライブラリにコンパイルします。 System.Design アセンブリへの参照を追加する必要があります。 新しいプロジェクトで、コンパイル済み DLL への参照を追加し、ライブラリ内のコンポーネントをツールボックスに追加 します

Visual Studio では、この機能に対する広範なサポートが用意されています。

チュートリアル: ツールボックスにカスタム コンポーネントを自動的に設定する」も参照してください。

デザイナーは必要に応じて、デザイナー トランザクション イベントに関する通知を表示できます。 デザイン モードで フォームに の DTComponent インスタンスを追加すると、デザイナー トランザクション イベント通知を受信するかどうかを確認するメッセージ ボックスが表示されます。 のインスタンスを右クリックすると表示されるショートカット メニューを使用して、これらの通知を DTComponent切り替えることができます。 トランザクションは、プロパティ ウィンドウを使用して値を変更すると作成されます。 コンポーネントのショートカット メニューで [サンプル トランザクションの実行] をクリックして、デザイナーに トランザクションを実行 してもらうこともできます。

C#
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);
    }
    }
}

注釈

トランザクションは、後で元に戻すことができるアクションを追跡できます。 トランザクション中に行われた変更は、トランザクションを取り消すことによって取り消すことができます。これにより、変更された各プロパティを変更前の値に設定することで、各変更の取り消しが自動的に試行されます。 トランザクションは、トランザクションが完了するまで表示の更新を延期することで、一連の操作中のパフォーマンスを向上させることもできます。

トランザクションが進行中の場合、一部のコンポーネントは、 イベントと TransactionClosed イベントをリッスンしてトランザクションが完了するまで処理をTransactionOpening延期します。 たとえば、プロパティ ウィンドウは、トランザクションが閉じられるまでトランザクションを開いた後に表示を更新しません。

元に戻す操作または複数の操作にトランザクションを使用するには、デザイナーに各操作または一連の操作に対して を作成 DesignerTransaction し、元に戻す必要があります。 一連の元に戻すイベントが正常に完了しない可能性があるトランザクションの外部でアクションを実行しないように注意してください。

の メソッドを呼び出すことで、CreateTransaction新しい DesignerTransactionIDesignerHost取得できます。 新しいDesignerTransactionトランザクション処理メカニズムを直接作成するのではなく、デザイナートランザクション処理メカニズムと正しく統合するために、アクティブIDesignerHostな からそれぞれDesignerTransactionを取得してください。

トランザクション内でアクションを実行するには、まずトランザクションを作成する必要があります。 次に、各変更または変更セットが OnComponentChanging 発生する前に メソッドを呼び出し OnComponentChanged 、各変更または一連の変更が発生した後に メソッドを呼び出す必要があります。 最後に、 メソッドを呼び出してトランザクションを Commit 完了して閉じます。

注意

プロパティ値を変更する場合は、 の メソッドをSetValue使用します。このメソッドは のコンポーネント変更メソッドIComponentChangeServiceを呼び出し、変更を表す を自動的に作成DesignerTransactionPropertyDescriptorします。

トランザクションを実行するには、次の手順を実行します。

  1. を呼び出 CreateTransaction して、トランザクションの DesignerTransaction 制御に使用できる を取得します。

  2. ブロック内で try 、 で DesignerTransaction追跡するアクションごとに、 メソッドを呼び出し OnComponentChanging 、変更または変更を行い、 メソッドを OnComponentChanged 呼び出して、変更または変更が行われたことを通知します。

  3. トランザクションを完了するには、 ブロック内から をfinally呼び出しますCommit

C# では、次の using 例のように、 ブロックではなく try/finally ステートメントを使用できます。

using (host.CreateTransaction() {  
// Insert your code here.  
}  

コミットされる前にトランザクションを取り消してロールバックするには、 メソッドを Cancel 呼び出します。 メソッドが Cancel 呼び出されると、 によって DesignerTransaction 追跡されるアクションは元に戻され、変更のロールバックが試行されます。 以前のトランザクションの一部として発生した操作を元に戻すには、開発環境で提供される元に戻すコマンドを使用する必要があります。

コンストラクター

DesignerTransaction()

説明を指定せずに DesignerTransaction クラスの新しいインスタンスを初期化します。

DesignerTransaction(String)

トランザクションの説明を指定して、DesignerTransaction クラスの新しいインスタンスを初期化します。

プロパティ

Canceled

トランザクションがキャンセルされたかどうかを示す値を取得します。

Committed

トランザクションがコミットされたかどうかを示す値を取得します。

Description

トランザクションの説明を取得します。

メソッド

Cancel()

トランザクションをキャンセルし、そのトランザクションのイベントによって行われた変更をすべて元に戻そうとします。

Commit()

トランザクションをコミットします。

Dispose(Boolean)

DesignerTransaction によって使用されているアンマネージド リソースを解放し、オプションでマネージド リソースも解放します。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
Finalize()

オブジェクトと関連付けられたリソースを解放します。 このトランザクションがまだコミットされていなかった場合、このオーバーライドがこのトランザクションをコミットします。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
OnCancel()

Cancel イベントを発生させます。

OnCommit()

トランザクションの実際のコミット処理を実行します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

IDisposable.Dispose()

DesignerTransaction によって使用されているすべてのリソースを解放します。

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

こちらもご覧ください