0

From this thread Here

it works fine and when i plug in my USB to TTL adapter which creates a port (comport10) it shows up in the box, however I want to have an initial empty combo box that the user has to drop down and choose the port, ive tried a google search with results like use ComboBoxComPorts.SelectedValue = -1 and variations but the combobox always shows a value. I wont paste the code in here as i have referred to it using the link above, unless you want it that is.

Jud Slater
  • 21
  • 4
  • ALWAYS post the relevant code directly. That link will be useless if the other pages disappears. – jmcilhinney Mar 19 '23 at 01:31
  • One thing that other code does wrong is setting the DataSource first. Always set DataSource after DisplayMember and ValueMember. It will work the other way but it means the binding happens first, then has to be changed. – jmcilhinney Mar 19 '23 at 01:34

3 Answers3

1

You simply set the SelectedIndex to -1 or the SelectedItem to Nothing after setting the DataSource.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
1

You can also add first dummy line in binding source :

'add items from Dictionary to BindingList
    _comPorts.Add(New ComPortInfo() With {.Caption = "Choose port...", .PortName = "0"}) 'add first line
    For Each kvp As KeyValuePair(Of String, String) In portDict
        _comPorts.Add(New ComPortInfo() With {.Caption = kvp.Value, .PortName = kvp.Key}) 'add
    Next
    

you will have to deal with ComboBoxComPorts.SelectedIndex if user select first line :

Private Sub ComboBoxComPorts_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxComPorts.SelectedIndexChanged
            If ComboBoxComPorts.SelectedIndex = 0 Then Exit Sub
            'etc....
        End Sub
tuyau2poil
  • 767
  • 1
  • 2
  • 7
  • Private Sub ComboBoxComPorts_SelectedIndexChanged sections gives the error has multiple definiations with identical signatures but other wise your other code works brilliantly – Jud Slater Mar 19 '23 at 14:53
  • good new ! for the sub, just past the if.... line in your existing sub, not the declaration as it allready exist. – tuyau2poil Mar 19 '23 at 16:27
0
ComboBoxComPorts.DropDownStyle = ComboBoxStyle.DropDownList
ComboBoxComPorts.SelectedIndex = -1

DropDownList

Specifies that the list is displayed by clicking the down arrow and that the text portion is not editable. This means that the user cannot enter a new value. Only values already in the list can be selected.

To deselect the currently selected item, set the SelectedIndex to -1.

smoorke
  • 49
  • 4
  • sorry i didn't read the code you linked but it seems your combobox is data-bound. i suggest you edit the given code to add a "None" or blank item at index 0 and use that to denote no selection is made. – smoorke Mar 18 '23 at 23:32
  • It is incorrect that you cannot set SelectedIndex to -1 if the control is bound. What is true is that the first item will be selected by default when binding occurs, so you need to set it in code after that. Also keep in mind that that will generate two SelectedIndexChanged events. – jmcilhinney Mar 19 '23 at 01:29
  • @jmcilhinney can either of you show a snippet of the code where this is achieved please – Jud Slater Mar 19 '23 at 11:05
  • @JudSlater, you already have everything you need. You posted a link to code that does the binding and this answer shows you how to clear the selection. Are you really saying that you can't put those two together? – jmcilhinney Mar 19 '23 at 12:06
  • @jmcilhinney I couldnt put anything together as no code snippet or suggestion was posted until Smoorke posted the code snippet, perhaps your message was posted before smoorke answered, either way the solution has been found, thanks for any input – Jud Slater Mar 20 '23 at 18:16
  • @JudSlater, but smoorke had already posted that code when you asked for an example here in these comments. You asked for an example 10 hours after you had already been provided with everything you needed. – jmcilhinney Mar 20 '23 at 23:49
  • @jmcilhinney Sorry bud I didnt see the code before seeing the messages, still learning how this site works. – Jud Slater Mar 25 '23 at 19:06
  • @jmcilhinney is the [microsoft documentation](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox.selectedindex) referring to something else then when it states `You cannot set the SelectedIndex of a ComboBox item to -1 if the item is a data-bound item.`? i'll edit my answer to omit that line – smoorke Mar 26 '23 at 02:27
  • @smoorke, I read that previously and assumed that it was talking about in the designer, but now I realise that the `SelectedIndex` property isn't exposed at design time. I'm not sure what that's talking about because you can absolutely set the `SelectedIndex` to -1 after setting the `DataSource`. It just seems to be flat-out wrong. – jmcilhinney Mar 26 '23 at 02:50