Results 1 to 4 of 4

Thread: Combobox with Image

  1. #1

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Combobox with Image

    I'm trying to place images in my combobox. Thanks to the search feature on this forum, I was able to find This example.

    When I add it to my code thought, it doesn't like the "MyBase.Load()" statement. It just underlines it with a squiggly blue line and says "Public Event Load (sender as object, e as system.eventargs) is an event and cannot be called directly. Use a 'Raise Event' statement to raise an event.

    What am I doing wrong?

  2. #2
    Hyperactive Member marniel647's Avatar
    Join Date
    Aug 2010
    Location
    MSDN Library
    Posts
    259

    Re: Combobox with Image

    can we see your full code.?

  3. #3

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Re: Combobox with Image

    Quote Originally Posted by marniel647 View Post
    can we see your full code.?
    Yes. I'm relatively new to VB.NET and I didn't write this code so that could very well be the issue. This is another dev's work.
    Code:
    Private Sub frmload()
          
            MyBase.Load()
    
            Dim items(Me.ImageList1.Images.Count - 1) As String
            For i As Int32 = 0 To Me.ImageList1.Images.Count - 1
                items(i) = "Item " & i.ToString
            Next
            Me.cboTrend.Items.AddRange(items)
            Me.cboTrend.DropDownStyle = ComboBoxStyle.DropDownList
            Me.cboTrend.DrawMode = DrawMode.OwnerDrawVariable
            Me.cboTrend.ItemHeight = Me.ImageList1.ImageSize.Height
            Me.cboTrend.Width = Me.ImageList1.ImageSize.Width + 18
            Me.cboTrend.MaxDropDownItems = Me.ImageList1.Images.Count
    
    
    
           
    
            'TODO: This line of code loads data into the 'DSStatusTrend.Status' table. You can move, or remove it, as needed.
            Me.StatusTableAdapter.Fill(Me.DSStatusTrend.Status)
            'TODO: This line of code loads data into the 'DSStatusTrend.ProJStatus' table. You can move, or remove it, as needed.
            Me.ProJStatusTableAdapter.Fill(Me.DSStatusTrend.ProJStatus)
            'TODO: This line of code loads data into the 'DsStatus.StatusTrend' table. You can move, or remove it, as needed.
            Me.StatusTrendTableAdapter.Fill(Me.DsStatus.StatusTrend)
            'TODO: This line of code loads data into the 'DsResults.Result4ExecSave' table. You can move, or remove it, as needed.
    
            'TODO: This line of code loads data into the 'DSTree.Site' tame.dosble. You can move, or remove it, as needed.
            Me.SiteTableAdapter.FillBy(Me.DSTree.Site, Val(ComboBox7.SelectedValue))
            '  Me.FiscalCalendarTableAdapter.Fill(Me.DSTree.FiscalCalendar, Now)
            Me.FiscalCalendarTableAdapter.Fill(Me.DSTree.FiscalCalendar)
    
            GroupBox2.Visible = False
            GroupBox1.Visible = False
            GroupBox3.Visible = False
            TabControl1.Visible = False
            cboFilter.SelectedIndex = 0
            '  TabControl2.Visible = False
            TreePopulate()
    
    
           
           
        End Sub

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Combobox with Image

    Add a ImageList to your form, add some images. Add the control to the form then add some items.

    Code:
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ComboIcon1.Items.Add("A", 0)
            ComboIcon1.Items.Add("B", 1)
            ComboIcon1.Items.Add("C", 2)
            ComboIcon1.Items.Add("D", 3)
            ComboIcon1.Items.Add("E", 4)
            ComboIcon1.SelectedIndex = 2
        End Sub
    End Class


    Control
    Code:
    Imports System.Windows.Forms
    Imports System.Drawing
    ''' <summary>
    ''' Used to display text and image
    ''' </summary>
    ''' <remarks>
    ''' The base is from Code Project and the overriding
    ''' of the items is Kevin S Gallagher for implementing
    ''' an additional Add method
    ''' </remarks>
    Public Class ComboIcon
       Inherits ComboBox
    
       Private mImageList As New ImageList
    
       Public Property ImageList() As ImageList
          Get
             Return mImageList
          End Get
          Set(ByVal sender As ImageList)
             mImageList = sender
          End Set
       End Property
    
       Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
          e.DrawBackground()
          e.DrawFocusRectangle()
    
          Dim Item As New ComboBoxIconItem
          Dim imageSize As New Size
          imageSize = mImageList.ImageSize
    
          Dim bounds As New Rectangle
          bounds = e.Bounds
    
          Try
             Item = CType(Me.Items(e.Index), ComboBoxIconItem)
             If (Item.ImageIndex <> -1) Then
                Me.ImageList.Draw(e.Graphics, bounds.Left, bounds.Top, Item.ImageIndex)
                e.Graphics.DrawString(Item.Text, e.Font, New SolidBrush(e.ForeColor), bounds.Left + imageSize.Width, bounds.Top)
             Else
                e.Graphics.DrawString(Item.Text, e.Font, New SolidBrush(e.ForeColor), bounds.Left, bounds.Top)
             End If
          Catch ex As Exception
             If (e.Index <> -1) Then
                e.Graphics.DrawString(Items(e.Index).ToString(), e.Font, New SolidBrush(e.ForeColor), bounds.Left, bounds.Top)
             Else
                e.Graphics.DrawString(Text, e.Font, New SolidBrush(e.ForeColor), bounds.Left, bounds.Top)
             End If
    
          End Try
          MyBase.OnDrawItem(e)
       End Sub
       '------------------------------------------------------------------------------------------------------------
       Public Class CollectionObjectInterface
          Implements IEnumerable
    
          Private owner As ComboIcon
    
          Friend Sub New(ByVal owner As ComboIcon)
             Me.owner = owner
          End Sub
          Public Sub AddRange(ByVal items() As Object)
             For Each item As Object In items
                Me.Add(item)
             Next
          End Sub
          Public Sub AddRange(ByVal value As ComboBox.ObjectCollection)
             For Each item As Object In value
                Me.Add(item)
             Next
          End Sub
          Public Sub Add(ByVal item As Object)
             Me.Insert(Me.Count, item)
          End Sub
          ''' <summary>
          ''' Simplifies adding a IconItem
          ''' </summary>
          ''' <param name="Text">Text to display</param>
          ''' <param name="Index">ImageList index for image to display</param>
          ''' <remarks></remarks>
          Public Sub Add(ByVal Text As String, ByVal Index As Integer)
             Dim Item = New ComboBoxIconItem(Text, Index)
             Me.Insert(Me.Count, Item)
          End Sub
          Public Sub Insert(ByVal index As Integer, ByVal item As Object)
             Me.owner.InnerItems.Insert(index, item)
          End Sub
          Public Sub Remove(ByVal item As Object)
             Dim index As Integer = Me.IndexOf(item)
             If (index > -1) Then
                Me.RemoveAt(index)
             End If
          End Sub
          Public Sub Clear()
             For i As Integer = (Me.Count - 1) To 0 Step -1
                Me.RemoveAt(i)
             Next
          End Sub
          Public Sub RemoveAt(ByVal index As Integer)
             Me.owner.InnerItems.RemoveAt(index)
          End Sub
          Public Function Contains(ByVal item As Object) As Boolean
             Return Me.owner.InnerItems.Contains(item)
          End Function
          Public Sub CopyTo(ByVal destination() As Object, ByVal arrayIndex As Integer)
             Me.owner.InnerItems.CopyTo(destination, arrayIndex)
          End Sub
          Public Function GetEnumerator() As System.Collections.IEnumerator Implements IEnumerable.GetEnumerator
             Return Me.owner.InnerItems.GetEnumerator()
          End Function
          Public Function IndexOf(ByVal value As Object) As Integer
             Return Me.owner.InnerItems.IndexOf(value)
          End Function
          Public ReadOnly Property Count() As Integer
             Get
                Return Me.owner.InnerItems.Count
             End Get
          End Property
          Public Property Item(ByVal index As Integer) As Object
             Get
                Return Me.owner.InnerItems(index)
             End Get
             Set(ByVal value As Object)
                Me.owner.InnerItems(index) = value
             End Set
          End Property
       End Class
       Private itemsInterface As CollectionObjectInterface
       Public Sub New()
          DrawMode = DrawMode.OwnerDrawFixed
          Me.itemsInterface = New CollectionObjectInterface(Me)
       End Sub
       Public Shadows ReadOnly Property Items() As CollectionObjectInterface
          Get
             Return Me.itemsInterface
          End Get
       End Property
       Private ReadOnly Property InnerItems() As ComboBox.ObjectCollection
          Get
             Return MyBase.Items
          End Get
       End Property
       '------------------------------------------------------------------------------------------------------------
    End Class
    
    Public Class ComboBoxIconItem
       Private mText As String
       Property Text() As String
          Get
             Return mText
          End Get
          Set(ByVal Value As String)
             mText = Value
          End Set
       End Property
    
       Private mImageIndex As Integer
       Property ImageIndex() As Integer
          Get
             Return mImageIndex
          End Get
    
          Set(ByVal Value As Integer)
             mImageIndex = Value
          End Set
       End Property
       Public Sub New()
          mText = ""
       End Sub
       Public Sub New(ByVal text As String)
          mText = text
       End Sub
       Public Sub New(ByVal text As String, ByVal imageIndex As Integer)
          mText = text
          mImageIndex = imageIndex
       End Sub
       Public Overrides Function ToString() As String
          Return mText
       End Function
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width