PDA

View Full Version : سوال: نمایش آیکون دد combo box



morteza@z
پنج شنبه 17 آذر 1390, 09:35 صبح
سلام چطوری می شود در کامبو باکس عکس گذاشت

arash020
جمعه 18 آذر 1390, 20:03 عصر
سلام




Code

The ComboIcon class has these Overrides:

Private ListaImg1 As New ImageList
'It is the ImageList associated to the Combo


Public Sub New()
DrawMode = DrawMode.OwnerDrawFixed
'Set the DrawMode to OwnerDraw

End Sub

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 = ListaImg1.ImageSize
Dim bounds As New Rectangle
bounds = e.Bounds
Try
item = Me.Items(e.Index)
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
That's it. The ComboBoxIconItem is very simple, it just has 2 properties: text and imageIndex, that is the imageindex of the image in the imagelist that should be displayed with the Item.

Using the code

So, in the Form, or where you want to put the comboBox you just have to write this code:

Dim MyCombo As New ComboIcon 'Create the Combo

MyCombo.ImageList = ImageList1
'Associates the Combo to an ImageList

MyCombo.Items.Add(New ComboBoxIconItem("Bart", 0))
'Add new Item text=Bart image=0

MyCombo.Items.Add(New ComboBoxIconItem("Marge", 2))
MyCombo.Items.Add(New ComboBoxIconItem("Homer", 1))
MyCombo.Items.Add(New ComboBoxIconItem("Lisa", 3))
MyCombo.Items.Add(New ComboBoxIconItem("Maggie", 4))
Me.Controls.Add(MyCombo) 'add the combobox to the form

'Place the combobox in a nice place

MyCombo.Top = 30
MyCombo.Left = 50

arash020
جمعه 18 آذر 1390, 20:04 عصر
سلام




Code

The ComboIcon class has these Overrides:

Private ListaImg1 As New ImageList
'It is the ImageList associated to the Combo


Public Sub New()
DrawMode = DrawMode.OwnerDrawFixed
'Set the DrawMode to OwnerDraw

End Sub

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 = ListaImg1.ImageSize
Dim bounds As New Rectangle
bounds = e.Bounds
Try
item = Me.Items(e.Index)
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
That's it. The ComboBoxIconItem is very simple, it just has 2 properties: text and imageIndex, that is the imageindex of the image in the imagelist that should be displayed with the Item.

Using the code

So, in the Form, or where you want to put the comboBox you just have to write this code:

Dim MyCombo As New ComboIcon 'Create the Combo

MyCombo.ImageList = ImageList1
'Associates the Combo to an ImageList

MyCombo.Items.Add(New ComboBoxIconItem("Bart", 0))
'Add new Item text=Bart image=0

MyCombo.Items.Add(New ComboBoxIconItem("Marge", 2))
MyCombo.Items.Add(New ComboBoxIconItem("Homer", 1))
MyCombo.Items.Add(New ComboBoxIconItem("Lisa", 3))
MyCombo.Items.Add(New ComboBoxIconItem("Maggie", 4))
Me.Controls.Add(MyCombo) 'add the combobox to the form

'Place the combobox in a nice place

MyCombo.Top = 30
MyCombo.Left = 50

ali_najari
یک شنبه 20 آذر 1390, 00:16 صبح
دوستان من این کامپونت رو قبلا نوشتم بهتره توی پست هایی که من دادم یکمی سرچ کنید میتونید پیدا کنید و تمامی اشکالاتش رو هم رفع کردم!
فکر کنم بهترین کامپونتی هست که بتونید استفاده کنید که قابلیت فارسی شدن رو داشته باشه