掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
リストボックスの選択した行番号を返すには? (ID:145555)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
ListBoxのSelectionModeで微妙に違うが結局はゴリゴリ書くしかないのかな VB6と同じで良ければIf以下は無くてそのままClickIndex を返せば良い。 Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged Console.WriteLine(GetListindex(Me, ListBox1).ToString()) End Sub '選択されていれば行番号を選択されていなければ-1を返す関数 Public Shared Function GetListindex(ByVal Form As Form, _ ByVal ListBox As ListBox) As Integer 'リストボックスの画面上のY位置 Dim y As Integer = Form.Top + ListBox.Top + SystemInformation.CaptionHeight 'リストボックスの選択Itemの位置 '(マウスのY位置 - ListBoxの位置) \ Itemの高さ(整数割り算) 'ListBox.ItemHeight / 2 は四捨五入を勘案 Dim ClickIndex As Integer = (Control.MousePosition.Y - _ y + ListBox.ItemHeight / 2) \ ListBox.ItemHeight - 1 'Itemが選択されれいるか否か If ListBox.SelectedIndices.IndexOf(ClickIndex) >= 0 Then Return ClickIndex '選択されていれば行番号を Else Return -1 '選択されていなければ0を返す End If End Function
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2020 Takeshi Okamoto All Rights Reserved.