リボン関連

複数のtoggleButton要素の中から1つだけしかオンにできないようにする

リボンのトグルボタン」で回答したコードです(下に記載しているコードは一部変更しています。)。

リボン上に複数のtoggleButton要素を設定したとき、いずれかのtoggleButton要素を選択したい → 1つのtoggleButton要素しかオンにできないようにしたい、という内容の質問でした。


この処理を実現するためにはtoggleButton要素getPressed属性のコールバックとonAction属性のコールバックを使います。

[リボンXML]

<?xml version="1.0" encoding="utf-8"?>
<customUI onLoad="tglSample_onLoad" xmlns="http://schemas.microsoft.com/office/2006/01/customui">
  <ribbon>
    <tabs>
      <tab id="tabSample" label="Sample Tab">
        <group id="grpSample" label="Sample Group">
          <toggleButton id="tglSample1" label="tgl1" size="normal" getPressed="tglSample_getPressed" onAction="tglSample_onAction" />
          <toggleButton id="tglSample2" label="tgl2" size="normal" getPressed="tglSample_getPressed" onAction="tglSample_onAction" />
          <toggleButton id="tglSample3" label="tgl3" size="normal" getPressed="tglSample_getPressed" onAction="tglSample_onAction" />
          <toggleButton id="tglSample4" label="tgl4" size="normal" getPressed="tglSample_getPressed" onAction="tglSample_onAction" />
          <toggleButton id="tglSample5" label="tgl5" size="normal" getPressed="tglSample_getPressed" onAction="tglSample_onAction" />
          <toggleButton id="tglSample6" label="tgl6" size="normal" getPressed="tglSample_getPressed" onAction="tglSample_onAction" />
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

[標準モジュール]

Option Explicit

Private myRibbon As IRibbonUI
Private flgTgl As Long

Private Sub tglSample_onLoad(ribbon As IRibbonUI)
  Set myRibbon = ribbon
  flgTgl = 0 '初期化
End Sub

Private Sub tglSample_getPressed(control As IRibbonControl, ByRef returnedVal)
  Select Case flgTgl
    Case 0
      returnedVal = False
    Case Else
      If Right$(control.ID, 1&) = CStr(flgTgl) Then
        returnedVal = True
      Else
        returnedVal = False
      End If
  End Select
End Sub

Private Sub tglSample_onAction(control As IRibbonControl, pressed As Boolean)
  If pressed Then
    flgTgl = CLng(Right$(control.ID, 1&))
    myRibbon.Invalidate
  End If
End Sub

上記コードを設定したファイルを開くと、”Sample Tab“タブとその中に6個のtoggleButton要素が表示されます。

tgl1」がオンになっている状態で「tgl2」をクリックしてオンにすると、

tgl1」がオフ「tgl2」がオンになり、1つのtoggleButton要素しかオンにできないことが確認できます。

Office Ribbon Editorがダウンロード出来ない?前のページ

複数のtoggleButton要素の中から1つだけしかオンにできないようにする(2)次のページ

関連記事

  1. リボン関連

    menu内にあるbuttonの数を増やす

    「外部のXMLファイルを読み込み、ユーザー名に応じてmenu内容を変更…

  2. リボン関連

    [リボン・カスタマイズ]splitButton要素の内容を動的に変更する。

    3年ほど前に書いた記事「menu内にあるbuttonの数を増やす」にコ…

  3. Office関連

    Custom UI Editorの最新版がGitHubで公開されました。

    下記記事等で紹介している、リボンUIをカスタマイズするためのツール「C…

  4. リボン関連

    Office Ribbon Editorはウィルス?

    私のHP「Office Ribbon Editorの紹介」でも紹介して…

  5. Excel

    Google TTSで文字列を読み上げるExcelアドイン

    前回の記事で書いたGoogle TTSで文字列を読み上げるマクロ(言語…

コメント

  1. この記事へのコメントはありません。

  1. この記事へのトラックバックはありません。

Time limit is exhausted. Please reload CAPTCHA.

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP