Home > DeveloperSection > Forums > Compare input to list
Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    December-17-2013 1:03 AM

 C# C# 
Ratings:


 1 Reply(s)
 608  View(s)
Rate this:

Is it possible to compare the input text in a textbox in a windows form to the items in a list and then save it if it doesn't exist there. 

public Form1()

{

    InitializeComponent();

    if (txtId.Text == Id.myList)

    {

        myList.Add(txtId.Text);

    }

    else (){

        MessageBox.Show("Id already exists");

    }

}



Pravesh Singh

Total Post:411

Points:2881
Posted on    December-17-2013 1:35 AM

Hi Manoj,

You can loop trough all the items in the list and create a bool variable to check whether you have found the value in the list.

bool _Found = false;

foreach (string a in list)

{

    if (a == textBox1.Text)

    {

        _Found = true;

        break;

    }

    else

        _Found = false;

}

if (_Found) { }

else { list.Add(textBox1.Text) }

Or use List(T).Contains Method

if (list.Contains(textBox1.Text)) { }


Don't want to miss updates? Please click the below button!

Follow MindStick