0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C#のListBoxクラスを使ったカーソルを固定する実装方法

Posted at

[結論]↑↓キーを押下してもカーソルが移動しない実装方法

private int selectedIndex;

protected void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
    this.shDetailListBox.SelectedIndex = this.selectedIndex;
}
  1. ユーザーが↑↓キーを押下すると、ListBox が自動でインデックスを変更 → SelectedIndexChanged イベントが発生する。

  2. そのイベントの中で、SelectedIndexを元の値に戻している。

  3. 結果、ユーザーがいくらキーを押してもインデックスが変わらないように見える。

そもそもListBoxクラスとは

項目のリストを表示するコントロールを表す。

SelectedIndexプロパティについて

image.png

SelectedIndexプロパティを使えば、選択されている項目のインデックス番号を取得できる。インデックス番号は、一番上の項目が 0 で、その下が 1 となります。選択された項目がなければ、-1になる。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?