Question

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 Cameron_PickerillTrinitapoli_Assn1
{
    public partial class seasonalBttnChanger : Form
    {
        public seasonalBttnChanger()
        {
            InitializeComponent();
        }

        //Triggers when program first loads
        //Sets up
        private void bttnChanger_Load(object sender, EventArgs e)
        {
            List<Button> lstTxt = new List<Button>
            {
            bttnSpring, bttnSummer, bttnAutumn, bttnWinter
            };

            //Wiring up event handlers in run time
            foreach (Button txt in lstTxt)
            {
                txt.MouseEnter += button_MouseEnter;
                txt.MouseLeave += button_MouseLeave;
            }
        }

        // Sets up different background colors for TextBoxes
        //* Static values for the color of each button need
        //to be added.
        //**Not what I was trying to accomplish
        //**This needs to go somewhere else
        void button_MouseEnter(object sender, EventArgs e)
        {
            try
            {
                // Event handlers always pass the parameter "sender" in as an object.
                //  You have to downcast it back to the actual type.
                String bttnName = null;
                String bttnNameSpring = "Spring";
                String bttnNameSummer = "Summer";
                String bttnNameAutumn = "Autumn";
                String bttnNameWinter = "Winter";
                Button txt = (Button)sender;
               // stkColor.Push(txt.BackColor);
                //txt.BackColor = Color.Red;
                bttnName = txt.Name;

                if (bttnName == bttnNameSpring)
                {
                    txt.BackColor = Color.LightGreen;
                }
                else if (bttnName == bttnNameSummer)
                {
                    //txt.BackColor = Color.Red;
                    txt.BackColor = Color.Red;
                }
                else if (bttnName == bttnNameAutumn)
                {
                    txt.BackColor = Color.Yellow;
                }
                else if (bttnName == bttnNameWinter)
                {
                    txt.BackColor = Color.Cyan;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error:\n   " + ex.Message);
            }
        }

        //Handler for mouse leaving the button
        void button_MouseLeave(object sender, EventArgs e)
        {
            try
            {
                Button txt = (Button)sender;
                //txt.BackColor = stkColor.Pop();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error:\n   " + ex.Message);
            }
          }
        }
    }
}

Alright, so I'm just trying to create a simple program that changes the background colors of these buttons to some plain colors when you mouse over them. I had the same thing working using TextBox, but I need to get it working with button. I changed everything over to use the Button class, and it seems to have all the same functionality, but now the program runs, but nothing happens when you mouse over the buttons.

I'm pretty new to c#, stack exchange, and only slightly less so to programming in general, so sorry if I'm dinking up on etiquette/formatting my questions, etc.

Was it helpful?

Solution

I think you are trying to reference your buttons title when you are actually calling the button name

Changing:

            String bttnName = null;
            String bttnNameSpring = "Spring";
            String bttnNameSummer = "Summer";
            String bttnNameAutumn = "Autumn";
            String bttnNameWinter = "Winter";
            Button txt = (Button)sender;

To:

                String bttnName = null;
            String bttnNameSpring = "bttnSpring";
            String bttnNameSummer = "bttnSummer";
            String bttnNameAutumn = "bttnAutumn";
            String bttnNameWinter = "bttnWinter";

Causes the code to work.

If you do infact want to use the "Spring", "Summer" etc reference than i would suggest changing

bttnName = txt.Name;

to

bttnName = txt.Text;

And ensure that the Text of the labels is set correctly

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top