digitalcommandsdesigneractionlist.cs

来自「中文名:Windows Forms 程序设计 英文名:Windows Form」· CS 代码 · 共 79 行

CS
79
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace AlarmClockControlLibrary {
  public class DigitalCommandsDesignerActionList : DesignerActionList {

    public DigitalCommandsDesignerActionList(IComponent component) : base(component) { }

    public override DesignerActionItemCollection GetSortedActionItems() {

      // Create list to store designer action items
      DesignerActionItemCollection actionItems = new DesignerActionItemCollection();

      // Fill list of designer action items

      actionItems.Add(new DesignerActionHeaderItem("Commands"));

      actionItems.Add(
        new DesignerActionTextItem(
        "Design-Time AlarmClockControl commands.",
        "Commands"));

      // Dock/Undock designer action method item
      actionItems.Add(
        new DesignerActionMethodItem(
          this,
          "ToggleDockStyle",
          this.GetDockStyleText(),
          "Commands",
          "Dock or undock this control in it's parent container."));

      // Return list of designer action items
      return actionItems;
    }

    // Toggle AlarmClockControl's Dock property
    public void ToggleDockStyle() {
      if( this.AlarmClockControl.Dock != DockStyle.Fill ) {
        this.SetProperty("Dock", DockStyle.Fill);
      }
      else {
        this.SetProperty("Dock", DockStyle.None);
      }
    }

    // Helper method that returns an appropriate
    // display name for the Dock/Undock property,
    // based on the AlarmClockControl's current Dock 
    // property value
    private string GetDockStyleText() {
      if( this.AlarmClockControl.Dock == DockStyle.Fill ) {
        return "Undock in parent container";
      }
      else {
        return "Dock in parent container";
      }
    }

    // Helper method to safely set a component抯 property
    private void SetProperty(string propertyName, object value) {
      // Get property
      PropertyDescriptor property = TypeDescriptor.GetProperties(this.AlarmClockControl)[propertyName];
      // Set property value
      property.SetValue(this.AlarmClockControl, value);
    }

    // Helper property to acquire a AlarmClockControl reference
    private AlarmClockControl AlarmClockControl {
      get { return (AlarmClockControl)this.Component; }
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?