1

I'm trying to position my label "date" over my other labels. But i'm really struggling with it. I tried it with bringToFront and with sendToBack, but neither worked. My goal is, that the date label is at the front.

Code:

            //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();

Picture: How it looks like at the moment

  • You've moved date to the front without even adding it to the the `test' object –  Apr 11 '17 at 12:35
  • I've already tried it after adding it to the test object. Like this: test.Controls[frühschicht.Name].SendToBack(); But it just gave me a NullReferenceException. Am i doing it wrong ? – CallMeLeonardo Apr 11 '17 at 12:43
  • ...and of course the same with the date, just with bringToFront... – CallMeLeonardo Apr 11 '17 at 12:48
  • Do test.Controls.Add(spätschicht); Then spätschicht.SendToBack(); –  Apr 11 '17 at 12:49
  • Same result. I didn't get an error, but compared to the picture i uploaded, nothing changed at all. – CallMeLeonardo Apr 11 '17 at 12:53
  • Possible duplicate of [Bring Winforms control to front](http://stackoverflow.com/questions/2618073/bring-winforms-control-to-front) – Rekshino Apr 11 '17 at 13:01
  • Already tried every solution of the other solution. I even tried a lot of other solutions which I found on google/stackoverflow. But nothing worked for me. Maybe i will just try to work around this problem... – CallMeLeonardo Apr 11 '17 at 13:23
  • What are you targetting: Winforms, WPF, ASP..? __Always__ tag your question correctly! – TaW Apr 11 '17 at 13:47
  • z-order must be set __after all__ competing controls have been added. Do that and update the question with the new code! We won't work on an assumption on how the current state of your code is! – TaW Apr 11 '17 at 13:50
  • @TaW Thanks to draw attention to me, for tagging my question wrong. Your answer helped me. I putted it at the very end and now it works. Should i post my finished code for other peoples with this problem, or just leave it as it is ? Still quite new here. – CallMeLeonardo Apr 11 '17 at 13:58
  • If you feel the problem is resolved you should accept the most useful answer. If none is given but the issue still resovled via the comments you can either write an answer yourself or delete the question. This is not a useless question (like, say, issues caused on typos) so it should not be deleted. I wrote an answer as I feel this is the most concise and useful way; posting the new and corrected code is less useful than posting the rules to follow imo. If you post a self-answer you can get reputation if somebody upvotes it. If you accept an answer you also get a little reputation.. – TaW Apr 11 '17 at 14:47

1 Answers1

2

Z-order is always set relative to all controls that exist in the Parent at the time it is set. Therefore you can't set it to a control you have not even added. Nor will it keep working after more controls are added.

So: Z-order(s) must be set after all competing controls have been added.

TaW
  • 53,122
  • 8
  • 69
  • 111