Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. inserting item into list box

inserting item into list box

Scheduled Pinned Locked Moved C#
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tanweer
    wrote on last edited by
    #1

    hi i m inserting some vlues into a listbox but now i want to restrict the duplication of entries in the list box private void lblpush_Click(object sender, EventArgs e) { for (int i = 0; i < tbxnumber.Text.Length - 1; i++) { if (list1.Items[i] == tbxnumber.Text[i]) return; object list = tbxnumber.Text[i]; list1.Items.Add(list); } } but it does not work

    H OriginalGriffO 2 Replies Last reply
    0
    • T tanweer

      hi i m inserting some vlues into a listbox but now i want to restrict the duplication of entries in the list box private void lblpush_Click(object sender, EventArgs e) { for (int i = 0; i < tbxnumber.Text.Length - 1; i++) { if (list1.Items[i] == tbxnumber.Text[i]) return; object list = tbxnumber.Text[i]; list1.Items.Add(list); } } but it does not work

      H Offline
      H Offline
      Harvey Saayman
      wrote on last edited by
      #2

      Here you go

      private void InsertListBoxItem(string NewItem)
      {
      if (!listBox1.Items.Contains(NewItem))
      {
      listBox1.Items.Add(NewItem);
      }
      }

      Hope it helps :)

      Harvey Saayman - South Africa Software Developer .Net, C#, SQL you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

      1 Reply Last reply
      0
      • T tanweer

        hi i m inserting some vlues into a listbox but now i want to restrict the duplication of entries in the list box private void lblpush_Click(object sender, EventArgs e) { for (int i = 0; i < tbxnumber.Text.Length - 1; i++) { if (list1.Items[i] == tbxnumber.Text[i]) return; object list = tbxnumber.Text[i]; list1.Items.Add(list); } } but it does not work

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        No, it won't. Problems: 1) You are looping round exactly as many times as there are characters in the textbox, rather than the number of times there are items in the list. 2) You are comparing the list item with a single character. Try: 1) using the codeblock widget to preserve your formatting when you post code fragments - it make it easier to read, and thus understand. 2) using foreach(ListBoxItem lbi in list1) instead of a for loop. 3) using ListBox.Contains instead of the loop at all!

        All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups