Textbox resize problem

R

ramhog

Hello all,
I am trying to resize a textbox to exactly fit the contents. The
method I am using gets it close but it is just a little bit too small.
Could someone tell me where I am going wrong here please?

/* SYSTEM GENERATED FOR THE TEXTBOX */
//
// txt
//
this.txt.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txt.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
this.txt.Location = new System.Drawing.Point(72, 48);
this.txt.MaxLength = 100;
this.txt.Name = "txt";
this.txt.Size = new System.Drawing.Size(288, 13);
this.txt.TabIndex = 2;
this.txt.Text = "Test String";

/* MY CODE */
SDR.Graphics g = txt.CreateGraphics();
SDR.Font fnt = new SDR.Font(txt.Font, SDR.FontStyle.Bold);
SDR.SizeF size = g.MeasureString(txt.Text, fnt);
txt.Width = SYS.Convert.ToInt32(size.Width);
txt.Height = SYS.Convert.ToInt32(size.Height);

Thank you.
Kalvin
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You need to make it a little bigger than the text itself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top