TextBox txt;//传进来的文本框实例,下面是字符串居中
StringFormat SF = new StringFormat();
SF.LineAlignment = StringAlignment.Center;//设置属性为水平居中
SF.Alignment = StringAlignment.Center;//设置属性为垂直居中
//设置文本框范围,坐标轴和宽高
Rectangle pbk = new Rectangle(txt.Location.X, txt.Location.Y, txt.Width, txt.Height);
g.DrawString(textvalue, txt.Font, Brushes.Black, pbk, SF);