带标签的奇怪绘图行为

时间:2014-07-01 05:12:22

标签: c# winforms

在启动时我生成了很多控件90准确无误并且一切正常工作除了标签之外他们没有被绘制或什么?他们在那里,因为我可以点击它们,并在这里显示正确的ID(点击事件)生成代码

    private static bool ClientsLoaded = false;
    private static WebBrowser[] Clients = new WebBrowser[45];
    private static Label[] ClientLabel = new Label[45];
    private static int MaximizedClient = -1;

    public Form1()
    {
        InitializeComponent();

        int WBoffsetX = 0;
        int WBoffsetY = 0;

        int lbloffsetX = 0;
        int lbloffsetY = 0;


        for (int i = 0; i < 45; i++)
        {
            var wb = new WebBrowser();
            Clients[i] = wb;
            wb.ScrollBarsEnabled = false;
            wb.Height = 12;
            wb.Width = 12;
            wb.Location = new Point(2 + WBoffsetX, 2 + WBoffsetY);
            WBoffsetX += 13;
            wb.ScriptErrorsSuppressed = true;

            this.Controls.Add(wb);

            ClientLabel[i] = new Label();

            ClientLabel[i].Name = "lbl_" + i;
            ClientLabel[i].Font = new Font("Arial", 12);
            ClientLabel[i].ForeColor = System.Drawing.Color.White;
            ClientLabel[i].Location = new Point(12 + lbloffsetX, 450 + lbloffsetY);
            lbloffsetX += 22;
            ClientLabel[i].Click += new EventHandler(lbl_click);
            ClientLabel[i].Text = "C" + i + ": o";

            this.Controls.Add(ClientLabel[i]);
        }
    }

我尝试添加了一个用于(45)clientlabel [i] .Refresh()的按钮,它没有做任何事我尝试将它们的可见性全部改为false然后回到true但是没有找到有趣的是,如果我隐藏lbl_1标签2文本将出现如果我有标签2标签3文本将出现但如果我将以前的标签更改回可见它们保持隐形文字

我可以点击表单上的一行

    private void lbl_click(object sender, EventArgs e)
    {
        int id = -1;
        var s = sender.ToString();

        for(int i = 0; i<=45; i++)
        {
            if (s.Contains("C" + i + ":"))
            {
                id = i;
            }
        }

        MessageBox.Show("Hello label, " + id);
    }

将弹出适当的ID等 有谁知道造成这种情况的原因可能是什么?或者如何解决它

1 个答案:

答案 0 :(得分:1)

好吧,我不知道是什么问题。此代码运行良好,与原始代码(AutoSize属性,高度和宽度的显式语句以及次要位置调整)仅有微小差异:

 for (int i = 0; i < ClientLabel.Length; i++)
   {    
        // Web browsers
        WebBrowser wb = new WebBrowser()
        {
            ScrollBarsEnabled = false,
            Height = 12,
            Width = 12,
            Location = new Point(2 + WBoffsetX, 2 + WBoffsetY),
            ScriptErrorsSuppressed = true
        };

        WBoffsetX += 13;
        Clients[i] = wb;

        // Labels
        Label label = new Label()
        {
            Name = "label_" + i,
            Text = "Data",
            AutoSize = true,
            Location = new Point(50 + lbloffsetX, 50 + lbloffsetY),
            Width = 100,
            Height = 20,
            Font = new Font("Arial", 12),
            ForeColor = System.Drawing.Color.White,
        };

        label.Click += new EventHandler(lbl_click);
        ClientLabel[i] = label;
        lbloffsetX += 30;
    }

    this.Controls.AddRange(Clients);
    this.Controls.AddRange(ClientLabel);