Click here to Skip to main content
15,887,027 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: Listbox Highlight color! Pin
Richard MacCutchan18-Jun-23 5:40
mveRichard MacCutchan18-Jun-23 5:40 
GeneralRe: Listbox Highlight color! Pin
Member Alienoiz18-Jun-23 6:00
Member Alienoiz18-Jun-23 6:00 
GeneralRe: Listbox Highlight color! Pin
Richard MacCutchan18-Jun-23 6:39
mveRichard MacCutchan18-Jun-23 6:39 
GeneralRe: Listbox Highlight color! Pin
Member Alienoiz18-Jun-23 6:49
Member Alienoiz18-Jun-23 6:49 
GeneralRe: Listbox Highlight color! Pin
Member Alienoiz18-Jun-23 7:08
Member Alienoiz18-Jun-23 7:08 
GeneralRe: Listbox Highlight color! Pin
Member Alienoiz18-Jun-23 7:47
Member Alienoiz18-Jun-23 7:47 
GeneralRe: Listbox Highlight color! Pin
Member Alienoiz18-Jun-23 8:41
Member Alienoiz18-Jun-23 8:41 
GeneralRe: Listbox Highlight color! Pin
Richard MacCutchan18-Jun-23 21:39
mveRichard MacCutchan18-Jun-23 21:39 
Here is a complete sample to do what you need:
VB.NET
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing

Module FormMain
    Public Sub Main()
        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)
        Application.Run(New LbForm)
    End Sub
End Module

'
' A simple form using a ListBox
'
Public Class LbForm
    Inherits System.Windows.Forms.Form

    Private Sub InitializeComponent()
        ListBox1 = New System.Windows.Forms.ListBox()
        TextBox1 = New System.Windows.Forms.TextBox()
        CopyBtn = New System.Windows.Forms.Button()
        SuspendLayout()
        '
        'ListBox1
        '
        ListBox1.Name = "ListBox1"
        ListBox1.Location = New System.Drawing.Point(33, 44)
        ListBox1.Size = New System.Drawing.Size(200, 200)
        ListBox1.TabIndex = 0
        ListBox1.FormattingEnabled = True
        ListBox1.ItemHeight = 16
        ListBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple
        ListBox1.Sorted = True
        ' Set the border style to a single, flat border.
        ListBox1.BorderStyle = BorderStyle.FixedSingle

        ' Set the DrawMode property to the OwnerDrawVariable value. 
        ' This means the MeasureItem and DrawItem events must be 
        ' handled.
        ListBox1.DrawMode = DrawMode.OwnerDrawVariable
        ListBox1.ClearSelected()
        '
        'TextBox1
        '
        TextBox1.Name = "TextBox1"
        TextBox1.Location = New System.Drawing.Point(253, 44)
        TextBox1.Size = New System.Drawing.Size(200, 200)
        TextBox1.TabIndex = 2
        TextBox1.Multiline = True
        '
        'CopyBtn
        '
        CopyBtn.Name = "CopyBtn"
        CopyBtn.Location = New System.Drawing.Point(206, 272)
        CopyBtn.Size = New System.Drawing.Size(75, 23)
        CopyBtn.TabIndex = 1
        CopyBtn.Text = "Copy"
        CopyBtn.UseVisualStyleBackColor = True
        '
        'LbForm
        '
        AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 16.0!)
        Font = New System.Drawing.Font("Calibri", 10.0)
        AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Name = "LbForm"
        ClientSize = New System.Drawing.Size(486, 314)
        Controls.Add(ListBox1)
        Controls.Add(TextBox1)
        Controls.Add(CopyBtn)
        Text = "ListBox Sample"
        ResumeLayout(False)
        PerformLayout()
    End Sub

    Private WithEvents ListBox1 As ListBox
    Private WithEvents TextBox1 As TextBox
    Private WithEvents CopyBtn As Button

    Public Sub New()
        InitializeComponent()
        GetFileNames()
    End Sub

    Private Sub GetFileNames()
        Dim openFileDialog1 As New OpenFileDialog()

        openFileDialog1.InitialDirectory = ".\"
        openFileDialog1.Filter = "VB.NET Files (*.vb)|*.vb|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 1
        openFileDialog1.Multiselect = True

        If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            For Each fname in openFileDialog1.SafeFileNames
                ListBox1.Items.Add(fname)
            Next
        End If
    End Sub

    '
    ' copy any selected items into the text box
    '
    Private Sub CopyBtn_Click(sender As Object, e As EventArgs) Handles CopyBtn.Click
        Dim lblist As StringBuilder
        lblist = New StringBuilder
        For Each name As String In ListBox1.SelectedItems
            lblist.AppendLine(name)
        Next
        ListBox1.ClearSelected()
        TextBox1.Text = lblist.ToString()
    End Sub

    ' Handle the DrawItem event for an owner-drawn ListBox.
    Private Sub ListBox1_DrawItem(ByVal sender As Object, _
        ByVal e As DrawItemEventArgs) Handles ListBox1.DrawItem

        ' If the item is the selected item, then draw the rectangle filled in
        ' yellow. The item is selected when a bitwise And of the State property
        ' and the DrawItemState.Selected property is true. 
        If ((e.State And DrawItemState.Selected) = DrawItemState.Selected) Then
            e.Graphics.FillRectangle(Brushes.Yellow, e.Bounds)
        Else
            ' Otherwise, draw the rectangle filled in beige.
            e.Graphics.FillRectangle(Brushes.Beige, e.Bounds)
        End If

        ' Draw a rectangle in blue around each item.
        e.Graphics.DrawRectangle(Pens.Blue, e.Bounds)

        ' Draw the text in the item.
        e.Graphics.DrawString(ListBox1.Items(e.Index), Me.Font, _
            Brushes.Black, e.Bounds.X, e.Bounds.Y)

        ' Draw the focus rectangle around the selected item.
        e.DrawFocusRectangle()
    End Sub

    ' Handle the MeasureItem event for an owner-drawn ListBox.
    Private Sub ListBox1_MeasureItem(ByVal sender As Object, _
        ByVal e As MeasureItemEventArgs) Handles ListBox1.MeasureItem

        ' Make the listbox item slightly higher
        e.ItemHeight += 5
    End Sub

End Class

I took some of the code from the link to the MSDN documentation that I provided in an earlier message. The basic idea is that it gets a list of filenames and populates the ListBox with them. You can then test the selection of individual items, and how to process the resulting set.
GeneralRe: Listbox Highlight color! Pin
Member Alienoiz19-Jun-23 0:46
Member Alienoiz19-Jun-23 0:46 
GeneralRe: Listbox Highlight color! Pin
Richard MacCutchan19-Jun-23 0:50
mveRichard MacCutchan19-Jun-23 0:50 
GeneralRe: Listbox Highlight color! Pin
Member Alienoiz19-Jun-23 1:02
Member Alienoiz19-Jun-23 1:02 
GeneralRe: Listbox Highlight color! Pin
Richard MacCutchan19-Jun-23 1:38
mveRichard MacCutchan19-Jun-23 1:38 
GeneralRe: Listbox Highlight color! Pin
Member Alienoiz19-Jun-23 2:28
Member Alienoiz19-Jun-23 2:28 
GeneralRe: Listbox Highlight color! Pin
Richard MacCutchan19-Jun-23 2:44
mveRichard MacCutchan19-Jun-23 2:44 
GeneralRe: Listbox Highlight color! Pin
Member Alienoiz19-Jun-23 2:51
Member Alienoiz19-Jun-23 2:51 
GeneralRe: Listbox Highlight color! Pin
Richard MacCutchan19-Jun-23 3:01
mveRichard MacCutchan19-Jun-23 3:01 
GeneralRe: Listbox Highlight color! Pin
Member Alienoiz19-Jun-23 3:08
Member Alienoiz19-Jun-23 3:08 
GeneralRe: Listbox Highlight color! Pin
Member Alienoiz19-Jun-23 3:50
Member Alienoiz19-Jun-23 3:50 
GeneralRe: Listbox Highlight color! Pin
Richard MacCutchan19-Jun-23 4:50
mveRichard MacCutchan19-Jun-23 4:50 
GeneralRe: Listbox Highlight color! Pin
Member Alienoiz19-Jun-23 5:06
Member Alienoiz19-Jun-23 5:06 
GeneralRe: Listbox Highlight color! Pin
Member Alienoiz18-Jun-23 6:08
Member Alienoiz18-Jun-23 6:08 
GeneralRe: Listbox Highlight color! Pin
Ralf Meier18-Jun-23 22:13
mveRalf Meier18-Jun-23 22:13 
Questionlistbox mass edit Pin
Member Alienoiz10-Jun-23 5:59
Member Alienoiz10-Jun-23 5:59 
AnswerRe: listbox mass edit Pin
Dave Kreskowiak10-Jun-23 6:14
mveDave Kreskowiak10-Jun-23 6:14 
GeneralRe: listbox mass edit Pin
Member Alienoiz10-Jun-23 6:53
Member Alienoiz10-Jun-23 6:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.