width of text in text box

brownpa

New member
Joined
Jun 16, 2006
Messages
4
Programming Experience
1-3
I everyone. I have a text box that is a standard width. I am trying to fill any remaining space in the text box after the user inputs text into the box, with astericks to the left of it to fill in the blank spaces in the text box. Any ideas on how this can be accomplished.

Any help in this matter would be greatly appreciated.
 
Some code below, it not very much off, using MeasureString with a single character (*) gives much worse result because it always adds some space before and after the string to be measured.
VB.NET:
[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] prependAsterix([/SIZE][SIZE=2][COLOR=#0000ff]ByRef[/COLOR][/SIZE][SIZE=2] tb [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] TextBox)
[/SIZE][SIZE=2][COLOR=#0000ff] Dim[/COLOR][/SIZE][SIZE=2] sb [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Text.StringBuilder
 sb.Append(tb.Text)
[/SIZE][SIZE=2][COLOR=#0000ff] Dim[/COLOR][/SIZE][SIZE=2] g [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Graphics = tb.CreateGraphics
 g.PageUnit = GraphicsUnit.Pixel
[/SIZE][SIZE=2][COLOR=#0000ff] Dim[/COLOR][/SIZE][SIZE=2] sf [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] SizeF = g.MeasureString(sb.ToString, tb.Font)
[/SIZE][SIZE=2][COLOR=#0000ff] Do [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] sf.Width < tb.Width
  sb.Insert(0, [/SIZE][SIZE=2][COLOR=#800000]"*"[/COLOR][/SIZE][SIZE=2])
  sf = g.MeasureString(sb.ToString, tb.Font)
[/SIZE][SIZE=2][COLOR=#0000ff] Loop
[/COLOR][/SIZE][SIZE=2] g.Dispose()
 tb.Text = sb.ToString
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
Call it like this:
VB.NET:
[SIZE=2]prependAsterix(TextBox2)
[/SIZE]
 
Back
Top