2017-04-11 22 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)