VSXtra-like implementation of the VS SDK's Combobox Reference Sample

Package source code:

using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell;
using VSXtra;

namespace DeepDiver.ComboboxCommands
{
  [PackageRegistration(UseManagedResourcesOnly = true)]
  [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0")]
  [InstalledProductRegistration(false, "#110", "#112", "1.0", IconResourceID = 400)]
  [ProvideLoadKey("Standard", "1.0", "ComboboxCommands", "DeepDiver", 1)]
  [ProvideMenuResource(1000, 1)]
  [Guid(GuidList.guidComboboxCommandsPkgString)]
  public sealed class ComboboxCommandsPackage : PackageBase
  {
  }
}
Command handlers for DropDownCombo, IndexedCombo and MRUCombo:

using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell;
using VSXtra;

namespace DeepDiver.ComboboxCommands
{
  [Guid(GuidList.guidComboboxCommandsCmdSetString)]
  public sealed partial class ComboCommandGroup : CommandGroup<ComboboxCommandsPackage>
  {
    [CommandId(CmdIDs.cmdidMyDropDownCombo)]
    [ListCommandId(CmdIDs.cmdidMyDropDownComboGetList)]
    public sealed class DropDownComboCommand : DropDownComboCommandHandler
    {
      protected override IEnumerable<string> GetListValues()
      {
        yield return Resources.Apples;
        yield return Resources.Oranges;
        yield return Resources.Pears;
        yield return Resources.Bananas;
      }

      protected override void OnExecute(OleMenuCommand command)
      {
        VsMessageBox.Show(SelectedValue, Resources.MyDropDownCombo);
      }
    }

    [CommandId(CmdIDs.cmdidMyIndexCombo)]
    [ListCommandId(CmdIDs.cmdidMyIndexComboGetList)]
    public sealed class IndexComboCommand : IndexComboCommandHandler
    {
      protected override IEnumerable<string> GetListValues()
      {
        yield return Resources.Lions;
        yield return Resources.Tigers;
        yield return Resources.Bears;
      }

      protected override void OnExecute(OleMenuCommand command)
      {
        VsMessageBox.Show(SelectedIndex.ToString(), Resources.MyDropDownCombo);
      }
    }

    [CommandId(CmdIDs.cmdidMyMRUCombo)]
    public sealed class MruComboCommand : MruComboCommandHandler
    {
      protected override void OnExecute(OleMenuCommand command)
      {
        VsMessageBox.Show(SelectedValue, Resources.MyMRUCombo);
      }
    }
  }
}
Command handler for DynamicCombo

using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.VisualStudio.Shell;
using VSXtra;

namespace DeepDiver.ComboboxCommands
{
  public partial class ComboCommandGroup
  {
    [CommandId(CmdIDs.cmdidMyDynamicCombo)]
    [ListCommandId(CmdIDs.cmdidMyDynamicComboGetList)]
    public sealed class DynamicComboCommand : DynamicComboCommandHandler
    {
      private readonly double[] _NumericZoomLevels = 
        { 4.0, 3.0, 2.0, 1.5, 1.25, 1.0, .75, .66, .50, .33, .25, .10 };
      private readonly string _ZoomToFit = Resources.ZoomToFit;
      private readonly string _Zoom_to_Fit = Resources.Zoom_to_Fit;
      private readonly NumberFormatInfo _NumberFormatInfo;

      public DynamicComboCommand()
      {
        CurrentZoomFactor = 1.0;
        SelectedValue = "100 %";
        _NumberFormatInfo = (NumberFormatInfo)CultureInfo.CurrentUICulture.NumberFormat.Clone();
      }

      protected override void OnExecute(OleMenuCommand command)
      {
        VsMessageBox.Show(CurrentZoomFactor.ToString(), Resources.MyDynamicCombo);
      }

      protected override IEnumerable<string> GetListValues()
      {
        for (int i = 0; i < _NumericZoomLevels.Length; i++)
          yield return _NumericZoomLevels[i].ToString("P0", _NumberFormatInfo);
        yield return _Zoom_to_Fit;
      }

      protected override bool IsInputValid(string input, out string output)
      {
        if (input.Equals(_ZoomToFit) || input.Equals(_Zoom_to_Fit))
        {
          CurrentZoomFactor = 0;
          output = _Zoom_to_Fit;
          return true;
        }
        output = input;
        CurrentZoomFactor = 0;
        try
        {
          float newZoom = Single.Parse(input.Replace(NumberFormatInfo.InvariantInfo.PercentSymbol, ""), CultureInfo.CurrentCulture);
          newZoom = (float)Math.Round(newZoom);
          if (newZoom < 0)
            throw (new ArgumentException(Resources.ZoomMustBeGTZero));
          CurrentZoomFactor = newZoom / (float)100.0;
          output = CurrentZoomFactor.ToString("P0", _NumberFormatInfo);
        }
        catch (FormatException)
        {
          // --- user typed in a non-numeric value, ignore it
        }
        catch (OverflowException)
        {
          // --- user typed in too large of a number, ignore it
        }
        return true;
      }

      public double CurrentZoomFactor { get; private set; }
    }
  }
}
Screenshots:

VSXtraCombobox1.png

Last edited Aug 5, 2008 at 7:59 AM by INovak, version 3

Comments

No comments yet.