【问题标题】:How to make panels and labels work together?如何让面板和标签协同工作?
【发布时间】:2019-09-16 23:53:08
【问题描述】:

我正在制作带有面板和标签的 10x10 立方体,以检查光标在面板上输入了多少次,更改面板上的颜色以及显示数字的标签,如 1、2、3、4 等。颜色就像这 1-5 是蓝色,6-10 是绿色,11-15 是黄色,20 或更多是红色,我的问题是光标仅触摸标签时;只有标签改变但我的面板上的颜色没有改变或我的标签背景改变颜色但面板有其他颜色。

图片:

我之前问过一些类似的问题,但只检查了我面板上的颜色,所以这是代码:Old_Question:


private void panel_MouseEnter(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; 

    ctrl.Tag = ++count;
}

然后,我修改了代码以使用我的标签。

 private void panel_MouseEnter(object sender, EventArgs e)
        {
            Control ctrl = sender as Control;
            Control lctrl = 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;


            lctrl.Text = count.ToString();// count for my label
            count++;
            ctrl.Tag = count;

        }

注意:我在标签和面板上添加了相同的事件。

【问题讨论】:

  • 你为什么不限制你的条件。 . .if (count >= 20) ctrl.BackColor = Color.Red; else if (count >= 15 && count = 10 && count = 5 and count
  • 因为我觉得不重要...
  • 请显示截图?
  • @TerribleDog 我更新帖子
  • 如果你只使用标签而不使用面板,代码应该更简单,使用更少的资源。

标签: c# winforms panel


【解决方案1】:

哈维尔·阿塞维斯,

问题是当触发 MouseEnter 事件时,两个控件需要同时更新。要做到这一点,您需要做两件事。首先,正如其他人所提到的,Panel 和 Label 控件都需要调用一个通用的 MouseEnter 事件处理程序例程。其次,您需要更新触发事件的发送者以及发送者的父控件或子控件,具体取决于发送者是面板还是标签。

这是一个关于如何完成此操作的示例:

private void Process_MouseEnter(object sender, EventArgs e)
{
    int count = 0;
    Panel p = null;

    if (sender is Panel)
    {
        p = sender as Panel;

        // Compute the count using data stored in Label.Tag
        foreach (Control c in p.Controls)
            if (c is Label)
            {
                Label l = c as Label;
                l.Text = (((int)l.Tag) + 1).ToString();
                l.Tag = (int)l.Tag + 1;
                count = (int)l.Tag;
                break;
            }

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

    } /* end Panel if */


    if (sender is Label)
    {
        Label l = sender as Label;
        l.Text = (((int)l.Tag) + 1).ToString();
        l.Tag = (int)l.Tag + 1;
        count = (int)l.Tag;

        //set backcolor of control based on tag number             
        p = l.Parent as Panel;

        if (count >= 20) p.BackColor = Color.Red;
        else if (count >= 15) p.BackColor = Color.Yellow;
        else if (count >= 10) p.BackColor = Color.Lime;
        else if (count >= 5) p.BackColor = Color.Cyan;
        else p.BackColor = Color.SlateBlue;
    } /* end Label if */
}

请注意,可以通过合并其他人提到的想法来改进此代码。

对于初学者,如果面板是您的想法,而不是某些要求的一部分,正如 Phil1970 所提到的,如果您不使用面板,您的代码会简单得多。此外,为了解决数字显示问题,研究使用 LABEL 的 AutoElipsis 和 AutoSize 属性可能会有所帮助。

【讨论】:

  • 在你的p.<some Panel method> = <some expression> 上我需要添加“如果颜色”?,我很困惑
  • 是的,没错。我不想给出完整的答案,只是你需要帮助的部分。我把东西放在“”中的地方,或者评论以“Place...”开头的地方。是您需要添加逻辑的地方。我已将代码更新为完整的工作版本。
  • 哦,别担心,谢谢!,您的代码工作得非常好!
猜你喜欢
  • 2020-01-17
  • 2017-11-15
  • 2020-01-13
  • 2014-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多