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.
>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) );
