主婦と.NETと犬のぶろぐ

奥様 と プログラム と お犬様 の 楽しい日常

目次

Blog 利用状況

ニュース

書庫

日記カテゴリ

ListBox(System.Windows.Forms.ListBox)

ListBox も VB6.0 の時からお世話になっているコントロールです。
今回は、自分が普段あんまりやらない使い方(オーナードロー・複数列・FormatString)
を主にやってみました。

■参考文献
ListBox クラス

■実行画像
オーナードロー
ListBox オーナードロー
複数列・FormatString
ListBox 複数列・FormatString

Public Class ListBoxTest

Private m_tabControl As TabControl
Private Sub ListBoxTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.m_tabControl = New TabControl() Me.m_tabControl.Dock = DockStyle.Fill Me.m_tabControl.TabPages.Clear() Me.Controls.Add(Me.m_tabControl)
' オーナードロー Dim ownerDrawPage As TabPage = New TabPage("OwnerDraw") Me.m_tabControl.TabPages.Add(ownerDrawPage) Dim ownerDrawListBox As ListBox = New ListBox() ownerDrawPage.Controls.Add(ownerDrawListBox) With ownerDrawListBox ' オーナードローにする .DrawMode = DrawMode.OwnerDrawFixed ' スクロールバーを常に表示 .ScrollAlwaysVisible = True .Dock = DockStyle.Fill .BeginUpdate() With .Items .Clear() .Add(Color.Tomato) .Add(Color.LimeGreen) .Add(Color.LightSeaGreen) .Add(Color.MediumPurple) .Add(Color.LimeGreen) .Add(Color.LightSeaGreen) .Add(Color.MediumPurple) .Add(Color.Tomato) End With .EndUpdate() AddHandler .DrawItem, AddressOf OwnerDrawItem End With
' 複数行 Dim mulitColumnPage As TabPage = New TabPage("MulitColumn") Me.m_tabControl.TabPages.Add(mulitColumnPage) Dim multiColumnListBox As ListBox = New ListBox() mulitColumnPage.Controls.Add(multiColumnListBox)
Dim dt As DataTable = New DataTable("test") dt.Columns.Add("id", GetType(Integer)) dt.Columns.Add("ひづけ", GetType(DateTime)) For counter As Integer = 0 To 20 System.Threading.Thread.Sleep(100) dt.Rows.Add(counter + 100, DateTime.Now) Next
With multiColumnListBox .Dock = DockStyle.Fill .MultiColumn = True ' 複数選択できるようにする .SelectionMode = SelectionMode.MultiExtended ' Control キーなどを押しながら選択 '.SelectionMode = SelectionMode.MultiSimple ' Control キーなどを押す必要なし
' 書式指定 .FormattingEnabled = True .FormatString = " yy/MM/dd HH:mm:ss:ms "
' データバインド .DataSource = dt.DefaultView .DisplayMember = "ひづけ" .ValueMember = "id"
.ColumnWidth = CInt(.CreateGraphics().MeasureString(.Items(.Items.Count - 1).ToString(), .Font).Width) End With
' ContextMenuStrip Dim context As ContextMenuStrip = New ContextMenuStrip() Me.ContextMenuStrip = context
'' 選択されている情報 Dim selectedMenu As ToolStripMenuItem = New ToolStripMenuItem("選択されてる情報") context.Items.Add(selectedMenu) AddHandler selectedMenu.Click, AddressOf SelectedOut
End Sub
' ListBox.Item を オーナードローする Private Sub OwnerDrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Dim lstBox As ListBox = DirectCast(sender, ListBox) Dim itm As Color = DirectCast(lstBox.Items(e.Index), Color)
Dim rect As Rectangle = New Rectangle(e.Bounds.X + 1, _ e.Bounds.Y + 1, _ lstBox.GetItemRectangle(e.Index).Width, _ lstBox.GetItemRectangle(e.Index).Height - 1) '' 背景 If e.Index Mod 2 = 0 Then e.Graphics.FillRectangle(System.Drawing.SystemBrushes.Window, rect) Else ' 偶数行のみ LinearGradientBrush で塗りつぶし Using brush As System.Drawing.Drawing2D.LinearGradientBrush _ = New System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.White, itm, 0.0F) e.Graphics.FillRectangle(brush, rect) End Using End If
'' 文字 Using textBrush As System.Drawing.SolidBrush = New System.Drawing.SolidBrush(itm) e.Graphics.DrawString(itm.Name, _ e.Font, _ textBrush, _ New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)) End Using '' フォーカスがある時の枠を描画する e.DrawFocusRectangle() End Sub
' 選択されている情報 を 出力ウィンドウに出力 Private Sub SelectedOut(ByVal sender As Object, ByVal e As System.EventArgs) If Me.m_tabControl.SelectedTab Is Nothing Then Return Dim tp As TabPage = Me.m_tabControl.SelectedTab Dim lbox As ListBox = DirectCast(tp.Controls(0), ListBox)
Dim SelectedIndices As ListBox.SelectedIndexCollection = _ lbox.SelectedIndices
Dim selectedItems As ListBox.SelectedObjectCollection = _ lbox.SelectedItems()
Console.WriteLine("ActiveTab:" & tp.Text) Console.WriteLine("全項目数:" & lbox.Items.Count()) Console.WriteLine("選択項目数:" & SelectedIndices.Count()) Console.WriteLine("SelectedValue:") Console.WriteLine(Convert.ToString(lbox.SelectedValue)) Console.WriteLine("SelectedIndices:") For Each index As Integer In SelectedIndices Console.WriteLine(index.ToString()) Next Console.WriteLine("SelectedItems:") For Each item As Object In selectedItems If TypeOf (item) Is DataRowView Then Dim dr As DataRowView = DirectCast(item, DataRowView) For Each col As DataColumn In dr.DataView.Table.Columns Console.Write(" " & col.ColumnName & "→" & Convert.ToString(dr(col.ColumnName))) Next Console.WriteLine() Else Console.WriteLine(Convert.ToString(item)) End If Next End Sub End Class

投稿日時 : 2007年1月31日 11:21

Feedback

# re: ListBox(System.Windows.Forms.ListBox) 2007/01/31 12:00 じゃんぬねっと

やばいw ListBox を 1 回も使ったことがないwww
そういえば、RichTextBox も使ったことがないwww

# re: ListBox(System.Windows.Forms.ListBox) 2007/01/31 12:34 なおこ(・∀・)

>> じゃんぬさん
> RichTextBox
うはwwwわたしもだwww

# re: ListBox(System.Windows.Forms.ListBox) 2007/02/01 11:24 ぽぴ王子

この手のなおこ(・∀・)さんの例に中さんの写真がないと不安になる王子が来ましたよ o(゚д゚o≡o゚д゚)o

そういや僕も RichTextBox って使ったことないや。
ListBox はある…はず。

あまり推奨されない方法として使ったことなら、ある。
Visible = False な ListBox を大量に作って、Sort プロパティを True にして
文字列を Add していって ListBox にソートさせた。
こないだ @IT で同じことをしている発言(誰か忘れたけど、痛い質問をしてた人)を
見てちょっと思い出し苦笑い。
ええ、10年前の VB4 の頃ですが、何か?

ソートアルゴリズムぐらい自分で組めよな、と1年後(いや、半年後)の僕も思ってたはずです。
ホント、なんであんなの作っちゃったんだろ…と今でも思い出して絶賛後悔中。
恥ずかしくて恥ずかしくて穴があったら埋めたいです。

# re: ListBox(System.Windows.Forms.ListBox) 2007/02/04 9:33 なおこ(・∀・)

>> ぽぴ王子さん
> RichTextBox
使い道を考えてたところ、運よく?
入院する前の夜間診療を受け、電子カルテの入力画面でそれらしきものを使ってました。

...ってもう専業主婦なのに職業病が抜けないですね...

# This is my first time go to see at here and i am truly happy to read everthing at single place. 2018/10/05 2:23 This is my first time go to see at here and i am t

This is my first time go to see at here and i am truly happy to read everthing at single
place.

# I am really grateful to the owner of this site who has shared this wonderful paragraph at here. 2018/10/06 8:34 I am really grateful to the owner of this site who

I am really grateful to the owner of this site who has shared this wonderful paragraph at
here.

# You made some good points there. I looked on the web to find out more about the issue and found most individuals will go along with your views on this web site. 2018/10/07 11:48 You made some good points there. I looked on the

You made some good points there. I looked on the web to find out more about the issue
and found most individuals will go along with your views on this web site.

# Hey there! This is kind of off topic but I need some advice from an established blog. Is it tough to set up your own blog? I'm not very techincal but I can figure things out pretty fast. I'm thinking about making my own but I'm not sure where to begin. D 2018/10/10 5:14 Hey there! This is kind of off topic but I need so

Hey there! This is kind of off topic but I need some advice from
an established blog. Is it tough to set up your own blog?
I'm not very techincal but I can figure things out pretty fast.
I'm thinking about making my own but I'm not sure
where to begin. Do you have any tips or suggestions?

Thanks

# Woah! I'm really enjoying the template/theme of this blog. It's simple, yet effective. A lot of times it's challenging to get that "perfect balance" between usability and appearance. I must say you have done a amazing job with this. Also, the b 2018/10/29 21:49 Woah! I'm really enjoying the template/theme of th

Woah! I'm really enjoying the template/theme of this blog.

It's simple, yet effective. A lot of times it's challenging to
get that "perfect balance" between usability and
appearance. I must say you have done a amazing job
with this. Also, the blog loads extremely fast for me on Internet explorer.
Excellent Blog!

# Sling tv coupons and promo codes for november 2018 Heya i'm for the first time here. I found this board and I find It truly useful & it helped me out much. I hope to give something back and help others like you helped me. Sling tv coupons and promo 2018/11/17 6:20 Sling tv coupons and promo codes for november 2018

Sling tv coupons and promo codes for november 2018
Heya i'm for the first time here. I found this board and
I find It truly useful & it helped me out much. I hope to give something
back and help others like you helped me. Sling tv coupons and promo codes for november 2018

# re: ListBox(System.Windows.Forms.ListBox) 2021/07/08 6:43 hydrochloquine

cloraquinn https://chloroquineorigin.com/# hydrochoriquine

# re: ListBox(System.Windows.Forms.ListBox) 2021/07/24 15:39 whats hcq

choroquine https://chloroquineorigin.com/# hydroxychloroquine 200 mg twice a day

# Test, just a test 2022/12/13 9:34 candipharm com

canadian generic pills http://candipharm.com

タイトル
名前
Url
コメント