A
A
Andrey Begin2018-08-27 22:40:36
C++ / C#
Andrey Begin, 2018-08-27 22:40:36

How can I get rid of such garbage in the code?

Wrote handlers for the buttons, when hovering and livin the text changes color accordingly.
But here a problem arose. These "mini-functions" take up too much space in the code
. Is there any way to reduce them? Is there an alternative solution to write a handler? Create a separate class for all this?

private void label5_MouseEnter(object sender, EventArgs e)
        {
            label5.ForeColor = Color.FromArgb(35, 33, 61);
        }

        private void label5_MouseLeave(object sender, EventArgs e)
        {
            label5.ForeColor = Color.White;
        }

        private void label6_MouseEnter(object sender, EventArgs e)
        {
            label6.ForeColor = Color.FromArgb(35, 33, 61);
        }

        private void label6_MouseLeave(object sender, EventArgs e)
        {
            label6.ForeColor = Color.White;
        }

        private void label7_MouseEnter(object sender, EventArgs e)
        {
            label7.ForeColor = Color.FromArgb(35, 33, 61);
        }

        private void label7_MouseLeave(object sender, EventArgs e)
        {
            label7.ForeColor = Color.White;
        }

        private void label8_MouseEnter(object sender, EventArgs e)
        {
            label8.ForeColor = Color.FromArgb(35, 33, 61);
        }

        private void label8_MouseLeave(object sender, EventArgs e)
        {
            label8.ForeColor = Color.White;
        }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Meloman19, 2018-08-28
@MoVVe

Lots of labels that do the same thing on hover? Isn't it easier to create just one handler and bind events from all the necessary labels to it?

private void label_MouseEnter(object sender, EventArgs e)
{
   (sender as Label).ForeColor = Color.FromArgb(35, 33, 61);
}

private void label_MouseLeave(object sender, EventArgs e)
{
   (sender as Label).ForeColor = Color.White;
}

Now all events specify the same handler.

S
Stanislav Makarov, 2018-08-27
@Nipheris

Create a separate class for all this?

Not a bad idea. You can also use WPF.

N
NonameProgrammer, 2018-08-27
@NonameProgrammer

I don't know... And if (as you were already told in the last question) use an array of labels and make an event handler for them? Before that, create an array (in order of precedence) with colors and when the event is called, the array index will move, thereby changing the color.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question