Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8075781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:06:12+00:00 2026-06-05T15:06:12+00:00

Bassicly im creating a program that reads information from an xml file into a

  • 0

Bassicly im creating a program that reads information from an xml file into a lisbox and allows the user to transfer items in the list box to another listBox.

But i want to some how disallow multiple items from being imported from one listBox to the other. I thought i can somehow do an experession to check if the String already Exists in the listBox.

The reason i want to do this is because the user can click x amount of times in order to import items and it’s unproffesional.

Any Help would be appreciated thank you.

private void button1_Click(object sender, EventArgs e)
{
    if (!listBox.Items.Exists) // Random Idea which doesnt work
    {
        listBox2.Items.Add(listBox1.Items[listBox1.SelectedIndex]);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:06:14+00:00Added an answer on June 5, 2026 at 3:06 pm
    private void button1_Click(object sender, EventArgs e)
    {
        if (!listBox.Items.Exists) // Random Idea which doesnt work
        {
        listBox2.Items.Add(listBox1.Items[listBox1.SelectedIndex]);
        }
    
    }
    

    That will work actually, but you need to use the Contains method. However, you may be missing one crucial point.

    What type of items are you using to populate your ListBox? Exists will call .Equals which, by default, uses reference equality. So, if you need to filter based on value, you need to override .Equals for your type and change the semantics.

    For example:

    class Foo 
    { 
        public string Name { get; set; }
        public Foo(string name)
        {
            Name = name;
        }
    }
    
    class Program
    {
        static void Main( string[] args )
        {
            var x = new Foo("ed");
            var y = new Foo("ed");
            Console.WriteLine(x.Equals(y));  // prints "False"
        }       
    }
    

    However, if we override .Equals to provide value type semantics…

    class Foo 
    { 
        public string Name { get; set; }
        public Foo(string name)
        {
            Name = name;
        }
    
        public override bool Equals(object obj)
        {
            // error and type checking go here!
            return ((Foo)obj).Name == this.Name;
        }
    
        // should override GetHashCode as well
    }
    
    class Program
    {
        static void Main( string[] args )
        {
            var x = new Foo("ed");
            var y = new Foo("ed");
            Console.WriteLine(x.Equals(y));  // prints "True"
            Console.Read();
        }       
    }
    

    And now your call to if(!listBox.Items.Contains(item)) will work as you intended it to. However, if you wish it to continue working you will need to add the item to both listboxes, not just listBox2.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Bassicly im creating a program that allows the user to enter values and if
I want to make a program that concatenates an exe and data into one
Bassicly im creating a schedule where the user can input his on time such
i am creating rss reader application with php.it is basicly getting rss url's from
Hey I have some problems creating my query into doctrine: My sql query looks
I basicly need to load the whole XML file, add a new line with
Bassicly i want the user to input 4 and then display the next question
I basicly need to be able to restrict the items that can be appended
I have a function creating a file for download like so: header(Content-type: application/octet-stream); header(Content-disposition:
Bassicly my aim is too extend a activity so when the user presses Next

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.