שתף באמצעות


ComboBox Default Value

Question

Monday, November 24, 2014 8:04 PM

What is the option called in properties for setting the default selection of a DropDownList.

All replies (9)

Monday, November 24, 2014 8:14 PM ✅Answered

Hello,

ComboBoxName.SelectedIndex = x where x (a integer) is the item to select

EDIT Here are some simple examples which requires three ComboBox controls. The first two work off the DataSource while the last on the Items (ObjectCollection) property of the ComboBox

ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
ComboBox1.DataSource = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames.ToList
ComboBox1.SelectedIndex = Weekday(Now, FirstDayOfWeek.Sunday) - 1

ComboBox2.DropDownStyle = ComboBoxStyle.DropDownList
ComboBox2.DataSource = New List(Of String) From {"John", "Mary", "Karen"}
Dim Index = ComboBox2.Items.IndexOf("Karen")
If Index > -1 Then
    ComboBox2.SelectedIndex = Index
Else
    MessageBox.Show("Not located")
End If

ComboBox3.Items.AddRange(New String() {"Apples", "Oranges"})
Index = ComboBox3.Items.IndexOf("Pears")
If Index > -1 Then
    ComboBox3.SelectedIndex = Index
Else
    ComboBox3.SelectedIndex = 0
End If

Note in ComboBox3 I select the first item when not found, if we did nothing then the index would be -1 thus showing no item. Now if we want to simple do this using ComboBox1 we can do the following so no item is selected else the first item is selected

ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
ComboBox1.DataSource = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames.ToList
ComboBox1.SelectedIndex = -1

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Tuesday, November 25, 2014 1:46 AM ✅Answered

That doesn't work. I have it set to DropDownList so users can't edit it.
So whenever I set the text it stays blank until I select from the dropdown manually.

You can also set the .SelectedIndex.

Where are you "setting it" in code?

Still lost in code, just at a little higher level.

:-)


Tuesday, November 25, 2014 11:50 AM ✅Answered

That doesn't work. I have it set to DropDownList so users can't edit it.
So whenever I set the text it stays blank until I select from the dropdown manually.

 Ah, ok. I missed the part where you said it was set to DropDownList. The best option you have that i know of is to do as Kevin and Frank have mentioned. That would be to set the SelectedIndex or the SelectedItem property in the Form.Load event.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If ComboBox1.Items.Count > 0 Then
            ComboBox1.SelectedIndex = 0 'sets the 1st item as the selected item when the form loads
        End If
    End Sub

If you say it can`t be done then i`ll try it


Tuesday, November 25, 2014 12:42 PM ✅Answered

Hello,

I modified my reply to include several examples. According to "user's can't edit it", that is covered.

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Monday, November 24, 2014 9:51 PM

Hello,

ComboBoxName.SelectedIndex = x where x (a integer) is the item to select

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

Im making a Windows Form application. I don't just put that in the form, and I can't find the option in the properties of the ComboBox


Monday, November 24, 2014 10:22 PM

Hi,

 You can set the Text property of the ComboBox to the Text of the Item you want used as the default Item. You can do that in the [Design] tab.

 For example, if you add the following items to the ComboBox`s Items collection in the design tab,

Item 1

Item 2

 and then set the Combobox`s Text property to Item 1 then Item 1 will be the selected item when the application is run.

If you say it can`t be done then i`ll try it


Tuesday, November 25, 2014 12:29 AM

Hi,

 You can set the Text property of the ComboBox to the Text of the Item you want used as the default Item. You can do that in the [Design] tab.

 For example, if you add the following items to the ComboBox`s Items collection in the design tab,

Item 1

Item 2

 and then set the Combobox`s Text property to Item 1 then Item 1 will be the selected item when the application is run.

If you say it can`t be done then i`ll try it

That doesn't work. I have it set to DropDownList so users can't edit it.
So whenever I set the text it stays blank until I select from the dropdown manually.


Tuesday, November 25, 2014 9:13 AM

Hi,

I think you are asking ASP.NET DropdownList control.I think, in ASP.NET DropDownList cannot able to edit the value.

You better create a custom control or jquery to edit a DropDownList as per Your user requirement.

Thanks,


Tuesday, November 25, 2014 12:48 PM

What is the option called in properties for setting the default selection of a DropDownList.

Why would you want to preset something that's meant for the user to select from?

Either way, be aware that the .SelectedIndex event will be raised.

Still lost in code, just at a little higher level.

:-)