• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

How to set ComboBox that has DropDownList style to empty after the Form is displayed for the first time?

i tried using ComboBox1.SelectedIndex = -1 in the Form Load Events but it did not work, the ComboBox1 still displaying Index 0

thanks
0
yukisu
Asked:
yukisu
  • 2
  • 2
  • 2
1 Solution
 
planoczCommented:
If you just want to have the combobox text area emtpy change too dropdown and not dropdownlist
0
 
planoczCommented:
If you want to clear the whole list then

 ComboBox1.Items.Clear()
0
 
UnFiReCommented:

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ComboBox1.Text = Nothing
        ComboBox1.SelectedItem = Nothing
    End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
yukisuAuthor Commented:
planocz,
ComboBox1.Items.Clear() does not work in Form_Load

UnFiRe,
ComboBox1.Text = Nothing
ComboBox1.SelectedItem = Nothing
also do not set the dropdownlist to empty

i want to set the dropdownlist to empty so the user is force to select one value from the list, thus the default value for the dropdownlist must be blank.
i can do this by setting ComboBox1.SelectedIndex = -1, but only after i called it when the form already loaded.
setting ComboBox1.SelectedIndex = -1 in the form_load event does not work
so when the form open for the first time, the dropdownlist index always go back to index 0

anyone know how to set the dropdownlist index to -1 (empty/blank value) on Form_Load Event?

thanks
0
 
UnFiReCommented:
yukisu,
The ComboBox.SelectedItem = Nothing MUST work
If it does not, there must be a statement that is executed AFTER the "ComboBox.SelectedItem = Nothing" statement that selects an item in your combobox.
ComboBox1.SelectedIndex = -1 also MUST work. The reason you are getting something selected after even these statements have been executed, is
1. you have some statements being executed after those above that select an item in your combobox
2. your combobox is bound to a datasource

if it is option 2, you can use this approach:
Instead of setting your datasource and displaymember in the design view, set it programmatically on the DropDown event
Something like this

    Private Sub ComboBox1_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.DropDown
        ComboBox1.DataSource = me.DataSet1.Table1
        ComboBox1.DisplayMember = "column1"
    End Sub

In design view set the datasource to None
On form load event or in design view you can set the text of combobox to display when it is first loaded
such as "Select User"
0
 
yukisuAuthor Commented:
UnFiRe,
nice solution, but i cannot set the text for the ComboBox since it is a DropDownList

thanks a lot
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now