הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Thursday, February 21, 2013 5:40 AM
hi, guys,
i have a text box and a combo box, and what i want to do is when User selects the item from combo box it should be sent to the text box to display as string, and if that item is sent to the text box then the item's text or item's background color in the combo box should be changed. so that it can be notified that the item which has different color in combo box is Already selected.
and i also want that when selects the colored item from combo box, the item already selected should be deleted from the text box please help me how to do this in vb.net..
All replies (1)
Thursday, February 21, 2013 7:27 AM ✅Answered
Create a variable to store the index of the currently selected item in the combobox. Then use the example here
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.drawmode(v=vs.100).aspx
which shows how to change the color, font and size of a combobox item based on its index. You can use the same code, but adjust the color only based on the index number that indicates which item has been copied to the text box.
If the user selcts an item you first check if it's the selected item. Adjust the saved variable and the text box accordingly, and continue to draw the combobox items.
Public Class Form1
Private animals() As String
Dim SelectedItemIndex As Integer = -1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
ComboBox1.DropDownWidth = 250
ComboBox1.DropDownStyle = ComboBoxStyle.DropDown
animals = New String() {"Elephant", "Crocodile", "lion"}
ComboBox1.DataSource = animals
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If SelectedItemIndex = ComboBox1.SelectedIndex Then
SelectedItemIndex = -1
tbxSelectedItem.Text = ""
Else
SelectedItemIndex = ComboBox1.SelectedIndex
tbxSelectedItem.Text = ComboBox1.Items(SelectedItemIndex)
End If
End Sub
Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
Dim myFont As System.Drawing.Font = ComboBox1.Font
Dim TextColor As New System.Drawing.Color
If e.Index = selecteditemindex Then
TextColor = System.Drawing.Color.Red
Else
TextColor = System.Drawing.Color.Black
End If
Dim myBrush As SolidBrush = New SolidBrush(TextColor)
' Draw the background of the item.
e.DrawBackground()
e.Graphics.DrawString(animals(e.Index), myFont, myBrush, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
' Draw the focus rectangle if the mouse hovers over an item.
e.DrawFocusRectangle()
End Sub
End Class