.NET2003で複数列表示のコンボボックスにするには?

解決


とす  2003-09-03 04:22:41  No: 79243

.NET2003のコンボボックスを使用して、ACCESSのコンボボックスのようにドロップダウンリスト部分のみを複数列表示にすることは可能でしょうか?


魔界の仮面弁士  2003-09-03 08:39:59  No: 79244

> .NET2003のコンボボックス
これは、System.Web.UI.WebControls.DropDownList の事ではなく、
System.Windows.Forms.ComboBox クラスの事ですよね?

とりあえず、こんな感じにしてみては如何でしょう。
コードにもう少し手を加えれば、汎用的な複数列対応ComboBoxコントロールにもできるかも。

Private Class ItemData
    Public ISBN As String
    Public Title As String
    Public Sub New(ByVal ISBN As String, ByVal Title As String)
        Me.ISBN = ISBN
        Me.Title = Title
    End Sub
    Public Overrides Function ToString() As String
        Return Title
    End Function
End Class

Private mySize As SizeF

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    With Me.ComboBox1
        .DrawMode = DrawMode.OwnerDrawFixed
        .DropDownStyle = ComboBoxStyle.DropDown
        mySize = .CreateGraphics.MeasureString("0-00000-000-0", .Font)
        .Items.Add(New ItemData("4-7981-0216-4", "標準 VB.NETプログラミング"))
        .Items.Add(New ItemData("1-56592-670-6", "Visual Basic Shell Programming"))
        .Items.Add(New ItemData("4-88135-831-6", "COM IDL & インターフェイスデザイン"))
    End With
End Sub

Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
    Const Margin As Integer = 5%

    Dim Item As ItemData = DirectCast(Me.ComboBox1.Items(e.Index), ItemData)
    Dim myPen As New Pen(Color.Brown)
    Dim SeparatorPosition As Single
    Dim B As Brush

    If e.State = DrawItemState.Selected Then
        B = SystemBrushes.HighlightText
    Else
        B = SystemBrushes.WindowText
    End If

    e.DrawBackground()
    With e.Graphics
        SeparatorPosition = Margin * 2 + mySize.Width
        .DrawString(Item.ISBN, e.Font, B, Margin, e.Bounds.Y)
        .DrawString(Item.Title, e.Font, B, SeparatorPosition + Margin, e.Bounds.Y)
        .DrawLine(myPen, SeparatorPosition, e.Bounds.Top, SeparatorPosition, e.Bounds.Bottom)
    End With

End Sub


とす  2003-09-03 19:54:37  No: 79245

なるほど、こんな手があったんですね。
おかげさまで、なんとか汎用的な形にも出来ました。
またなにか質問するときがありましたら、よろしくお願いします。


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加