samedi 30 avril 2016

Changing Form BackColor in a If Statement

I am working on this form which has a list box full of items that are of a class called Team. I want that when a item is selected the main form will change its BackColor. I have this in a SelectedIndexChange event within a if statement, but when I run the program nothing happens when the item is slected. Here is my code:

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 TicketMaster
{
    public partial class frmMaster : Form
    {
        public frmMaster()
        {
            InitializeComponent();

            Team minnesotaTwins = new Team();
            minnesotaTwins.teamName = "Minnesota Twins";
            minnesotaTwins.stringPrice = "$79.00";
            minnesotaTwins.ticketPrice = 79.00;

            Team coloradoRockies = new Team();
            coloradoRockies.teamName = "Colorado Rockies";
            coloradoRockies.stringPrice = "$54.00";
            coloradoRockies.ticketPrice = 54.00;

            Team balOrioles = new Team();
            balOrioles.teamName = "Baltomore Orioles";
            balOrioles.stringPrice = "$86.00";
            balOrioles.ticketPrice = 86.00;

            Team bosRedsoxs = new Team();
            bosRedsoxs.teamName = "Boston Redsoxs";
            bosRedsoxs.stringPrice = "$75.00";
            bosRedsoxs.ticketPrice = 75.00;

            listTeams.Items.Add(minnesotaTwins);
            listTeams.Items.Add(coloradoRockies);
            listTeams.Items.Add(balOrioles);
            listTeams.Items.Add(bosRedsoxs);



        }

        public void listTeams_SelectedIndexChanged(object sender, EventArgs e)
        {
            Team team = (Team)listTeams.SelectedItem;

            lblSelectedPrice.Text = lblSelectedPrice.Tag + team.stringPrice;

            if (listTeams.SelectedIndex == 1)
            {
                this.BackColor = System.Drawing.Color.SteelBlue;
            }

        }

        private void form1_Load(object sender, EventArgs e)
        {

        }

        private void listTeams_MouseHover(object sender, EventArgs e)
        {
            lblPrices.Visible = true;
        }

        private void listTeams_MouseLeave(object sender, EventArgs e)
        {
            lblPrices.Visible = false;
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire