Jump to content

"Mascarar" valor combobox


Recommended Posts

Não sei se é bem isto que queres, mas fica aqui...

Este codigo mostra-te uma descrição conforme passas com o rato por cima dos itens da combobox..

Usa uma classe (Pessoa) que tem o valor do item (Nome) e a descrição:

Tem uma combobox (ComboBox1) e uma label (Label1) que mostra a descrição


Option Explicit On
Option Strict On

Public Class Form1

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Me.ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
    AddHandler ComboBox1.DrawItem, AddressOf ComboBox1_DrawItem

    Me.ComboBox1.Items.Add(New Pessoa("Maria", "Olhos verdes"))
    Me.ComboBox1.Items.Add(New Pessoa("Joana", "Cabelo comprido"))
    Me.ComboBox1.Items.Add(New Pessoa("Rita", "Mamas grandes"))

   End Sub

   Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem


    If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
	    e.Graphics.DrawString(ComboBox1.Items(e.Index).ToString, e.Font, Brushes.White, e.Bounds)
	    Dim p As Pessoa = DirectCast(Me.ComboBox1.Items(e.Index), Pessoa)
	    Me.Label1.Text = p.Descricao
	    e.Graphics.DrawString(ComboBox1.Items(e.Index).ToString, e.Font, Brushes.Black, e.Bounds)
    End If

   End Sub

   Private Sub ComboBox1_DropDownClosed(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.DropDownClosed
    Me.Label1.Text = String.Empty
   End Sub

End Class

Classe Pessoa:

Option Explicit On
Option Strict On

Public Class Pessoa

   Private _nome As String
   Private _descricao As String

   Public ReadOnly Property Descricao() As String
	    Return _descricao
    End Get
   End Property

   Public Sub New(ByVal Nome As String, ByVal Descricao As String)
    _nome = Nome
    _descricao = Descricao
   End Sub

   Public Overrides Function ToString() As String
    Return _nome
   End Function

End Class

Edited by vikcch
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.