フォーカスを受け取らないボタンを作成する - C#

フォーカスを受け取らないボタンの作成方法を紹介します。

概要

Buttonクラスを継承したコンポーネントクラスを作成し、コンポーネントクラスのSelectableスタイルをFalseに設定することでフォーカスを受け取らないボタンを実現します。

準備

コンポーネントクラスを作成します。作成後
  public partial class NoFocusedButton : Component
  public partial class NoFocusedButton : Button
に置き換えます。

コード (コンポーネントクラスのコード)

以下のコードを記述します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ImageEditor
{
  public partial class NoFocusedButton : Button
  {
    public NoFocusedButton()
    {
      InitializeComponent();
    }

    public NoFocusedButton(IContainer container)
    {
      container.Add(this);

      InitializeComponent();

      SetStyle(ControlStyles.Selectable, false);
    }
  }
}
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
掲載日: 2012-09-01
iPentec all rights reserverd.