2017-04-11 70 views
1

我试图将我的标签“日期”放在我的其他标签上。但我真的很挣扎着。我用bringToFront和sendToBack尝试了它,但都没有成功。我的目标是,日期标签在前面。我不能更改我的标签的z位置

代码:

  //Create Fill Panels 
      createFillPanels(); 

      //Create Panel 
      Panel test = new Panel(); 
      count++; 
      //Fill Panel 
      test.Name = "panel" + i; 
      test.Width = 245; 
      test.Height = 170; 
      test.BackColor = Color.White; 
      Label date = new Label(); 
      if(i >= 9) 
      { 
       int description = i + 1; 
       date.Text = description.ToString(); 
      } 
      else 
      { 
       int description = i + 1; 
       date.Text = 0 + description.ToString(); 
      } 

      //Create Label 

      string day = date.Text; 
      string year = DateTime.Now.Year.ToString(); 
      string month = tbMonat.Text; 
      string stringDate = day + "." + month + "." + year; 
      DateTime dt = Convert.ToDateTime(stringDate); 

      if (dt.DayOfWeek == DayOfWeek.Sunday) 
      { 
       test.BackColor = Color.LightGray; 
      } 

      //Label frühschicht 
      Label frühschicht = new Label(); 
      string frühschichtText = datenbank.panelBefüllen(tag, monat, jahr, "Frühschicht", null, null, null, null); 

      if(frühschichtText != null) 
      { 
       frühschicht.Text = "Frühschicht: " + frühschichtText; 
       frühschicht.ForeColor = Color.Black; 
       frühschicht.Width = 215;    } 
      frühschicht.Location = new System.Drawing.Point(frühschichtlocationX, frühschichtlocationY); 

      date.Location = new System.Drawing.Point(datelocationX, datelocationY); 
      foreach (Control c in this.Controls) 

       if (frühschicht.Text.Contains("Offe")) 
       { 
        frühschicht.BackColor = Color.LightPink; 

        fehlercount++; 
       } 
      frühschicht.SendToBack(); 
      test.Controls.Add(frühschicht); 
      date.BringToFront(); 
      test.Controls.Add(date); 
      flowLayoutPanel1.Controls.Add(test); 
      test.Show(); 

      //Label spätschicht 
      Label spätschicht = new Label(); 
      string spätschichtText = datenbank.panelBefüllen(tag, monat, jahr, "Spätschicht", null, null, null, null); 

      if (spätschichtText != null) 
      { 
       spätschicht.Text = "Spätschicht: " + spätschichtText; 
       spätschicht.ForeColor = Color.Black; 
       spätschicht.Width = 215; 
      } 
      spätschicht.Location = new System.Drawing.Point(spätschichtlocationX, spätschichtlocationY); 
      spätschicht.SendToBack(); 
      test.Controls.Add(spätschicht); 
      date.Location = new System.Drawing.Point(datelocationX, datelocationY); 
      date.BringToFront(); 
      foreach(Control c in this.Controls) 

      if(spätschicht.Text.Contains("Offe")) 
      { 
        spätschicht.BackColor = Color.LightPink; 
        fehlercount++; 
      } 

      test.Controls.Add(date); 
      flowLayoutPanel1.Controls.Add(test); 
      test.Show(); 

图片: How it looks like at the moment

+0

您已将日期移至前面,但未将其添加到'test'对象 –

+0

我已将其添加到测试对象后尝试过。像这样:test.Controls [frühschicht.Name] .SendToBack();但它只是给了我一个NullReferenceException。我做错了吗? – CallMeLeonardo

+0

...当然与日期一样,只是用bringToFront ... – CallMeLeonardo

回答

2

Z-顺序总是设置相对于它设置的时间存在于母公司所有控件。因此,您无法将其设置为您尚未添加的控件。添加更多控件后,它也不会继续工作。

所以:在添加了所有竞争对照后,必须设置Z-order(s)