I have a button that stores strings in an array but I can click it several times and add the same value to it. I just want to know how to code that when I press the button it will prompt a message and don't add the strings to the array. Please help me. Thank you.
1
Expert's answer
2012-10-24T09:49:10-0400
To do that you need to check if your array already contains inputted string. If it does – you should show a message and wait to another string to be inputted, other way you should add it to your array. Here is an example in C#. First of all you should create an array where all your strings will be saved.& Class List is a powerful implementation of a dynamic array. It represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists. & private static List<string> myDynamArray = new List<string>() ;
Now you should enter some sting in a field (textbox). I called it MyTextBox. After you click the button (Add_btn) the code below will be done:
if (myDynamArray.Contains(MyTextBox.Text)) // check, if our array contains inputted string
{ MessageBox.Show("Array already contains this string (" + MyTextBox.Text + ")"); // if it does, we show& the message } else { myDynamArray.Add(MyTextBox.Text); //if it don’t – we add inputed string to the array }
Comments
Leave a comment