次の方法で共有


PropertyGrid.SelectedObjects プロパティ

現在選択されているオブジェクトを取得または設定します。

Public Property SelectedObjects As Object ()
[C#]
public object[] SelectedObjects {get; set;}
[C++]
public: __property Object* get_SelectedObjects();public: __property void set_SelectedObjects(Object* __gc[]);
[JScript]
public function get SelectedObjects() : Object[];
public function set SelectedObjects(Object[]);

プロパティ値

Object 型の配列。既定値は空の配列です。

例外

例外の種類 条件
ArgumentException オブジェクトの配列内の項目のいずれかの値が null 参照 (Visual Basic では Nothing) です。

解説

PropertyGrid には、配列内のすべてのオブジェクトに共通するプロパティだけが表示されます。配列を SelectedObjects に割り当てると、以降の SelectedObject への参照がすべて置き換えられます。

使用例

[Visual Basic, C#] SelectedObjects プロパティの設定方法については、次のコード例を参照してください。この例を実行するには、次のコードを空のフォームに貼り付けます。必ずすべてのイベントをイベント処理メソッドに関連付けるようにしてください。

 
' This form combines uses a CheckedListBox and PropertyGrid to 
' combine some controls.  CheckedListBox1 is populated with itself
' and the other controls on the form. The user can then click Button1
' to see the PropertyGrid for the selected controls.

Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Button1 As System.Windows.Forms.Button

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.Button1 = New System.Windows.Forms.Button
        Me.TextBox1.Location = New System.Drawing.Point(40, 20)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = "TextBox1"
        Me.Label1.Location = New System.Drawing.Point(40, 60)
        Me.Label1.Name = "Label1"
        Me.Label1.TabIndex = 2
        Me.Label1.Text = "Label1"
        Me.Button1.Location = New System.Drawing.Point(40, 200)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 4
        Me.Button1.Size = New Size(100, 50)
        Me.Button1.Text = "Show properties for selected control(s)"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(350, 350)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        InitializeCheckedListBox()
        InitializePropertyGrid()


    End Sub

    Public Shared Sub Main()
        Application.Run(New Form1)
    End Sub

    ' This method initializes CheckedListBox1 with a list of all the controls
    ' on the form. It sets the selection mode to single selection and
    ' allows selection with a single click. It adds itself to the list before 
    ' adding itself to the form.
    Friend WithEvents CheckedListBox1 As System.Windows.Forms.CheckedListBox

    Private Sub InitializeCheckedListBox()
        Me.CheckedListBox1 = New CheckedListBox
        Me.CheckedListBox1.Location = New System.Drawing.Point(40, 90)
        Me.CheckedListBox1.CheckOnClick = True
        Me.CheckedListBox1.Name = "CheckedListBox1"
        Me.CheckedListBox1.Size = New System.Drawing.Size(120, 94)
        Me.CheckedListBox1.TabIndex = 1
        Me.CheckedListBox1.SelectionMode = SelectionMode.One
        Me.CheckedListBox1.ThreeDCheckBoxes = True

        Dim aControl As Control
        For Each aControl In Me.Controls
            Me.CheckedListBox1.Items.Add(aControl, False)
        Next

        Me.CheckedListBox1.DisplayMember = "Name"
        Me.CheckedListBox1.Items.Add(CheckedListBox1)
        Me.Controls.Add(Me.CheckedListBox1)
    End Sub


    'Declare a propertyGrid.
    Friend WithEvents propertyGrid1 As PropertyGrid

    'Initialize propertyGrid1.
    Private Sub InitializePropertyGrid()
        propertyGrid1 = New PropertyGrid
        propertyGrid1.Name = "PropertyGrid1"
        propertyGrid1.Location = New Point(185, 20)
        propertyGrid1.Size = New System.Drawing.Size(150, 300)
        propertyGrid1.TabIndex = 5

        'Set the sort to alphabetical and set Toolbar visible
        'to false, so the user cannot change the sort.
        propertyGrid1.PropertySort = PropertySort.Alphabetical
        propertyGrid1.ToolbarVisible = False
        propertyGrid1.Text = "Property Grid"

        ' Add the PropertyGrid to the form, but set its
        ' visibility to False so it will not appear when the form loads.
        propertyGrid1.Visible = False
        Me.Controls.Add(propertyGrid1)

    End Sub

    ' Sets the SelectedObjects property of PropertyGrid1's 
    ' SelectedObjects to the controls the user has selected in CheckedListBox1.
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles Button1.Click

        propertyGrid1.Visible = True
        Dim selectedControls(CheckedListBox1.CheckedItems.Count - 1) As Control
        Dim counter As Integer
        For counter = 0 To CheckedListBox1.CheckedItems.Count - 1
            selectedControls(counter) = CheckedListBox1.CheckedItems(counter)
        Next
        propertyGrid1.SelectedObjects = selectedControls
    End Sub

[C#] 
// This form combines uses a CheckedListBox and PropertyGrid to 
// combine some controls.  CheckedListBox1 is populated with itself
// and the other controls on the form. The user can then click Button1
// to see the PropertyGrid for the selected controls.

using System.Windows.Forms;

public class Form1:
    System.Windows.Forms.Form
{
    internal System.Windows.Forms.TextBox TextBox1;
    internal System.Windows.Forms.Label Label1;
    internal System.Windows.Forms.Button Button1;

    public Form1() : base()
    {        

        //This call is required by the Windows Form Designer.
        this.TextBox1 = new System.Windows.Forms.TextBox();
        this.Label1 = new System.Windows.Forms.Label();
        this.Button1 = new System.Windows.Forms.Button();
        this.TextBox1.Location = new System.Drawing.Point(40, 20);
        this.TextBox1.Name = "TextBox1";
        this.TextBox1.TabIndex = 0;
        this.TextBox1.Text = "TextBox1";
        this.Label1.Location = new System.Drawing.Point(40, 60);
        this.Label1.Name = "Label1";
        this.Label1.TabIndex = 2;
        this.Label1.Text = "Label1";
        this.Button1.Location = new System.Drawing.Point(40, 200);
        this.Button1.Name = "Button1";
        this.Button1.TabIndex = 4;
        this.Button1.Size = new System.Drawing.Size(100, 50);
        this.Button1.Text = "Show properties for selected control(s)";
        this.Button1.Click += new System.EventHandler(Button1_Click);
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(350, 350);
        this.Controls.Add(this.Label1);
        this.Controls.Add(this.TextBox1);
        this.Controls.Add(this.Button1);
        this.Name = "Form1";
        this.Text = "Form1";
        InitializeCheckedListBox();
        InitializePropertyGrid();
    }

    public static void Main()
    {
        Application.Run(new Form1());
    }

    // This method initializes CheckedListBox1 with a list of all 
    // the controls on the form. It sets the selection mode
    // to single selection and allows selection with a single click.
    // It adds itself to the list before adding itself to the form.

    internal System.Windows.Forms.CheckedListBox CheckedListBox1;

    private void InitializeCheckedListBox()
    {
        this.CheckedListBox1 = new CheckedListBox();
        this.CheckedListBox1.Location = new System.Drawing.Point(40, 90);
        this.CheckedListBox1.CheckOnClick = true;
        this.CheckedListBox1.Name = "CheckedListBox1";
        this.CheckedListBox1.Size = new System.Drawing.Size(120, 94);
        this.CheckedListBox1.TabIndex = 1;
        this.CheckedListBox1.SelectionMode = SelectionMode.One;
        this.CheckedListBox1.ThreeDCheckBoxes = true;

        foreach ( Control aControl in this.Controls )
        {
            this.CheckedListBox1.Items.Add(aControl, false);
        }

        this.CheckedListBox1.DisplayMember = "Name";
        this.CheckedListBox1.Items.Add(CheckedListBox1);
        this.Controls.Add(this.CheckedListBox1);
    }


    // Declare a propertyGrid.
    internal PropertyGrid propertyGrid1;

    // Initialize propertyGrid1.
    private void InitializePropertyGrid()
    {
        propertyGrid1 = new PropertyGrid();
        propertyGrid1.Name = "PropertyGrid1";
        propertyGrid1.Location = new System.Drawing.Point(185, 20);
        propertyGrid1.Size = new System.Drawing.Size(150, 300);
        propertyGrid1.TabIndex = 5;

        // Set the sort to alphabetical and set Toolbar visible
        // to false, so the user cannot change the sort.
        propertyGrid1.PropertySort = PropertySort.Alphabetical;
        propertyGrid1.ToolbarVisible = false;
        propertyGrid1.Text = "Property Grid";

        // Add the PropertyGrid to the form, but set its
        // visibility to False so it will not appear when the form loads.
        propertyGrid1.Visible = false;
        this.Controls.Add(propertyGrid1);

    }

    // Sets the SelectedObjects property of PropertyGrid1's 
    // SelectedObjects to the controls the user has selected in 
    // CheckedListBox1.
    private void Button1_Click(object sender, System.EventArgs e)
    {
        propertyGrid1.Visible = true;
        Control[] selectedControls = 
            new Control[CheckedListBox1.CheckedItems.Count];
        
        for(int counter = 0; 
            counter < CheckedListBox1.CheckedItems.Count; counter++)
        {
            selectedControls[counter] = 
                (Control) CheckedListBox1.CheckedItems[counter];
        }
        propertyGrid1.SelectedObjects = selectedControls;
    }

[C++, JScript] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

PropertyGrid クラス | PropertyGrid メンバ | System.Windows.Forms 名前空間