0

Am a newbie in C#. I have have created a list players with 2 radio button options for two teams.

Am able to add user input to the list but cant check if a name already exists in the list. Am using code below:

using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
  namespace teamVal
         {
             public partial class Form1 : Form
              {
                   List<String> teams;
                  public Form1()
                  {
                      InitializeComponent();
                       // Create lists
                       teams = new List<String> { };
                   }

        private void btn_Submit2_Click(object sender, EventArgs e)
        {
            string newteamname = txt_TeamName.Text.Trim();
            if (newteamname.Length == 0) return;

            if (teams.Contains(newteamname))
            {
                lbl_TeamStatus.Text = newteamname + " already exists";
                return;
            }
            
            if (rdo_Team1.Checked == true && (newteamname.Length > 0))

            {
                teams.Add(newteamname + ", Team 1");
                lst_Teams.DataSource = null;
                lst_Teams.DataSource = teams;
            }
            else if (rdo_Team2.Checked == true && (newteamname.Length > 0))
            {
                teams.Add(newteamname + ", Team 2");
                lst_Teams.DataSource = null;
                lst_Teams.DataSource = teams;
            }
            else
            {
                lbl_TeamStatus.Text = "You must check a team box";
                return;
            }
            txt_TeamName.Text = String.Empty;
        }
    }
}
2
  • 1
    please edit your question to fix the format - and check the preview before posting. also: welcome to stackoverflow. i recommend taking the tour, as well as reading how to ask a good question and what's on topic. Commented Jun 25, 2021 at 13:39
  • From your code example it looks like you are appending ", Team 1" or ", Team 2" to the team name and then inserting this into the team list. Your check for whether a team exists in the list does not take this append into account. I would suggest storing which team they are as a different variable. Commented Jun 25, 2021 at 14:49

1 Answer 1

1

I think the if condition is wrong:

if (teams.Contains(newteamname)){ }

It can be as follows:

if (teams?.Where(x => x.Contains(newteamname)).Any() == true){ }

You are also appending the radio button text to string and then adding it to the list, which is why your condition is not working.

Sign up to request clarification or add additional context in comments.

1 Comment

Genius ! That worked. I couldnt have figured that out in a million years.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.