Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why the richTextbox control is not location in the middle of the form1?

private void btnSettingsFile_Click(object sender, EventArgs e)
{
    RichTextBox rtx1 = new RichTextBox();
    rtx1.Size = new Size(250,250);
    rtx1.BackColor = Color.Black;
    rtx1.ForeColor = Color.Yellow;
    this.Controls.Add(rtx1);
    rtx1.BringToFront();
    rtx1.Location = new Point(this.ClientSize.Width / 2, this.ClientSize.Height / 2);
    rtx1.AppendText(File.ReadAllText(settingsFile));
}

first I tried this.Width / 2 and this.Height / 2 but it put it on the right bottom then I tried with the ClientSize but this put it a bit to the bottom not in the center of form1.

I want the richTextBox to be in the center of form1.

not in the center of form1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

A control’s .Location property marks its top left corner, not its center. Therefore the top left corner of the control IS centered on the form, and this matches what we can see in the image.

To fix it, you need to subtract back 1/2 of the length and width of the control.

rtx1.Location = new Point( (this.ClientSize.Width / 2) - (rtx1.Width / 2), (this.ClientSize.Height / 2) - (rtx1.Height / 2) );
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading