简体   繁体   English

如何从我的阵列列表中删除玩家

[英]How can I delete a player from my array list

I am making a database program but not using SQL i am making a 5-a-side football team and have 2 array lists one for player details and one for match details.我正在制作一个数据库程序,但不使用 SQL 我正在制作一个 5 人制足球队,并有 2 个数组列表,一个用于球员详细信息,一个用于比赛详细信息。 I know how to add a new player but i cant seem to delete a player and was wondering if anybody could help.我知道如何添加新玩家,但我似乎无法删除玩家,想知道是否有人可以提供帮助。

        FootballClub PlayerMemberData;

        PlayerMemberData = new FootballClub(0);
        PlayerMemberData.Firstname = "Daniel";
        PlayerMemberData.Surname = "Whitcombe";
        PlayerMemberData.age = 19;
        PlayerMemberData.Address1 = "7 High Street";
        PlayerMemberData.Address2 = "Trelewis";
        PlayerMemberData.City = "Treharris";
        PlayerMemberData.Country = "Mid Glam";
        PlayerMemberData.Postcode = "CF46 6AB";
        PlayerMemberData.HomeNum = 0;
        PlayerMemberData.MobNum = 0;
        PlayerMemberData.Email = "dan@hotmail.co.uk";
        PlayerMemberData.EmergencyName = "mam";
        PlayerMemberData.EmergencyNum = 424242;

        ClubData.Add(PlayerMemberData);

        PlayerMemberData = new FootballClub(0);
        PlayerMemberData.Firstname = "EFEF";
        PlayerMemberData.Surname = "WhitEWFEWFcombe";
        PlayerMemberData.age = 19;
        PlayerMemberData.Address1 = "7 High FEWF";
        PlayerMemberData.Address2 = "FWEFF";
        PlayerMemberData.City = "FWEFEWF";
        PlayerMemberData.Country = "Mid Glam";
        PlayerMemberData.Postcode = "CF46 3VD";
        PlayerMemberData.HomeNum = 0;
        PlayerMemberData.MobNum = 0;
        PlayerMemberData.Email = "FWEFEW@hotmail.co.uk";
        PlayerMemberData.EmergencyName = "Mam";
        PlayerMemberData.EmergencyNum = 424242;

        ClubData.Add(PlayerMemberData);

        MatchPreformance PlayerPreformanceData;

        PlayerPreformanceData = new MatchPreformance(0);
        PlayerPreformanceData.MinutesPlayed = 32;
        PlayerPreformanceData.Goals = 1;
        PlayerPreformanceData.Penalties = 0;
        PlayerPreformanceData.CardGiven = 'R';

        MatchData.Add(PlayerPreformanceData);

        PlayerPreformanceData = new MatchPreformance(0);
        PlayerPreformanceData.MinutesPlayed = 32;
        PlayerPreformanceData.Goals = 1;
        PlayerPreformanceData.Penalties = 0;
        PlayerPreformanceData.CardGiven = 'Y';

        MatchData.Add(PlayerPreformanceData);

        ShowCurrentMember();
    }

    #region Variables
    FootballClub PlayerMemberData = new FootballClub(0);
    MatchPreformance PlayerPreformanceData = new MatchPreformance(1);
    ArrayList ClubData = new ArrayList();
    ArrayList MatchData = new ArrayList();
    public int CurrentPlayerShown = 0;
    // Path to the file for loading/saving. Note the '@' sign in front of the string. Without it all '\' have to be double '\\'
    string filePathName = @"C:\Users\Clare\Desktop\Football_Data.txt";
    #endregion

    #region DataStructure
    // ==============================The Data Structure=====================================
    [Serializable]
    private struct FootballClub
    {
        //the data types making up the struct
        public string Firstname;
        public string Surname;
        public int age;
        public string Address1;
        public string Address2;
        public string City;
        public string Country;
        public string Postcode;
        public int HomeNum;
        public int MobNum;
        public string Email;
        public string EmergencyName;
        public int EmergencyNum;

        //the "constructor". Fills data structure with default vaules 
        public FootballClub(int x)
        {
            Firstname = "";
            Surname = "";
            age = 0;
            Address1 = "";
            Address2 = "";
            City = "";
            Country = "";
            Postcode = "";
            HomeNum = 0;
            MobNum = 0;
            Email = "";
            EmergencyName = "";
            EmergencyNum = 0;
        }
    }
    [Serializable]
    private struct MatchPreformance
    {
        public int MinutesPlayed;
        public int Goals;
        public int Penalties;
        public char CardGiven;
        public int TotalTime;
        public int TotalGoals;
        public int TotalPenalties;

        public MatchPreformance(int x)
        {
            MinutesPlayed = 0;
            Goals = 0;
            Penalties = 0;
            CardGiven = ' ';
            TotalTime = 0;
            TotalGoals = 0;
            TotalPenalties = 0;
        }
    }
    // ==============================End of Data Structure=====================================
    #endregion

    #region MatchDetails
    private void NewMatch_Butt_Click(object sender, EventArgs e)
    {
        //create a new TabPage
        TabPage newTP = new TabPage();
        //add it to the TabControl
        tabControl1.TabPages.Add(newTP);
        //set the title to 'Match'plus the tab number
        int TabPageNumber = tabControl1.SelectedIndex+1; //adds 1 from the current tab
        tabControl1.TabPages[TabPageNumber].Text = "Match " + (TabPageNumber+1);
        //make it the selected tab
        tabControl1.SelectTab(TabPageNumber);
        DeleteMatch_Butt.Enabled = true;// now that we have something to delete we can enable this button
        //make the new tab page the parent of the panel that holds all the controls
        TabPanel.Parent = tabControl1.SelectedTab; //
    }

    private void DeleteMatch_Butt_Click(object sender, EventArgs e)
    {
        // Removes the selected tab:
        tabControl1.TabPages.Remove(tabControl1.SelectedTab);
        //disable button if only one tabpage left
        if (tabControl1.SelectedIndex < 1)
        {
            DeleteMatch_Butt.Enabled = false;
        }
    }

    private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
    {
        TabPanel.Parent = tabControl1.SelectedTab;
    }

    private void PrevPlayer_Butt_Click(object sender, EventArgs e)
    {
        --CurrentPlayerShown;
        ShowCurrentMember();
        UpdatePrevNextBtnStatus();
    }

    private void NextPlayer_Butt_Click(object sender, EventArgs e)
    {
        ++CurrentPlayerShown;
        ShowCurrentMember();
        UpdatePrevNextBtnStatus();
    }

    private void MinutesPlayed_Num_ValueChanged(object sender, EventArgs e)
    {
        MatchPreformance PlayerPreformanceData = new MatchPreformance(0);
        PlayerPreformanceData = (MatchPreformance)MatchData[CurrentPlayerShown];
        PlayerPreformanceData.MinutesPlayed = (int)MinutesPlayed_Num.Value;
        MatchData[CurrentPlayerShown] = PlayerPreformanceData;
    }

    private void GoalsScored_Num_ValueChanged(object sender, EventArgs e)
    {
        MatchPreformance PlayerPreformanceData = new MatchPreformance(0);
        PlayerPreformanceData = (MatchPreformance)MatchData[CurrentPlayerShown];
        PlayerPreformanceData.Goals = (int)GoalsScored_Num.Value;
        MatchData[CurrentPlayerShown] = PlayerPreformanceData;
    }

    private void PenaltiesScored_Num_ValueChanged(object sender, EventArgs e)
    {
        MatchPreformance PlayerPreformanceData = new MatchPreformance(0);
        PlayerPreformanceData = (MatchPreformance)MatchData[CurrentPlayerShown];
        PlayerPreformanceData.Penalties = (int)PenaltiesScored_Num.Value;
        MatchData[CurrentPlayerShown] = PlayerPreformanceData;
    }

    private void YellowCard_RadioButt_CheckedChanged(object sender, EventArgs e)
    {
        MatchPreformance PlayerPreformanceData = new MatchPreformance(0);
        PlayerPreformanceData = (MatchPreformance)MatchData[CurrentPlayerShown];
        PlayerPreformanceData.CardGiven = 'Y';
        MatchData[CurrentPlayerShown] = PlayerPreformanceData;
    }

    private void RedCard_RadioButt_CheckedChanged(object sender, EventArgs e)
    {
        MatchPreformance PlayerPreformanceData = new MatchPreformance(0);
        PlayerPreformanceData = (MatchPreformance)MatchData[CurrentPlayerShown];
        PlayerPreformanceData.CardGiven = 'R';
        MatchData[CurrentPlayerShown] = PlayerPreformanceData;
    }
    #endregion

    #region PlayerDetails
    private void FirstNameTextBox_TextChanged(object sender, EventArgs e)
    {
        //1. Create a new empty structure to hold member data
        FootballClub PlayerMemberData = new FootballClub(0);
        //2. Copy content of the current entry into the new empty structure
        PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
        //3. Change only the name currently shown in the GUI in the structure
        PlayerMemberData.Firstname = FirstNameTextBox.Text;
        //4. overwrite the old data with the changed one.
        ClubData[CurrentPlayerShown] = PlayerMemberData;
    }

    private void SurnameTextBox_TextChanged(object sender, EventArgs e)
    {
        FootballClub PlayerMemberData = new FootballClub(0);
        PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
        PlayerMemberData.Surname = SurnameTextBox.Text;
        ClubData[CurrentPlayerShown] = PlayerMemberData;
    }

    private void PlayerAge_ValueChanged(object sender, EventArgs e)
    {
        FootballClub PlayerMemberData = new FootballClub(0);
        PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
        PlayerMemberData.age = (int)PlayerAge.Value;
        ClubData[CurrentPlayerShown] = PlayerMemberData;
    }

    private void Address1_Text_TextChanged(object sender, EventArgs e)
    {
        FootballClub PlayerMemberData = new FootballClub(0);
        PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
        PlayerMemberData.Address1 = Address1_Text.Text;
        ClubData[CurrentPlayerShown] = PlayerMemberData;
    }

    private void Address2_Text_TextChanged(object sender, EventArgs e)
    {
        FootballClub PlayerMemberData = new FootballClub(0);
        PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
        PlayerMemberData.Address2 = Address2_Text.Text;
        ClubData[CurrentPlayerShown] = PlayerMemberData;
    }

    private void City_Text_TextChanged(object sender, EventArgs e)
    {
        FootballClub PlayerMemberData = new FootballClub(0);
        PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
        PlayerMemberData.City = City_Text.Text;
        ClubData[CurrentPlayerShown] = PlayerMemberData;
    }

    private void Country_Text_TextChanged(object sender, EventArgs e)
    {
        FootballClub PlayerMemberData = new FootballClub(0);
        PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
        PlayerMemberData.Country = Country_Text.Text;
        ClubData[CurrentPlayerShown] = PlayerMemberData;
    }

    private void Postcode_Text_TextChanged(object sender, EventArgs e)
    {
        FootballClub PlayerMemberData = new FootballClub(0);
        PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
        PlayerMemberData.Postcode = Postcode_Text.Text;
        ClubData[CurrentPlayerShown] = PlayerMemberData;
    }

    private void HomeNumber_Text_TextChanged(object sender, EventArgs e)
    {
        int HomeNum;
        FootballClub PlayerMemberData = new FootballClub(0);
        PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
        HomeNum = Convert.ToInt32(HomeNumber_Text.Text);
        PlayerMemberData.HomeNum = HomeNum;
        ClubData[CurrentPlayerShown] = PlayerMemberData;
    }

    private void MobileNum_Text_TextChanged(object sender, EventArgs e)
    {
        int MobNum;
        FootballClub PlayerMemberData = new FootballClub(0);
        PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
        MobNum = Convert.ToInt32(MobileNum_Text.Text);
        PlayerMemberData.HomeNum = MobNum;
        ClubData[CurrentPlayerShown] = PlayerMemberData;
    }

    private void Email_Text_TextChanged(object sender, EventArgs e)
    {
        FootballClub PlayerMemberData = new FootballClub(0);
        PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
        PlayerMemberData.Email = Email_Text.Text;
        ClubData[CurrentPlayerShown] = PlayerMemberData;
    }

    private void EmergName_Text_TextChanged(object sender, EventArgs e)
    {
        FootballClub PlayerMemberData = new FootballClub(0);
        PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
        PlayerMemberData.EmergencyName = EmergName_Text.Text;
        ClubData[CurrentPlayerShown] = PlayerMemberData;
    }

    private void EmergNum_Text_TextChanged(object sender, EventArgs e)
    {
        int EmergNumber;
        FootballClub PlayerMemberData = new FootballClub(0);
        PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
        EmergNumber = Convert.ToInt32(EmergNum_Text.Text);
        PlayerMemberData.EmergencyNum = EmergNumber;
        ClubData[CurrentPlayerShown] = PlayerMemberData;
    }
    #endregion

    #region ShowMember

    private void ShowCurrentMember()
    {
        FootballClub PlayerMemberData;
        PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];

        FirstNameTextBox.Text = PlayerMemberData.Firstname;
        SurnameTextBox.Text = PlayerMemberData.Surname;
        PlayerAge.Value = PlayerMemberData.age;
        Address1_Text.Text = PlayerMemberData.Address1;
        Address2_Text.Text = PlayerMemberData.Address2;
        City_Text.Text = PlayerMemberData.City;
        Country_Text.Text = PlayerMemberData.Country;
        Postcode_Text.Text = PlayerMemberData.Postcode;
        HomeNumber_Text.Text = " " + PlayerMemberData.HomeNum;
        MobileNum_Text.Text = " " + PlayerMemberData.MobNum;
        Email_Text.Text = PlayerMemberData.Email;
        EmergName_Text.Text = PlayerMemberData.EmergencyName;
        EmergNum_Text.Text = " " + PlayerMemberData.EmergencyNum;

        MatchPreformance PlayerPreformanceData;
        PlayerPreformanceData = (MatchPreformance)MatchData[CurrentPlayerShown];

        MinutesPlayed_Num.Value = PlayerPreformanceData.MinutesPlayed;
        GoalsScored_Num.Value = PlayerPreformanceData.Goals;
        PenaltiesScored_Num.Value = PlayerPreformanceData.Penalties;
        if (PlayerPreformanceData.CardGiven == 'Y') YellowCard_RadioButt.Checked = true;
        else RedCard_RadioButt.Checked = true;
        TotalTime_Text.Text = " " + PlayerPreformanceData.MinutesPlayed;
        TotalGoals_Text.Text = " " + PlayerPreformanceData.Goals;
        TotalPenalties_Text.Text = " " + PlayerPreformanceData.Penalties;

        Player1.Text = "player  " + (CurrentPlayerShown + 1);
        UpdatePrevNextBtnStatus();
    }

    private void UpdatePrevNextBtnStatus()
    {
        if (CurrentPlayerShown <= 0) PrevPlayer_Butt.Enabled = false;
        else PrevPlayer_Butt.Enabled = true;

        if (CurrentPlayerShown >= (ClubData.Count - 1)) NextPlayer_Butt.Enabled = false;
        else NextPlayer_Butt.Enabled = true;
    }
    #endregion

    #region Clear

    private void Clear_Butt_Click(object sender, EventArgs e)
    {
        ClearData();
    }

    private void ClearData()
    {
        //clear the data struct 
        FootballClub PlayerMemberData = new FootballClub(0);
        PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
        PlayerMemberData.Firstname = "";
        PlayerMemberData.Surname = "";
        PlayerMemberData.age = 0;
        PlayerMemberData.Address1 = "";
        PlayerMemberData.Address2 = "";
        PlayerMemberData.City = "";
        PlayerMemberData.Country = "";
        PlayerMemberData.Postcode = "";
        PlayerMemberData.HomeNum = 0;
        PlayerMemberData.MobNum = 0;
        PlayerMemberData.Email = "";
        PlayerMemberData.EmergencyName = "";
        PlayerMemberData.EmergencyNum = 0;

        ClubData[CurrentPlayerShown] = PlayerMemberData;

        MatchPreformance PlayerPreformanceData = new MatchPreformance(1);
        PlayerPreformanceData = (MatchPreformance)MatchData[CurrentPlayerShown];
        PlayerPreformanceData.MinutesPlayed = 0;
        PlayerPreformanceData.Goals = 0;
        PlayerPreformanceData.Penalties = 0;
        PlayerPreformanceData.CardGiven = ' ';

        MatchData[CurrentPlayerShown] = PlayerPreformanceData;

        ShowCurrentMember();
    }
    #endregion

    #region Save
    private void SaveData()
    {
        // CREATE a file for saving (technically a 'FileStream')
        FileStream fs = new FileStream(filePathName, FileMode.Create);

        // Construct a BinaryFormatter and use it to serialise the data to the stream.
        // this "serialises" the ArrayList into binary (i.e. not to human readable text file)
        BinaryFormatter binFormatter = new BinaryFormatter();

        // Write the file to disk
        binFormatter.Serialize(fs, ClubData);

        // Close the file
        fs.Close();
    } // end of Save method
    #endregion

    #region Load
    private void LoadData()
    {
        // OPEN file for loading
        FileStream fs = new FileStream(filePathName, FileMode.Open);

        // Construct a BinaryFormatter for de-serialisation of data from the stream.
        BinaryFormatter binFormatter = new BinaryFormatter();

        // Read the file from disk
        ClubData = (ArrayList)binFormatter.Deserialize(fs);

        // Close the file
        fs.Close();

        CurrentPlayerShown = 0;
        ShowCurrentMember();

    }
    #endregion

    private void NewPlayer_Butt_Click(object sender, EventArgs e)
    {
        FootballClub PlayerMemberData;

        PlayerMemberData = new FootballClub(1);
        PlayerMemberData.Firstname = "";
        PlayerMemberData.Surname = "";
        PlayerMemberData.age = 0;
        PlayerMemberData.Address1 = "";
        PlayerMemberData.Address2 = "";
        PlayerMemberData.City = "";
        PlayerMemberData.Country = "";
        PlayerMemberData.Postcode = "";
        PlayerMemberData.HomeNum = 0;
        PlayerMemberData.MobNum = 0;
        PlayerMemberData.Email = "";
        PlayerMemberData.EmergencyName = "";
        PlayerMemberData.EmergencyNum = 0;

        ClubData.Add(PlayerMemberData);

        MatchPreformance PlayerPreformanceData;

        PlayerPreformanceData = new MatchPreformance(1);
        PlayerPreformanceData.MinutesPlayed = 0;
        PlayerPreformanceData.Goals = 0;
        PlayerPreformanceData.Penalties = 0;
        PlayerPreformanceData.CardGiven = ' ';

        MatchData.Add(PlayerPreformanceData);

        ShowCurrentMember();
    }

    private void DeletePlayer_Butt_Click(object sender, EventArgs e)
    {

    }




}

} ` }`

You can use syntax.您可以使用语法。

for (int i = 0; i < ClubData.Count; i++)
    {
        if (/* Some condition that matches the potential delete */){
          ClubData.RemoveAt(i);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM