469,645 Members | 1,526 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

How to populate a second drop down based on the content of a first


Hi,

I have two drop downs and the content of the second one must changed everytime the user changes the selection in the first one. Is any easy way of doing this?

Thanks in advance.
Alonso

Nov 20 '05 #1
3 1523
Are these bound to tables in a dataset. If so, bind one to the master and
one to a DataRelation which you need to create between the master and
dependent table.

If this is not the case that they are bound, then what criteria are you
using to fill the Combo's

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Alonso" <Al****@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...

Hi,

I have two drop downs and the content of the second one must changed everytime the user changes the selection in the first one. Is any easy way
of doing this?
Thanks in advance.
Alonso

Nov 20 '05 #2
Hi,

Change the content of the second combobox it the first combobox
selected index changed event.

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

ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
ComboBox2.DropDownStyle = ComboBoxStyle.DropDownList

ComboBox1.Items.Add("Fruit")
ComboBox1.Items.Add("Pets")
ComboBox1.Items.Add("Cars")
ComboBox1.SelectedIndex = 0
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

ComboBox2.Items.Clear()
Select Case ComboBox1.SelectedIndex
Case 0
ComboBox2.Items.Add("Apple")
ComboBox2.Items.Add("Peach")
ComboBox2.Items.Add("Orange")
Case 1
ComboBox2.Items.Add("Dog")
ComboBox2.Items.Add("Cat")
ComboBox2.Items.Add("Parrot")
Case Else
ComboBox2.Items.Add("Pickup truck")
ComboBox2.Items.Add("SUV")
ComboBox2.Items.Add("Compact Car")
End Select
ComboBox2.SelectedIndex = 0
End Sub
Ken
--------------

"Alonso" <Al****@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com:
Hi,

I have two drop downs and the content of the second one must changed
everytime the user changes the selection in the first one. Is any easy way
of doing this?

Thanks in advance.
Alonso


--
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus (http://www.grisoft.com).
Version: 7.0.230 / Virus Database: 263.3.6 - Release Date: 6/25/2004
Nov 20 '05 #3
* =?Utf-8?B?QWxvbnNv?= <Al****@discussions.microsoft.com> scripsit:
I have two drop downs and the content of the second one must changed
everytime the user changes the selection in the first one. Is any easy
way of doing this?


You could store objects in the 1st combobox that provide the strings for
the 2nd dropdown in a public property:

\\\
Public Class MyItem
Private m_Text As String
Private m_SubItems() As String

Public Property Text() As String
Get
Return m_Text
End Get
Set(ByVal Value As String)
m_Text = Value
End Set
End Property

Public Property SubItems() As String()
Get
Return m_SubItems()
End Get
Set(ByVal Value() As String)
m_SubItems = Value
End Set
End Property

Public Overrides Function ToString() As String
Return m_Text
End Function
End Class
///

When selection in the 1st combobox changes, you can take the selected
item of the 1st combobox and loop through its 'SubItems' and add the
items to the 2nd combobox.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Greg Scharlemann | last post: by
1 post views Thread by Greg Scharlemann | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.