Click here to Skip to main content
15,066,928 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am a newbie trying to validate user input in c# with code below. I want the label to display a message if the name is already in the list.Someone help please.

namespace Tounament
{
    public partial class Form1 : Form
    {
       
       
        List<String> teams;
        

        public Form1()
        {

            // Create lists
            
            players = new List<String> { };
            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";
            }

            if (rdo_Team1.Checked == true && (newteamname.Length > 0))
            {
                teams.Add(newteamname + ", Team 1");
                lst_Teams.DataSource = null;
                lst_Teams.DataSource = teams;
            }
            
            else
            {
                lbl_TeamStatus.Text = "You must check a team box";
                return;
            }
            txt_TeamName.Text = String.Empty;


What I have tried:

The first part work but cant figure out why this part doesn't work
Posted
Updated 24-Jun-21 8:52am
v3

Look at your code:
C#
if (newteamname.Length == 0) return;
if (teams.Contains(newplayer))
    {
    lbl_TeamStatus.Text = newteamname + " already exists";
    }

if (rdo_Team1.Checked == true && (newteamname.Length > 0))
    {
    teams.Add(newteamname + ", Team 1");
    ...
    }
 else
    ...

If you find that it exists, you go ahead and add it anyway!
So ... add a return statement after the error message is displayed ...
   
Comments
Donbrookie 24-Jun-21 8:04am
   
Thanks originalGriff. I have added a return statement as suggested but still not able to identify if a name already exists in the list.
As OriginalGriff mentioned: "add a return statement after the error message is displayed ... "

C#
if (teams.Contains(newplayer))
{
    lbl_TeamStatus.Text = newteamname + " already exists";
    return; //<- here!!!
}
   
Comments
Donbrookie 25-Jun-21 3:08am
   
Thanks Maciej Los I have done exactly as suggested but for some reason I can still enter the same input twice into the list. The validation is not working
Maciej Los 25-Jun-21 3:29am
   
There must be an error in your code (which hasn't posted). So, improve your question and provide actual code to give us a chance to help you.
Donbrookie 25-Jun-21 6:04am
   
Thanks Maciej Los. This is the whole below, The add player to list part works but the Add team player part doesn't.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tournament
{
public partial class Form1 : Form
{

List<string> players;
List<string> teams;


public Form1()
{

InitializeComponent();

// Create lists

players = new List<string> { };
teams = new List<string> { };

}


//Add player to list
private void btn_Submit1_Click(object sender, EventArgs e)
{
string newplayer = txt_Name.Text.Trim();
if (newplayer.Length == 0) return;
if(players.Contains(newplayer))
{
lbl_Status.Text = newplayer + " already exists";
}
else
{
players.Add(newplayer);

lst_Players.DataSource = null;
lst_Players.DataSource = players;
lbl_Status.Text = newplayer + " has been added";
}
txt_Name.Text = String.Empty;
}
//Add team player to list
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;
}
else
{
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;
}
}

private void Form1_Load(object sender, EventArgs e)
{

}
Maciej Los 25-Jun-21 7:04am
   
As i mentioned, you should improve question. Do not post such of content in comment.
Donbrookie 25-Jun-21 7:36am
   
OK Maciej Los.
Its a windows form application

I have have created 2 lists. One for players and the other for Team players.

I am able to check in the players list if a name I am adding doesn't already exist in the list. That works fine.

My problem is I cant check if a name I am adding to the team players list already exist and display a message "Player already exists" like I did the players list above.

Is my explanation better now?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900