kulpot / STUDY.VS2022.CSharp.HeroMaker

C# avatar maker app 01 How to use checkboxes tutorial
1 stars 0 forks source link

C# avatar maker app 16 delete a list and reset #3

Open kulpot opened 8 months ago

kulpot commented 8 months ago

###################### Form2.cs ######################################

using C__heromaker_10; using C__avatar_maker_app_12; 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 C__avatar_maker_app_13 { public partial class Form2 : Form { //-----START------- C# avatar maker app 15 sort a list of objects tutorial ----------------------------------------- BindingSource bs = new BindingSource(); //-----END------- C# avatar maker app 15 sort a list of objects tutorial -----------------------------------------

    public Form2()
    {
        InitializeComponent();
        //-----START------- C# avatar maker app 13 use a static class to store a list of objects -------------------------------
        //BindingSource bs = new BindingSource(); // move above Form() constructor
        bs.DataSource = HeroList.hallOfFame;
        listBox1.DataSource = bs;
        listBox1.DisplayMember = "Name";    //ErrorSolutionFrom:Hero class needs a ToString() method or ListBox1 needs a DisplayMember property set.
        //-----END------- C# avatar maker app 13 use a static class to store a list of objects -------------------------------

    }

    //-----START------- C# avatar maker app 15 sort a list of objects tutorial -----------------------------------------
    private void btn_sortAZ_Click(object sender, EventArgs e)
    {
        HeroList.hallOfFame.Sort();     //ERROR: The hero class should have the CompareTo method
        bs.ResetBindings(false);
    }

    private void btn_sortZA_Click(object sender, EventArgs e)
    {
        HeroList.hallOfFame.Sort();
        HeroList.hallOfFame.Reverse();
        bs.ResetBindings(false);
    }
    //-----END------- C# avatar maker app 15 sort a list of objects tutorial -----------------------------------------

    //-----START--------- C# avatar maker app 16 delete a list and reset ---------------------------------------------
    private void btn_delete_Click(object sender, EventArgs e)
    {
        HeroList.hallOfFame.Remove((Hero)listBox1.SelectedItem);
        bs.ResetBindings(false);

    }
    //-----END--------- C# avatar maker app 16 delete a list and reset ---------------------------------------------

}

}

kulpot commented 8 months ago

###################### Form1.cs ###############################################

using Cavatar_maker_app_13; using C__avatar_maker_app_12; using Cheromaker_10; 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;

//------------------ C# avatar maker app 01 How to use checkboxes tutorial ----------------------------- //ref link:https://www.youtube.com/watch?v=UgbzxnlckxY&list=PLhPyEFL5u-i2w2fa7ErcbkbEkjqLh7Io1&index=15

// label(Hero's Name), textbox(MessageBox), groupbox, checkbox x8(abilities[0-7], button(btn_create), status_message

//----------------- C# avatar maker app 02 How to use a listbox tutorial ------------------------------- //ref link:https://www.youtube.com/watch?v=ihBNF1j5qNU&list=PLhPyEFL5u-i2w2fa7ErcbkbEkjqLh7Io1&index=14

// listbox(Item Collection)(SelectionMode:MultiSimple), label(Office Locations)

//----------------- C# avatar maker app 03 How to use radio buttons tutorial ------------------------------ //ref link:https://www.youtube.com/watch?v=0Byuvm4acFM&list=PLhPyEFL5u-i2w2fa7ErcbkbEkjqLh7Io1&index=14

// Radio button x4, groupbox(Preferred Transport), radio button x4(rdo prefix)(JetPack:Property->Checked->True,

//----------------- C# heromaker app 04 How to use scrollbars in Winforms ----------------------------------- //ref link:https://www.youtube.com/watch?v=dKXTx0swVUU&list=PLhPyEFL5u-i2w2fa7ErcbkbEkjqLh7Io1&index=13

// Scroolbars(LimitSpedd+Stamina+Strength to a maximum of 100totalPts, GroupBox(Speed-Stamina-Strength), HScrollBar x3(width300height15),label x6(Speed/Stamina/Strength/lbl_speed/stamina/strenght), trackBar,

//---------------- C# hermaker app 05 How to program a date picker in C# ------------------------------------- //ref link:https://www.youtube.com/watch?v=mq0lEliFY0E&list=PLhPyEFL5u-i2w2fa7ErcbkbEkjqLh7Io1&index=11

// GroupBox, DateTimePicker x3, Label x3,

//--------------- C# avatar maker app 06 How to use a number picker ------------------------------------------------ //ref link:https://www.youtube.com/watch?v=3tZNK3WPOW4&list=PLhPyEFL5u-i2w2fa7ErcbkbEkjqLh7Io1&index=12

// label, numericalUpDown(name:num_year_experience), \n

//-------------- C# avatar maker app 07 How to use the color picker ---------------------------------------------- //ref link:https://www.youtube.com/watch?v=_XBet_gEycg&list=PLhPyEFL5u-i2w2fa7ErcbkbEkjqLh7Io1&index=11

// label(Cape Color), pictureBox,

//------------- C# avatar maker app 08 How to use the trackbar control ------------------------------------------- //ref link:https://www.youtube.com/watch?v=H5cPurr1EeE&list=PLhPyEFL5u-i2w2fa7ErcbkbEkjqLh7Io1&index=10

// label(Dark Side Propensity),label(0/lbl_dark_side), trackBar(minimum:-10/largeChange:3),

//------------- C# heromaker part 09 Picturebox control Visual Studio ------------------------------- //ref link:https://www.youtube.com/watch?v=inK--sPEgws&list=PLhPyEFL5u-i2w2fa7ErcbkbEkjqLh7Io1&index=7

// groupBox(portrait), pictureBox(sizeMode:Zoom),

//-------------- C# heromaker 10 hero object --------------------------------------------------------- //ref link:https://www.youtube.com/watch?v=8zXxow4OZiY&list=PLhPyEFL5u-i2w2fa7ErcbkbEkjqLh7Io1&index=6

// created Hero.cs(HeroProperties/HeroConstructorMethod),

//-------------- C# avatar maker app 12 static list to work as a global variable ---------------------------- //ref link:https://www.youtube.com/watch?v=zJKHf7gi4iM&list=PLhPyEFL5u-i2w2fa7ErcbkbEkjqLh7Io1&index=4&t=20s

// created HeroList.cs,

//-------------- C# avatar maker app 13 use a static class to store a list of objects ------------------------------- //ref link:https://www.youtube.com/watch?v=LmZE2R2lUD4&list=PLhPyEFL5u-i2w2fa7ErcbkbEkjqLh7Io1&index=5

// added Form2.cs(WindowsForm)(Form2 Ref: Form2 f2 = new Form2();), listBox1(Form2.cs)(Prop:DataSource->addProjectDataSource->object->ERROR:noObject->Solution:Form2 Ref.AddedWithProp.), TextBox(Form2.cs)(Properties:Mutliline->True), button x3(Form2.cs),

//-------------- C# avatar maker app 15 sort a list of objects tutorial ----------------------------------------- //ref link:https://www.youtube.com/watch?v=v_FUCihxEzM&list=PLhPyEFL5u-i2w2fa7ErcbkbEkjqLh7Io1&index=3

// edit Form2.cs Button(name:A->Z/designName:btn_sortAZ),Button(Z->A/btn_sortZA),Add method to(button:A->Z/Z->A),

//---------------- C# avatar maker app 16 delete a list and reset --------------------------------------------- //ref link:https://www.youtube.com/watch?v=GGedi6Cme6s&list=PLhPyEFL5u-i2w2fa7ErcbkbEkjqLh7Io1&index=3&t=49s

// edit Form2.cs button(text:Delete/name:btn_delete)(ClickEvent), edit Form1.cs->Add:button(text:Reset Form/name:btn_reset),

namespace HeroMaker { public partial class Form1 : Form { //----START------- C# heromaker part 09 Picturebox control Visual Studio ------------------------------- // picture of hero string picture_of_hero = ""; //----END------- C# heromaker part 09 Picturebox control Visual Studio -------------------------------

    //------START------- C# avatar maker app 12 static list to work as a global variable ----------------------------
    //HeroList myHeroes = new HeroList(); // Problem:The scope of myHeroes is limited to the click function.(Solution:reposition here) //Error: avatarMakerApp13->HeroList.cs->turnTo Static
    //-------END------ C# avatar maker app 12 static list to work as a global variable ----------------------------

    public Form1()
    {
        InitializeComponent();
    }

    //------START---------- C# avatar maker app 01 How to use checkboxes tutorial -----------------------------

    private void btn_create_Click(object sender, EventArgs e)
    {
        //-----START------- C# heromaker 10 hero object ---------------------------------------------------------
        string heroName = txt_name.Text;
        //-----END------- C# heromaker 10 hero object ---------------------------------------------------------

        bool[] abilities = {false, false, false, false, false, false, false, false };

        abilities[0] = chk_fly.Checked;
        abilities[1] = chk_xray.Checked;
        abilities[2] = chk_invisible.Checked;
        abilities[3] = chk_energy.Checked;
        abilities[4] = chk_luck.Checked;
        abilities[5] = chk_fart.Checked;
        abilities[6] = chk_water_breath.Checked;
        abilities[7] = chk_timecontrol.Checked;

        //----START--------- C# avatar maker app 02 How to use a listbox tutorial -------------------------------
        // listbox
        // cities
        // array or list?
        List<String> cities = new List<String>();

        // add the selected items to cities
        foreach(String s in lst_cities.SelectedItems)
        {
            cities.Add(s);
        }
        //-----END---------- C# avatar maker app 02 How to use a listbox tutorial -------------------------------

        //-----START---------- C# avatar maker app 03 How to use radio buttons tutorial ------------------------------
        // preferred transport
        // use a single string since only one mode can be the preferred transport.

        string preferred_transport = " ";

        if (rdo_jetpack.Checked)
            preferred_transport = "Jet Pack";
        if (rdo_landspeeder.Checked)
            preferred_transport = "Land Speeder";
        if (rdo_teleport.Checked)
            preferred_transport = "Teleport";
        if (rdo_batmobile.Checked)
            preferred_transport = "Batmobile";
        //------END--------- C# avatar maker app 03 How to use radio buttons tutorial ------------------------------

        //-----START---------- C# heromaker app 04 How to use scrollbars in Winforms -----------------------------------
        int speed = scroll_speed.Value;
        int stamina = scroll_stamina.Value;
        int strength = scroll_strength.Value;
        if(speed + stamina + strength > 100)
        {
            MessageBox.Show("You cannot have more than 100 total points for speed, stamina, strength");
            Close();
        }
        //-------END-------- C# heromaker app 04 How to use scrollbars in Winforms -----------------------------------

        //------START-------- C# hermaker app 05 How to program a date picker in C# -------------------------------------
        // dates

        DateTime birthday = date_birthday.Value;
        DateTime superPowerDiscovery = date_power_discovery.Value;
        DateTime fatefulDay = date_fate.Value;
        //------END-------- C# hermaker app 05 How to program a date picker in C# -------------------------------------

        //-----START-------- C# avatar maker app 06 How to use a number picker ------------------------------------------------
        // years experience

        decimal years_experience = num_years_experience.Value;
        //-----END-------- C# avatar maker app 06 How to use a number picker ------------------------------------------------

        //----START------- C# avatar maker app 08 How to use the trackbar control -------------------------------------------
        // dark side
        int dark_side = 0;
        dark_side = trk_dark_side.Value;
        //-----END------ C# avatar maker app 08 How to use the trackbar control -------------------------------------------

        //------START---------- C# avatar maker app 01 How to use checkboxes tutorial -----------------------------
        string status_message = "Your new hero is " + txt_name.Text + "." +
            " \nYou have selected the following abilities: ";

        if (abilities[0])
            status_message += "Fly, ";
        if (abilities[1])
            status_message += "xRay Vision, ";
        if (abilities[2])
            status_message += "Invisibility, ";
        if (abilities[3])
            status_message += "Absorb Energy, ";
        if (abilities[4])
            status_message += "Extreme Luck, ";
        if (abilities[5])
            status_message += "Explosive Farts, ";
        if (abilities[6])
            status_message += "Water Breathing, ";
        if (abilities[7])
            status_message += "Time Control, ";
        //------END---------- C# avatar maker app 01 How to use checkboxes tutorial -----------------------------

        //----START--------- C# avatar maker app 02 How to use a listbox tutorial -------------------------------
        //listbox
        status_message += "\nThe hero works in these cities: "; 
        foreach(String city in cities)
        {
            status_message += city + ", ";
        }
        //-----END---------- C# avatar maker app 02 How to use a listbox tutorial -------------------------------

        //-----START---------- C# avatar maker app 03 How to use radio buttons tutorial ------------------------------
        status_message += " \nYour hero prefers to travel by " + preferred_transport + ".";
        //-----END---------- C# avatar maker app 03 How to use radio buttons tutorial ------------------------------

        //-----START---------- C# heromaker app 04 How to use scrollbars in Winforms -----------------------------------
        status_message += " \nSpeed: " + speed + " Stamina: " + stamina + " Strength: " + strength + ".";
        //-----END---------- C# heromaker app 04 How to use scrollbars in Winforms -----------------------------------

        //------START-------- C# hermaker app 05 How to program a date picker in C# -------------------------------------
        status_message += " \nYour hero was born on " + birthday;
        status_message += " \nThe hero discovered super powers on " + superPowerDiscovery;
        status_message += " \nThe fateful day for this person is " + fatefulDay;
        //------END-------- C# hermaker app 05 How to program a date picker in C# -------------------------------------

        //-----START-------- C# avatar maker app 06 How to use a number picker ------------------------------------------------
        status_message += " \nYears experience: " + years_experience;
        //-----END-------- C# avatar maker app 06 How to use a number picker ------------------------------------------------

        //-----START------- C# avatar maker app 07 How to use the color picker ----------------------------------------------
        status_message += "\nThe cape color for your hero is " + pic_cape_color.BackColor.ToString();
        //-----END------- C# avatar maker app 07 How to use the color picker ----------------------------------------------

        //----START------- C# avatar maker app 08 How to use the trackbar control -------------------------------------------
        status_message += "\nThe dark side probability for your hero is " + dark_side;
        //-----END------ C# avatar maker app 08 How to use the trackbar control -------------------------------------------

        //----START------- C# heromaker part 09 Picturebox control Visual Studio -------------------------------
        status_message += " \n The picture of the heros is: " + picture_of_hero;
        //----END------- C# heromaker part 09 Picturebox control Visual Studio -------------------------------

        //-----START------- C# heromaker 10 hero object ---------------------------------------------------------
        Hero hero = new Hero(heroName, abilities, cities, 
            preferred_transport, speed, stamina, strength, 
            birthday, superPowerDiscovery, fatefulDay, years_experience, 
            pic_cape_color.BackColor.ToString(),
            dark_side, 
            picture_of_hero);
        //-----END------- C# heromaker 10 hero object ---------------------------------------------------------

        //-----START------- C# avatar maker app 13 use a static class to store a list of objects -------------------------------
        HeroList.hallOfFame.Add(hero);
        //-----END------- C# avatar maker app 13 use a static class to store a list of objects -------------------------------

        //------START------- C# avatar maker app 12 static list to work as a global variable ----------------------------
        //HeroList myHeroes = new HeroList(); // Problem:The scope of myHeroes is limited to the click function.
        //myHeroes.hallOfFame.Add(hero);    //Error:avatarmakerapp13->because of HeroList turn to static
        //-------END------ C# avatar maker app 12 static list to work as a global variable ----------------------------

        //------START---------- C# avatar maker app 01 How to use checkboxes tutorial -----------------------------
        MessageBox.Show(status_message);
        //------END---------- C# avatar maker app 01 How to use checkboxes tutorial -----------------------------
        //------START------- C# avatar maker app 12 static list to work as a global variable ----------------------------
        //MessageBox.Show("your have made " + myHeroes.hallOfFame.Count() + " different heroes"); //Error:avatarmakerapp13->because of HeroList turn to static
        //-------END------ C# avatar maker app 12 static list to work as a global variable ----------------------------

        //-----START------- C# avatar maker app 13 use a static class to store a list of objects -------------------------------
        MessageBox.Show("your have made " + HeroList.hallOfFame.Count() + " different heroes");
        //-----END------- C# avatar maker app 13 use a static class to store a list of objects -------------------------------

        //-----START------- C# avatar maker app 13 use a static class to store a list of objects -------------------------------
        Form2 f2 = new Form2();
        f2.Show();
        //-----END------- C# avatar maker app 13 use a static class to store a list of objects -------------------------------
    }

    //--------START------- C# heromaker app 04 How to use scrollbars in Winforms -----------------------------------

    private void scroll_speed_Scroll(object sender, ScrollEventArgs e)
    {
        lbl_speed.Text = scroll_speed.Value.ToString();
    }

    private void scroll_stamina_Scroll(object sender, ScrollEventArgs e)
    {
        lbl_stamina.Text = scroll_stamina.Value.ToString();
    }

    private void scroll_strength_Scroll(object sender, ScrollEventArgs e)
    {
        lbl_strength.Text = scroll_strength.Value.ToString();
    }
    //------END--------- C# heromaker app 04 How to use scrollbars in Winforms -----------------------------------

    //-----START------- C# avatar maker app 07 How to use the color picker ----------------------------------------------
    private void pictureBox1_Click(object sender, EventArgs e)
    {
        ColorDialog colorPicker = new ColorDialog();

        if(colorPicker.ShowDialog() == DialogResult.OK)
        {
            pic_cape_color.BackColor = colorPicker.Color;
        }
    }
    //------END------- C# avatar maker app 07 How to use the color picker ----------------------------------------------

    //----START------- C# avatar maker app 08 How to use the trackbar control -------------------------------------------
    private void trackBar1_Scroll(object sender, EventArgs e)
    {
        lbl_dark_side.Text = trk_dark_side.Value.ToString();
    }
    //-----END------ C# avatar maker app 08 How to use the trackbar control -------------------------------------------

    //----START------- C# heromaker part 09 Picturebox control Visual Studio -------------------------------
    private void pictureBox1_Click_1(object sender, EventArgs e)
    {
        OpenFileDialog portraitPicker = new OpenFileDialog();

        if(portraitPicker.ShowDialog() == DialogResult.OK)
        {
            pictureBox1.Image = new Bitmap(portraitPicker.FileName);

            picture_of_hero = portraitPicker.FileName;
        }
    }
    //-----END------ C# heromaker part 09 Picturebox control Visual Studio -------------------------------

    //-----START--------- C# avatar maker app 16 delete a list and reset ---------------------------------------------
    private void btn_reset_Click(object sender, EventArgs e)
    {
        Form1 NewForm = new Form1();
        NewForm.StartPosition = FormStartPosition.Manual;
        NewForm.Location = this.Location;

        NewForm.Show();
        this.Dispose(false);
    }
    //------END-------- C# avatar maker app 16 delete a list and reset ---------------------------------------------

    //------END---------- C# avatar maker app 01 How to use checkboxes tutorial -----------------------------
}

}

kulpot commented 8 months ago

################# Hero.cs ####################################

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace C__heromaker_10 { //------START------ C# heromaker 10 hero object --------------------------------------------------------- //public class Hero

//-----START------- C# avatar maker app 15 sort a list of objects tutorial -----------------------------------------
public class Hero : IComparable
//-----END------- C# avatar maker app 15 sort a list of objects tutorial -----------------------------------------
{
    // HeroConstructorMethod //Create->Highlight all Hero Properties->CTRL+. ->Select Create Constructor Method
    public Hero(string name, 
        bool[] specialAbilities, 
        List<string> officeLocations, 
        string preferredTransport, 
        int speed, int staming, int strength, 
        DateTime birthday, 
        DateTime superPowerDiscoveryDate, 
        DateTime fatefulDay, 
        decimal yearsExperience, 
        string capeColor, 
        int darkSidePropensity, 
        string portraitPhoto)
    {
        Name = name;
        SpecialAbilities = specialAbilities;
        OfficeLocations = officeLocations;
        PreferredTransport = preferredTransport;
        Speed = speed;
        Staming = staming;
        Strength = strength;
        Birthday = birthday;
        SuperPowerDiscoveryDate = superPowerDiscoveryDate;
        FatefulDay = fatefulDay;
        YearsExperience = yearsExperience;
        CapeColor = capeColor;
        DarkSidePropensity = darkSidePropensity;
        PortraitPhoto = portraitPhoto;
    }

    // HeroProperties
    public string Name { get; set; }
    public bool[] SpecialAbilities { get; set; }
    public List<String> OfficeLocations { get; set; }
    public string PreferredTransport {  get; set; }

    public int Speed { get; set; }   // This also could have benn stored as an array of int "int[] speedStamingStrength = {0,0,0};
    public int Staming { get; set; }   
    public int Strength { get; set; }

    public DateTime Birthday { get; set; }
    public DateTime SuperPowerDiscoveryDate { get; set; }
    public DateTime FatefulDay { get; set; }

    public decimal YearsExperience { get; set; }

    public String CapeColor { get; set; }
    //public string CapeColor { get; set; }

    public int DarkSidePropensity { get; set; }

    public String PortraitPhoto { get; set; }
    //public string PortraitPhoto { get; set; }
    //-----END------- C# heromaker 10 hero object ---------------------------------------------------------

    //-----START------- C# avatar maker app 15 sort a list of objects tutorial -----------------------------------------
    public int CompareTo(object obj)
    {
        Hero other = (Hero)obj;
        return Name.CompareTo(other.Name);
    }
    //-----END------- C# avatar maker app 15 sort a list of objects tutorial -----------------------------------------

}

}

kulpot commented 8 months ago

########################## HeroList.cs ####################################

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using C__heromaker_10;

namespace C__avatar_maker_app_12 { //-----START------- C# avatar maker app 12 static list to work as a global variable ---------------------------- //public class HeroList //------END------ C# avatar maker app 12 static list to work as a global variable ----------------------------

//------START------ C# avatar maker app 13 use a static class to store a list of objects -------------------------------
static class HeroList       // Static means that there will be exactly one copy of the list in the entire application.
//------END------ C# avatar maker app 13 use a static class to store a list of objects -------------------------------
{
    //-----START------- C# avatar maker app 12 static list to work as a global variable ----------------------------
    //public List<Hero> hallOfFame = new List<Hero>();
    //------END------ C# avatar maker app 12 static list to work as a global variable ----------------------------

    //------START------ C# avatar maker app 13 use a static class to store a list of objects -------------------------------
    static public List<Hero> hallOfFame = new List<Hero>();
    //------END------ C# avatar maker app 13 use a static class to store a list of objects -------------------------------

}

}

kulpot commented 8 months ago

######################## Program.cs #############################

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms;

namespace HeroMaker { internal static class Program { ///

/// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }