I am writing a WinForms program that prints text from the label on the pictureBox. Made it in two ways:

1) Dynamically creating a label and pictureBox

2) Statically creating them

The main parameters were set the same, but the results were different. Below is the code of the first and second methods (I’ll still throw the private void InitializeComponent() from Form1.Designer.cs so that you can clearly see that the properties are set the same):

one)

 public Form1() { InitializeComponent(); Size = new Size(500, 500); var text = "Hello world"; var font = new Font("Times New Roman", 20); var pictureBox = new PictureBox() { Parent = this, BackColor = SystemColors.ControlLightLight, Size = new Size(300, 300), Location = new Point(50, 50) }; var label = new Label { Parent = pictureBox, Text = text, BackColor = SystemColors.Control, AutoSize = true, Font = font, Location = new Point(60, 60) }; label.MouseDown += (o, e) => { label.UseCompatibleTextRendering = !label.UseCompatibleTextRendering; }; pictureBox.MouseDown += (o, e) => { label.Visible = !label.Visible; }; pictureBox.Paint += (o, e) => { if (label.UseCompatibleTextRendering) e.Graphics.DrawString(label.Text, label.Font, new SolidBrush(label.ForeColor), label.Location); else TextRenderer.DrawText(e.Graphics, label.Text, label.Font, label.Location, label.ForeColor); }; } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Text = "Form1"; } 

2)

 public Form1() { InitializeComponent(); label.MouseDown += (o, e) => { label.UseCompatibleTextRendering = !label.UseCompatibleTextRendering; }; pictureBox.MouseDown += (o, e) => { label.Visible = !label.Visible; }; pictureBox.Paint += (o, e) => { if (label.UseCompatibleTextRendering) e.Graphics.DrawString(label.Text, label.Font, new SolidBrush(label.ForeColor), label.Location); else TextRenderer.DrawText(e.Graphics, label.Text, label.Font, label.Location, label.ForeColor); }; } private void InitializeComponent() { this.pictureBox = new System.Windows.Forms.PictureBox(); this.label = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // // pictureBox // this.pictureBox.BackColor = System.Drawing.SystemColors.ControlLightLight; this.pictureBox.Location = new System.Drawing.Point(50, 50); this.pictureBox.Name = "pictureBox"; this.pictureBox.Size = new System.Drawing.Size(300, 300); this.pictureBox.TabIndex = 0; this.pictureBox.TabStop = false; // // label // this.label.AutoSize = true; this.label.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); this.label.Location = new System.Drawing.Point(60, 60); this.label.Name = "label"; this.label.Size = new System.Drawing.Size(146, 31); this.label.TabIndex = 1; this.label.Text = "Hello world"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(500, 500); this.Controls.Add(this.label); this.Controls.Add(this.pictureBox); this.Name = "Form1"; this.Text = "Form1"; ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } 

Here are the results of the first and second methods:

1st method

2nd method

Why are the conclusions different?

    1 answer 1

    In the first case, the parent container for the label is the pictureBox . Accordingly, the label is located relative to the edge of this pictureBox and closes the drawn text, which is most likely present there.

    In the second case, the parent container for the label is Form1 . The label located relative to the edge of Form1 , and you see both the text drawn in the pictureBox and the label from which this text was drawn.

    • Damn, for sure, but I thought that if the label was transferred to the pictureBox at the very beginning, it would automatically become attached to it, and not to the form. Now I took a closer look at InitializeComponent() and it shows that the label added to the form. Thank you - Valeriy