⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commandsdesigneractionlist.cs

📁 中文名:Windows Forms 程序设计 英文名:Windows Forms Programming in c# 作者: Chris Sells 翻译: 荣耀 蒋贤哲 出版社:人民
💻 CS
字号:
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 CommandsDesignerActionList : DesignerActionList {

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

    public override DesignerActionItemCollection GetSortedActionItems() {

      if( this.Component == null ) return null;
      
      // 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."));

      // EditClockHands designer action method item
      actionItems.Add(
        new DesignerActionMethodItem(
          this,
          "EditClockHands",
          "Edit Clock Hands...",
          "Commands",
          "Configure the AlarmClockControl's hour, minute and second hands.",
          true));

      // Return list of designer action items
      return actionItems;
    }
    // EditClockHands method
    public void EditClockHands() {
      // Create form
      HandsEditorForm form = new HandsEditorForm();
      // Set current hand values
      form.HourHand = this.AlarmClockControl.HourHand;
      form.MinuteHand = this.AlarmClockControl.MinuteHand;
      form.SecondHand = this.AlarmClockControl.SecondHand;
      // Update new hand values of OK button was pressed
      if( form.ShowDialog() == DialogResult.OK ) {
        IDesignerHost designerHost = this.GetService(typeof(IDesignerHost)) as IDesignerHost;
        if( designerHost != null ) {
          DesignerTransaction t = designerHost.CreateTransaction();
          try {
            this.SetProperty("HourHand", form.HourHand);
            this.SetProperty("MinuteHand", form.MinuteHand);
            this.SetProperty("SecondHand", form.SecondHand);
            t.Commit();
          }
          catch { t.Cancel(); }
        }
      }
    }

    // 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -