Results 1 to 7 of 7

Thread: Check Combobox selection for Duplicates

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2019
    Posts
    28

    Check Combobox selection for Duplicates

    Hello All,

    I have 4 combo boxes with COMM Ports to allow user to select what comm port to use to connect an instrument.

    Upon Clicking the Apply Button, I want to check and see that there are no duplicate selection on the COMM Port and pop an error message.

    This seems like an easy task but I am lost as to how to do it.

    CODE Code:
    1. Dim COMMbobox = {PSComboBox, PS2Combobox, DMM1ComboBox, DMM2ComboBox}
    2.  
    3. 'Check Duplicate between COMMbobox

  2. #2
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    2,065

    Re: Check Combobox selection for Duplicates

    Quote Originally Posted by Prasanna2019 View Post
    Hello All,

    I have 4 combo boxes with COMM Ports to allow user to select what comm port to use to connect an instrument.

    Upon Clicking the Apply Button, I want to check and see that there are no duplicate selection on the COMM Port and pop an error message.

    This seems like an easy task but I am lost as to how to do it.

    CODE Code:
    1. Dim COMMbobox = {PSComboBox, PS2Combobox, DMM1ComboBox, DMM2ComboBox}
    2.  
    3. 'Check Duplicate between COMMbobox
    Hi Prasanna,

    Firstly... you'll need to correct this line as shown.
    Code:
    Dim COMMbobox() = {PSComboBox, PS2Combobox, DMM1ComboBox, DMM2ComboBox}
    Then you'll be able to check each box in turn, remembering that the boxes are called: -

    COMMbobox(0)
    COMMbobox(1)
    COMMbobox(2)
    COMMbobox(3)
    Code:
        Dim opn(4) As Boolean 
        For i = 0 to 3
            	opn(i) = COMMbobox(i).IsOpen
        Next

    Poppa
    Last edited by Poppa Mintin; Apr 15th, 2021 at 09:44 AM.
    Along with the sunshine there has to be a little rain sometime.

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,202

    Re: Check Combobox selection for Duplicates

    If you want to get spicy you could use LINQ:
    Code:
    Dim duplicates = From cbo In COMMbobox
                     Group By cboText = cbo.Text
                     Into cboTexts = Group
                     Where cboTexts.Count() > 1
                     Select cboTexts.First().Text
    What this does is group the items in the array by their text, queries the groups where the count is more than 1, then selects the first item in the group's text.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2019
    Posts
    28

    Re: Check Combobox selection for Duplicates

    Hey Poppa,

    I get an error saying that 'IsOpen' is not a member of ComboBox.

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,372

    Re: Check Combobox selection for Duplicates

    Another approach

    Code:
            ' count ComboBoxes with selections
            Dim cts As Integer
            cts = (From cb In COMMbobox
                   Where cb.SelectedIndex >= 0
                   Select cb).Count
    
            ' count unique ComboBoxes 
            Dim ctUniq As Integer
            ctUniq = (From cb In COMMbobox
                      Where cb.SelectedIndex >= 0
                      Select cb.SelectedItem Distinct).Count
    
            If cts <> ctUniq Then
                'error here
                Stop
            Else
                'ok
                Stop
            End If
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    2,065

    Re: Check Combobox selection for Duplicates

    Quote Originally Posted by Prasanna2019 View Post
    Hey Poppa,

    I get an error saying that 'IsOpen' is not a member of ComboBox.
    Ah... Sorry, I misread your post, I thought your 'ComboBox' were names you had given to your ports.

    Pop.
    Along with the sunshine there has to be a little rain sometime.

  7. #7
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,567

    Re: Check Combobox selection for Duplicates

    Here is a solution

    • First item in each ComboBox is "Make a selection"
    • Each ComboBox has the same items
    • Prevents duplicate selections
    • Apply button is only enabled when validation is true


    Code:
    Public Class Form1
        ''' <summary>
        ''' ComboBoxes for ports
        ''' </summary>
        Private ComboBoxList As List(Of ComboBox)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            ' populate all ComboBoxes on form, not in panels etc
            ComboBoxList = Controls.OfType(Of ComboBox).ToList()
    
            ' dummy up for each ComboBox data source
            Dim choices = Enumerable.Range(1, 10).Select(Function(index) $"Port {index}").ToList
    
            ' default selection is 
            choices.Insert(0, "(Make a selection)")
    
            ' set SelectedIndexChange event for each ComboBox, set data sources
            For Each comboBox As ComboBox In ComboBoxList
    
                ' need to clone
                comboBox.DataSource = choices.Select(Function(portItem) portItem.Clone()).Cast(Of String).ToList()
                AddHandler comboBox.SelectedIndexChanged, AddressOf AllComboBoxes_SelectedIndexChanged
    
            Next
    
        End Sub
        ''' <summary>
        ''' * When current ComboBox is the same as another revert to make a selection
        ''' * Set Enabled property of Submit button
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub AllComboBoxes_SelectedIndexChanged(sender As Object, e As EventArgs)
    
            Dim changedBox As ComboBox = DirectCast(sender, ComboBox)
    
            Dim result = ComboBoxList.FirstOrDefault(
                Function(comboBox)
                    Return comboBox.Name <> changedBox.Name AndAlso
                           changedBox.SelectedIndex <> 0 AndAlso
                           comboBox.SelectedItem IsNot Nothing AndAlso
                           comboBox.SelectedItem.ToString = changedBox.SelectedItem.ToString()
                End Function)
    
    
            If result IsNot Nothing Then
                changedBox.SelectedIndex = 0
                MessageBox.Show($"Selected Option has already been chosen")
            End If
    
            ' no duplicates, none are non-selections
            ApplyButton.Enabled = ComboBoxList.All(Function(item) item.SelectedIndex <> 0)
    
        End Sub
    
        Private Sub ApplyButton_Click(sender As Object, e As EventArgs) Handles ApplyButton.Click
            Operations.Process(ComboBoxList.Select(Function(comboBox) comboBox.Text))
        End Sub
    End Class
    Public Class Operations
        Public Shared Sub Process(Ports As IEnumerable(Of String))
            For Each port As String In Ports
                Console.WriteLine(port)
            Next
        End Sub
    End Class
    Note on the code, with more knowledge much of the code would be placed into a class and an event would be used to indicate a problem but I kept it to all code in a form as most developers seems to code this way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width