Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 09-11-2009
Member
 
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 ..
Reply With Quote
  #2  
Old 09-11-2009
Member
 
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
Reply With Quote
  #3  
Old 09-11-2009
Member
 
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 ..
Reply With Quote
  #4  
Old 09-11-2009
Member
 
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
Reply With Quote
  #5  
Old 09-11-2009
Member
 
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?
Reply With Quote
  #6  
Old 09-11-2009
Member
 
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Combobox and Text value"
Thread Thread Starter Forum Replies Last Post
Need Help With ComboBox Reyas Software Development 1 29-04-2012 12:53 PM
Use of combobox in script Martien Software Development 3 15-01-2010 11:58 AM
Using a variable from Flash combobox Hashim Software Development 3 06-10-2009 10:39 PM
How to lock combobox JamesB Software Development 3 19-03-2009 07:20 PM
Add ComboBox in DataGrid in VB 6.0 Jateen Software Development 3 15-01-2009 06:31 PM


All times are GMT +5.5. The time now is 10:18 AM.