0
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);
            }
          }
        }
    }
}

好的,所以我只是想创建一个简单的程序,当您将鼠标悬停在这些按钮上时,它会将这些按钮的背景颜色更改为一些纯色。我使用 TextBox 也有同样的工作,但我需要让它与按钮一起工作。我将所有内容都更改为使用 Button 类,它似乎具有所有相同的功能,但是现在程序运行了,但是当您将鼠标悬停在按钮上时没有任何反应。

我对 c#、堆栈交换很陌生,而且对一般编程来说只是稍微少一点,如果我在礼仪/格式化我的问题等方面很抱歉,我很抱歉。

4

1 回答 1

1

我认为您在实际调用按钮名称时试图引用您的按钮标题

改变:

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

至:

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

使代码工作。

如果您确实想使用“Spring”、“Summer”等参考,我建议您更改

bttnName = txt.Name;

bttnName = txt.Text;

并确保标签的文本设置正确

于 2013-10-13T09:55:16.233 回答