【问题标题】:My code works when pasted into button but not from class我的代码在粘贴到按钮时有效,但在课堂上无效
【发布时间】:2015-10-27 09:58:23
【问题描述】:

我试图将代码形式的按钮放入一个类中,但在测试期间我注意到我的表单上的面板没有更新。直接将面板设置为按钮时效果很好(参见buttons2的代码)。

button1 代码...

private void button1_Click(object sender, EventArgs e)
        {
          SpecialPanelBuilder spb = new SpecialPanelBuilder();
          panel1 = spb.PopulatedPanel();

        }

特别小组类...

public class SpecialPanelBuilder
    {
        public Panel PopulatedPanel()
        {
            Panel p = new Panel();
            Graphics g;

            int size = 30;
            int TextLeft = size;
            //int LeftStartBox = 


            g = p.CreateGraphics();

            g.FillRectangle(Brushes.Red, 10, 10, size, size);
            g.DrawRectangle(new Pen(Color.Black), 10, 10, size, size);
            g.DrawString(("test"), new Font("Arial", size /2, FontStyle.Bold),
            Brushes.Black,
            (size + TextLeft),
            (size) - (-2));

            g.FillRectangle(Brushes.Orange, 300, 10, size, size);
            g.DrawRectangle(new Pen(Color.Black), 300, 10, size, size);
            g.DrawString(("test"), new Font("Arial", size / 2, FontStyle.Bold),
            Brushes.Black,
            (size + TextLeft + 300),
            (size) - (-2));

            g.FillRectangle(Brushes.Green, 600, 10, size, size);
            g.DrawRectangle(new Pen(Color.Black), 600, 10, size, size);
            g.DrawString(("test"), new Font("Arial", size/2, FontStyle.Bold),
            Brushes.Black,
            (size + TextLeft + 600),
            (size) - (-2));

            return p;
    }

按钮 2 代码以及该类的所有代码...

private void button2_Click(object sender, EventArgs e)
        {



            int size = 30;
            int TextLeft = size;
            //int LeftStartBox = 

            Graphics g;
            g = panel1.CreateGraphics();

            g.FillRectangle(Brushes.Red, 10, 10, size, size);
            g.DrawRectangle(new Pen(Color.Black), 10, 10, size, size);
            g.DrawString(("Below Threshold"), new Font("Arial", size / 2, FontStyle.Bold),
            Brushes.Black,
            (size + TextLeft),
            (size) - (-2));

            g.FillRectangle(Brushes.Orange, 300, 10, size, size);
            g.DrawRectangle(new Pen(Color.Black), 300, 10, size, size);
            g.DrawString(("Less than 20% above threshold"), new Font("Arial", size / 2, FontStyle.Bold),
            Brushes.Black,
            (size + TextLeft + 300),
            (size) - (-2));

            g.FillRectangle(Brushes.Green, 600, 10, size, size);
            g.DrawRectangle(new Pen(Color.Black), 600, 10, size, size);
            g.DrawString(("On Order"), new Font("Arial", size / 2, FontStyle.Bold),
            Brushes.Black,
            (size + TextLeft + 600),
            (size) - (-2));

        }

【问题讨论】:

    标签: c# .net oop


    【解决方案1】:

    好吧,我不确定是否正确,但在按钮 2 中,您的图形对象 g 是使用 panel1.CreateGraphics() 设置的,而 button1.Code 中的情况并非如此。所以你实际上是在使用你的内部面板变量的图形对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多