Results 1 to 6 of 6

Thread: Combobox and Text value

  1. #1
    Join Date
    Jul 2009
    Posts
    77

    Combobox and Text value

    I have a ComboBox that uses an Access database to display text (may be not the best way I doubt). I wish it that each text has an image, for example I have the word dog, cat, rabbit. I want the word dog is behind a picture so that when users click on the word, the image appears in a PictureBox to display the image. But how can I associate a word with an image ?

    Here is my code to display in the PictureBox
    Code:
            Dim selectedItem As String
            selectedItem = ComboBox1.Text
     
            Try
     
                PictureBox1.Image = Image.FromFile(ComboBox1.SelectedItem.ToString)
     
            Catch ex As Exception
     
                ' Handle exception
     
            End Try
    I tried this originally in a combobox

    Code:
       
          ComboBox1.Items.Clear()
          ComboBox1.Items.Add("D:\Download\Image1.jpg")
          ComboBox1.Items.Add("D:\Download\Image2.jpg")
    but it gives me the link of the images in my combo ..

  2. #2
    Join Date
    May 2008
    Posts
    685

    Re: Combobox and Text value

    You can use a hashtable
    Code:
    Public Class Form1
        Dim az As Hashtable
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            az = New Hashtable()
            az.Add("chat", "C:\LeaveSheet.JPG")
            az.Add("chien", "C:\untitled.bmp")
            ComboBox1.Items.Add("dog")
            ComboBox1.Items.Add("cat")
        End Sub
     
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            Try
                PictureBox1.Image = Image.FromFile(az(ComboBox1.SelectedItem.ToString).ToString)
     
            Catch ex As Exception
                ' Handle exception
            End Try
        End Sub
    End Class

  3. #3
    Join Date
    Jul 2009
    Posts
    77

    Re: Combobox and Text value

    Thank you for that useful reply. One more thing, how could you do that when the mouse passes over a name, photo change by itself? unless I have to click ..
    I hesitate a mousesomething ..

  4. #4
    Join Date
    May 2008
    Posts
    685

    Re: Combobox and Text value

    Example: The mouse is over ListBox2, we have eX and eY as screen coordinates, and now how to get the index.

    We will first turn eX and eY in client coordinates (relative to the listbox)

    ListBox2.PointToClient(New Point(e.X, e.Y)
    Then ListBox2.IndexFromPoint () will return the index overflown.


    Private Sub ListBox2_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
    _Handles ListBox2.DragOver

    IndexdInsertion = ListBox2.IndexFromPoint(ListBox2.PointToClient(New Point(e.X, e.Y)))

    End Sub

  5. #5
    Join Date
    Jul 2009
    Posts
    77

    Re: Combobox and Text value

    I tried, I said that is not declared IndexedInsertion and ComboRace.IndexFromPoint for the IndexFromPoint is not a member of System.Windows.Form.ComboBox

    I have to have 5 different controls, and I want to use a combobox so is this possible with what I want to do?

  6. #6
    Join Date
    May 2008
    Posts
    685

    Re: Combobox and Text value

    For the combobox, I think it is possible because the listbox is admitted of the two dimension x, y. You can test including your picture in combobox. If you're interested in, here is an example

    In the load or when filling your combobox

    Code:
    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.ComboBox1.Items.AddRange(items)
     
            Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
     
            Me.ComboBox1.DrawMode = DrawMode.OwnerDrawVariable
     
            Me.ComboBox1.ItemHeight = Me.ImageList1.ImageSize.Height
     
            Me.ComboBox1.Width = Me.ImageList1.ImageSize.Width + 18
     
            Me.ComboBox1.MaxDropDownItems = Me.ImageList1.Images.Count
    In the event of combobox1_draw

    Code:
    Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
     
            If e.Index <> -1 Then
     
                If e.Index <> -1 Then
     
                    e.Graphics.DrawImage(Me.ImageList1.Images(e.Index), e.Bounds.Left, e.Bounds.Top)
     
                End If
     
            End If
        End Sub
    In the event ComboBox1_MeasureItem
    Code:
     Private Sub ComboBox1_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles ComboBox1.MeasureItem
     
            e.ItemHeight = Me.ImageList1.ImageSize.Height
     
            e.ItemWidth = Me.ImageList1.ImageSize.Width
     
        End Sub

Similar Threads

  1. Need Help With ComboBox
    By Reyas in forum Software Development
    Replies: 1
    Last Post: 29-04-2012, 12:53 PM
  2. Use of combobox in script
    By Martien in forum Software Development
    Replies: 3
    Last Post: 15-01-2010, 11:58 AM
  3. Using a variable from Flash combobox
    By Hashim in forum Software Development
    Replies: 3
    Last Post: 06-10-2009, 10:39 PM
  4. How to lock combobox
    By JamesB in forum Software Development
    Replies: 3
    Last Post: 19-03-2009, 07:20 PM
  5. Add ComboBox in DataGrid in VB 6.0
    By Jateen in forum Software Development
    Replies: 3
    Last Post: 15-01-2009, 06:31 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,541,785.98001 seconds with 17 queries