如何制作具有相同行为的不同面板? [英] how to make different panels with same conduct?

查看:33
本文介绍了如何制作具有相同行为的不同面板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在制作带有面板的 10x10 立方体,当您单击面板 X 次时面板需要更改颜色,但是代码太大了,有没有其他方法可以让代码不那么长?

Hello I'm making 10x10 cube with panels and the panels needs to change color when you click X times the panel but the code is so large, Is there another way for the code not to be so long?

这是我的代码:

int cont1 = 0, cont2 = 0, cont3 = 0, cont4 = 0, cont5 = 0, cont6 = 0, cont7 = 0, cont8 = 0, cont9 = 0, cont10 = 0;

然后是改变颜色的事件(我所有的面板都有相同的代码,但不同的是cont"和面板名称):

then the event to change de color(All of my Panels have the same code but difference is "cont" and panel name):

 private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            cont1++;
            if (cont1 <= 5)
            {
                panel1.BackColor = Color.SlateBlue;
            }
            if (cont1 >=5)
            {
                panel1.BackColor = Color.Cyan;
            } 
            if (cont1 >= 10)
            {
                panel1.BackColor = Color.Lime;
            }
            if (cont1 >= 15)
            {
                panel1.BackColor = Color.Yellow;
            }
            if (cont1 >= 20)
            {
                panel1.BackColor = Color.Red;
            } 
        }

//other panel 
private void panel2_MouseClick(object sender, MouseEventArgs e)
        {
            cont2++;
            if (cont2 <= 5)
            {
                panel2.BackColor = Color.SlateBlue;
            }
            if (cont2 >= 5)
            {
                panel2.BackColor = Color.Cyan;
            }
            if (cont2 >= 10)
            {
                panel2.BackColor = Color.Lime;
            }
            if (cont2 >= 15)
            {
                panel2.BackColor = Color.Yellow;
            }
            if (cont2 >= 20)
            {
                panel2.BackColor = Color.Red;
            }
        }

注意:每个面板都会改变颜色,而不是同时改变实际上使用 4x4 但 10x10 对我来说很大

note: Each panel changes its color, not all at the same time Actually work with 4x4 but 10x10 is large for me

推荐答案

您实际上不需要创建大量变量,因为您需要跟踪控制状态.您可以利用 控制标签并减少您编写的代码量.

You don't actually need to create large amount of variables because you need to keep track of control states. You can leverage the existence of control Tags and cut down the amount of code you write.

无论是 1000 x 1000 面板,以下方法都可以完美地适用于您的用例.你需要做的就是将所有目标面板 mouseClick 指向它并允许它处理其余部分.

Be it 1000 x 1000 panels the below method would work flawlessly for your usecase. All you need do is point all target panel mouseClick to it and allow it handle the rest.

private void panel_MouseClick(object sender, MouseEventArgs e)
{
    Control ctrl = sender as Control;

    //get previous value from control tag or start at 0
    int count = ctrl.Tag == null ? 0 : (int)ctrl.Tag;

    //set backcolor of control based on tag number             
    if (count >= 20) ctrl.BackColor = Color.Red;
    else if (count >= 15) ctrl.BackColor = Color.Yellow; 
    else if (count >= 10) ctrl.BackColor = Color.Lime;
    else if (count >= 5)  ctrl.BackColor = Color.Cyan;
    else ctrl.BackColor = Color.SlateBlue; 

   // if (count == xx)
    //{// you may want to reset count after a certain number. Do that here...}

    ctrl.Tag = ++count;
}

这篇关于如何制作具有相同行为的不同面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆