【MMC】Microsoft 管理コンソール ~プログラム作成 / シンプル画面[2] ~


サンプル

SampleControl.cs

using System;
using System.Text;
using System.Windows.Forms;
using Microsoft.ManagementConsole;

public partial class SampleControl : UserControl, IFormViewControl
{
 SampleFormView selectionFormView = null;

 public SampleControl()
 {
   InitializeComponent();
   
   this.Dock = DockStyle.Fill;
   this.listView1.View = System.Windows.Forms.View.Details;
   ColumnHeader userColumnHeader = new ColumnHeader();
   userColumnHeader.Text = "User";
   userColumnHeader.Width = 200;
   this.listView1.Columns.Add(userColumnHeader);

   ColumnHeader birthdayColumnHeader = new ColumnHeader();
   birthdayColumnHeader.Text = "BirthDay";
   birthdayColumnHeader.Width = 200;
   this.listView1.Columns.Add(birthdayColumnHeader);
 }
 void IFormViewControl.Initialize(FormView parentSelectionFormView)
 {
   selectionFormView = (SampleFormView)parentSelectionFormView;

   selectionFormView.SelectionData.ActionsPaneItems.Clear();
   selectionFormView.SelectionData.ActionsPaneItems.Add(
     new Microsoft.ManagementConsole.Action(
         "Show Selection",
         "Shows the Names of the selected Items in the ListView.",
         -1,
         "ShowSelection"));
 }
 public void RefreshData(string[][] users)
 {
   this.listView1.Items.Clear();

   foreach (string[] user in users)
   {
     ListViewItem listViewItem = new ListViewItem();
     listViewItem.Text = user[0];
     listViewItem.SubItems.Add(user[1]);
     this.listView1.Items.Add(listViewItem);
   }
 }
 public void ShowSelection()
 {
   if (this.listView1.SelectedItems == null)
   {
     MessageBox.Show("There are no items selected");
   }
   else
   {
     MessageBox.Show(
      "Selected Users: \n" + this.GetSelectedUser());
   }
 }
 /// <summary>
 /// Update the context.
 /// </summary>
 private void listView1_SelectedIndexChanged(object sender, EventArgs e)
 {
   if (this.listView1.SelectedItems.Count == 0)
   {
     this.selectionFormView.SelectionData.Clear();
   }
   else
   {
     this.selectionFormView.SelectionData.Update(
       this.GetSelectedUser(),
       this.listView1.SelectedItems.Count > 1, null, null);

     this.selectionFormView.SelectionData.DisplayName =
       ((this.listView1.SelectedItems.Count == 1) ? 
       this.listView1.SelectedItems[0].Text : "Selected Objects");
   }
 }
 private string GetSelectedUser()
 {
   StringBuilder selectedUsers = new StringBuilder();

   foreach (ListViewItem listViewItem in this.listView1.SelectedItems)
   {
     selectedUsers.Append(listViewItem.Text + "\n");
   }

   return selectedUsers.ToString();
 }
 /// <summary>
 /// Handle mouse clicks.
 /// </summary>
 private void listView1_MouseClick(object sender, MouseEventArgs e)
 {
   if (e.Button == MouseButtons.Right)
   {
     bool rightClickedOnSelection = false;

     ListViewItem rightClickedItem =
         this.listView1.GetItemAt(e.X, e.Y);
     if (rightClickedItem == null || rightClickedItem.Selected == false)
     {
       rightClickedOnSelection = false;
     }
     else
     {
       rightClickedOnSelection = true;
     }
     this.selectionFormView.ShowContextMenu(
       base.PointToScreen(e.Location), rightClickedOnSelection);
   }
 }
}