Ejemplo n.º 1
0
        // read textfiles data into table.
        private void readFile()
        {
            try
            {
                // read txt files into list
                lines          = File.ReadAllLines(cPath.pathPlayers, Encoding.Default).ToList();
                listRole       = File.ReadAllLines(cPath.pathRole, Encoding.Default).ToList();
                listBatting    = File.ReadAllLines(cPath.pathBatting, Encoding.Default).ToList();
                listBowling    = File.ReadAllLines(cPath.pathBowling, Encoding.Default).ToList();
                listCountry    = File.ReadAllLines(cPath.pathCountry, Encoding.Default).ToList();
                listSignedYear = File.ReadAllLines(cPath.pathSignYear, Encoding.Default).ToList();

                //split data from txt files
                //
                foreach (string line in lines)
                {
                    int countSemiColumn = line.Split(';').Length - 1;

                    if (countSemiColumn > 8)
                    {
                        //skip error
                        continue;
                    }
                    else
                    {
                        //store data into string from line after spliting
                        string[] entries = line.Split(';');

                        //instance of cPlayer class
                        cPlayers player = new cPlayers();

                        switch (entries[3].Trim())
                        {
                        case "0":
                            player.role = listRole[0];
                            break;

                        case "1":
                            player.role = listRole[1];
                            break;

                        case "2":
                            player.role = listRole[2];
                            break;

                        case "3":
                            player.role = listRole[3];
                            break;
                        }

                        switch (entries[4].Trim())
                        {
                        case "0":
                            player.battiing = listBatting[0];
                            break;

                        case "1":
                            player.battiing = listBatting[1];
                            break;
                        }

                        switch (entries[5].Trim())
                        {
                        case "0":
                            player.bowling = listBowling[0];
                            break;

                        case "1":
                            player.bowling = listBowling[1];
                            break;

                        case "2":
                            player.bowling = listBowling[2];
                            break;

                        case "3":
                            player.bowling = listBowling[3];
                            break;

                        case "4":
                            player.bowling = listBowling[4];
                            break;

                        case "5":
                            player.bowling = listBowling[5];
                            break;

                        case "6":
                            player.bowling = listBowling[6];
                            break;

                        case "7":
                            player.bowling = listBowling[7];
                            break;

                        case "8":
                            player.bowling = listBowling[8];
                            break;
                        }

                        switch (entries[6].Trim())
                        {
                        case "0":
                            player.signedYear = listSignedYear[0];
                            break;

                        case "1":
                            player.signedYear = listSignedYear[1];
                            break;

                        case "2":
                            player.signedYear = listSignedYear[2];
                            break;

                        case "3":
                            player.signedYear = listSignedYear[3];
                            break;

                        case "4":
                            player.signedYear = listSignedYear[4];
                            break;

                        case "5":
                            player.signedYear = listSignedYear[5];
                            break;

                        case "6":
                            player.signedYear = listSignedYear[6];
                            break;

                        case "7":
                            player.signedYear = listSignedYear[7];
                            break;

                        case "8":
                            player.signedYear = listSignedYear[8];
                            break;

                        case "9":
                            player.signedYear = listSignedYear[9];
                            break;

                        case "10":
                            player.signedYear = listSignedYear[10];
                            break;

                        case "11":
                            player.signedYear = listSignedYear[11];
                            break;

                        case "12":
                            player.signedYear = listSignedYear[12];
                            break;
                        }

                        switch (entries[8].Trim())
                        {
                        case "0":
                            player.country = listCountry[0];
                            break;

                        case "1":
                            player.country = listCountry[1];
                            break;

                        case "2":
                            player.country = listCountry[2];
                            break;

                        case "3":
                            player.country = listCountry[3];
                            break;

                        case "4":
                            player.country = listCountry[4];
                            break;

                        case "5":
                            player.country = listCountry[5];
                            break;

                        case "6":
                            player.country = listCountry[6];
                            break;

                        case "7":
                            player.country = listCountry[7];
                            break;

                        case "8":
                            player.country = listCountry[8];
                            break;

                        case "9":
                            player.country = listCountry[9];
                            break;

                        case "10":
                            player.country = listCountry[10];
                            break;
                        }

                        player.name   = entries[0].Trim();
                        player.age    = entries[1].Trim();
                        player.number = entries[2].Trim();
                        player.salary = entries[7].Trim();


                        listPlayers.Add(player);
                    }

                    //read data into list view from listPlayer
                    foreach (cPlayers play in listPlayers)
                    {
                        myFrm.listView1.Items.Add(new ListViewItem(new string[] { play.name, play.age, play.number, play.role, play.battiing, play.bowling, play.signedYear, play.salary, play.country }));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
Ejemplo n.º 2
0
        private void btnCreate_Click(object sender, EventArgs e)
        {
            //checking file exist or not
            //if not then it will create it
            cTextFile verifyTxtFile = new cTextFile();

            verifyTxtFile.fileExist();


            validation();


            try
            {
                //logics of validation

                if (val == true)
                {
                    if (MessageBox.Show("Do you Want to Add this Player", "Add Profile", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        //obj reference of cPlayer
                        cPlayers player = new cPlayers();


                        //reading text and cmbbox value
                        player.name       = txtName.Text;
                        player.age        = txtAge.Text;
                        player.number     = txtNumber.Text;
                        player.role       = cmbRole.SelectedItem.ToString();
                        player.battiing   = cmbBatting.SelectedItem.ToString();
                        player.bowling    = cmbBowling.SelectedItem.ToString();
                        player.signedYear = cmbSignedYear.SelectedItem.ToString();
                        player.salary     = txtSalary.Text;
                        player.country    = cmbCountry.SelectedItem.ToString();


                        //STORING INDEX NUMBER INTO ARRAY FOR TEXTFILE.
                        string[] entries = new string[9];

                        entries[0] = player.name;
                        entries[1] = player.age;
                        entries[2] = player.number;
                        entries[3] = cmbRole.SelectedIndex.ToString();       //selecting index of combo box to add in text file.
                        entries[4] = cmbBatting.SelectedIndex.ToString();    //selecting index of combo box to add in text file.
                        entries[5] = cmbBowling.SelectedIndex.ToString();    //selecting index of combo box to add in text file.
                        entries[6] = cmbSignedYear.SelectedIndex.ToString(); //selecting index of combo box to add in text file.
                        entries[7] = player.salary;
                        entries[8] = cmbCountry.SelectedIndex.ToString();    //selecting index of combo box to add in text file.

                        //Write into Text file
                        StreamWriter sw = new StreamWriter(cPath.pathPlayers, true);
                        sw.WriteLine(entries[0] + ";" + entries[1] + ";" + entries[2] + ";" + entries[3] + ";" + entries[4] + ";" + entries[5] + ";" + entries[6] + ";" + entries[7] + ";" + entries[8]);
                        sw.Close();
                        MessageBox.Show("New Player has been Added Successfully!!!.", "Player Added", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

                        //Write into list view
                        myFrm.listView1.Items.Add(new ListViewItem(new string[] { player.name, player.age, player.number, player.role, player.battiing, player.bowling, player.signedYear, player.salary, player.country }));

                        txtAge.Clear();
                        txtName.Clear();
                        txtNumber.Clear();
                        txtSalary.Clear();
                        cmbBatting.Items.Clear();
                        cmbBowling.Items.Clear();
                        cmbCountry.Items.Clear();
                        cmbRole.Items.Clear();
                        cmbSignedYear.Items.Clear();
                    }
                }
            }
            catch (Exception ex)
            {
                err = ex.ToString();
            }
        }