Ok, so this is the problem: in C# forms I've created a new private void:
private void NewBtn(string Name, int x, int y)
Which has a purpose of creating a picturebox that imitates behavior of a button (don't ask why, I simply enjoy complicating things) and can be called as many times as I want.
Font btnFont = new Font("Tahoma", 16);
PictureBox S = new PictureBox();
S.Location = new System.Drawing.Point(x, y);
S.Paint += new PaintEventHandler((sender, e) =>
{
e.Graphics.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.AntiAlias;
e.Graphics.DrawString(Name, btnFont, Brushes.Black, 0, 0);
});
Controls.Add(S);
Now, I am worried about part with Paint/Graphics (ignore rest of the code, I only gave some of it). I want to center the text that I write as "Name" in void when I call it "NewBtn(Name, x, y)". So, what should I put as
e.Graphics.DrawString(Name, btnFont, Brushes.Black, ThisX???, 0);
Suggestions?
You can improve this by measuring the string only once, considering the font and text won't change for a specific Button/PictureBox.
And I would also suggest checking if
S.Size
is wider / taller thansize
and handling it, so graphics won't try to draw a string starting at negative coordinates.Try using the Graphics.DrawString methods that uses the String.Drawing.StringFormat Option
You have two options here the first to use coordinates.
the second is to create a rectange like this: