guys help me with the code for adding items in the combo box but without duplicates. i have an assignment which is a simple web browser. the combo box will be the address bar. and everytime i input an address, it will be added in combo box and should not repeat just like in the browsers. i hope you could help me. thanks

Dani AI

Generated

Brief, practical addendum: and point out the obvious quick checks — those work for tiny, throwaway lists. For a reliable address-bar history that avoids duplicates over time you want three things: normalize the address (so "http://example.com", "example.com/" and "EXAMPLE.COM" become the same key), use a fast lookup (HashSet) to avoid linear scans, and persist the list if you want it to survive app restarts.

WinForms (VB.NET) pattern — normalize, test a HashSet, then add and persist:

' Form-level
Private addressHistory As HashSet(Of String)

Private Sub Form1_Load(...) Handles MyBase.Load
    Dim stored = If(My.Settings.RecentUrls, New System.Collections.Specialized.StringCollection())
    addressHistory = New HashSet(Of String)(stored.Cast(Of String)(), StringComparer.OrdinalIgnoreCase)
    ComboBox1.Items.AddRange(stored.Cast(Of String)().ToArray())
End Sub

Private Sub AddAddress(rawInput As String)
    Dim s = rawInput.Trim()
    If Not s.StartsWith("http://", StringComparison.OrdinalIgnoreCase) AndAlso Not s.StartsWith("https://", StringComparison.OrdinalIgnoreCase) Then
        s = "http://" & s
    End If

    Dim u As Uri = Nothing
    Dim key As String = If(Uri.TryCreate(s, UriKind.Absolute, u),
                          (u.Scheme & "://" & u.Host & If(u.AbsolutePath = "/", "", u.AbsolutePath)).TrimEnd("/"c).ToLowerInvariant(),
                          s.ToLowerInvariant())

    If addressHistory.Add(key) Then
        ComboBox1.Items.Insert(0, key)
        If My.Settings.RecentUrls Is Nothing Then My.Settings.RecentUrls = New System.Collections.Specialized.StringCollection()
        My.Settings.RecentUrls.Insert(0, key)
        My.Settings.Save()
    End If
End Sub

Web/ASP.NET pattern — client-side MRU with HTML5 datalist + localStorage (works per browser):

<script>
const key = 'addrHistory';
let history = JSON.parse(localStorage.getItem(key) || '[]');

function normalize(u){
  u = u.trim();
  if (!/^https?:\/\//i.test(u)) u = 'http://' + u;
  try { let url = new URL(u); return (url.origin + url.pathname).replace(/\/$/,'').toLowerCase(); }
  catch(e){ return u.toLowerCase(); }
}

function addAddress(raw){
  const n = normalize(raw);
  if (!history.includes(n)) {
    history.unshift(n); history = history.slice(0,50);
    localStorage.setItem(key, JSON.stringify(history));
    rebuildDatalist();
  }
}
</script>

<input id="addr" list="historyList">
<datalist id="historyList"></datalist>

Notes/troubleshooting: if your ComboBox is data-bound you must update the underlying collection (not Items.Add). Normalize consistently (add scheme when missing) so the same real URL can't slip in under multiple forms. This approach addresses the simple checks suggested earlier by and while preventing subtle duplicates and making history persistent.

Recommended Answers

All 3 Replies

Member Avatar for Member #857553

ComboBox.FindString will return the index of an item that matches.

See if this helps.

If combox1.FindString("The String To Search") < 0 Then
            combox1.Items.Add("The String To Search")
        End If

Thanks for your valuable sharing of combobox add items.

The following will add "some text" to the combo box only if it doesn't already exist.

Dim text As String = "some text"

If Not ComboBox1.Items.Contains(text) Then
    ComboBox1.Items.Add(text)
End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.