By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,327 Members | 849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,327 IT Pros & Developers. It's quick & easy.

Adjust width of a textbox based on number of characters?

P: n/a
Hi,

I'm writing a control that inherits from TextBox. I need to adjust the
width of this control based on the number of characters in the control. I'm
having a hard time with this.

The problem (as I see it) is that the size of the font is in points, but the
size of the TextBox is in Pixels. I can't figure out a way to use VB.NET to
convert.

Anyone give me a clue?

Thanks
Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Oh yea, forgot to mention... I am using a fixed font. What I need to do is
look at the MaxLength of the TextBox, and adjust the width based on that.

Thanks.
"Scott Kilbourn" <skilbourn@NO_SPAM_PLEASE.appliedsystems.com> wrote in
message news:ut**************@TK2MSFTNGP12.phx.gbl...
Hi,

I'm writing a control that inherits from TextBox. I need to adjust the
width of this control based on the number of characters in the control. I'm having a hard time with this.

The problem (as I see it) is that the size of the font is in points, but the size of the TextBox is in Pixels. I can't figure out a way to use VB.NET to convert.

Anyone give me a clue?

Thanks

Nov 20 '05 #2

P: n/a
Hi,

Dim g As Graphics = TextBox1.CreateGraphics

TextBox1.Width = g.MeasureString(TextBox1.Text, TextBox1.Font).Width + 10

g.Dispose()
Ken
------------
"Scott Kilbourn" <skilbourn@NO_SPAM_PLEASE.appliedsystems.com> wrote in
message news:ut**************@TK2MSFTNGP12.phx.gbl...
Hi,

I'm writing a control that inherits from TextBox. I need to adjust the
width of this control based on the number of characters in the control.
I'm
having a hard time with this.

The problem (as I see it) is that the size of the font is in points, but
the
size of the TextBox is in Pixels. I can't figure out a way to use VB.NET
to
convert.

Anyone give me a clue?

Thanks

Nov 20 '05 #3

P: n/a
This would work, except that I don't actually have a value in the TextBox at
the time. I tried this...

g.MeasureString("XXXXXXXXXXXXXXXXXXXX", TextBox1.Font).Width + 10

And it worked perfectly. :)

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi,

Dim g As Graphics = TextBox1.CreateGraphics

TextBox1.Width = g.MeasureString(TextBox1.Text, TextBox1.Font).Width + 10

g.Dispose()
Ken
------------
"Scott Kilbourn" <skilbourn@NO_SPAM_PLEASE.appliedsystems.com> wrote in
message news:ut**************@TK2MSFTNGP12.phx.gbl...
Hi,

I'm writing a control that inherits from TextBox. I need to adjust the
width of this control based on the number of characters in the control.
I'm
having a hard time with this.

The problem (as I see it) is that the size of the font is in points, but
the
size of the TextBox is in Pixels. I can't figure out a way to use VB.NET to
convert.

Anyone give me a clue?

Thanks


Nov 20 '05 #4

P: n/a
This works... m_iMaxLineLength is the value for the number of characters
per line.

gTemp.MeasureString(Strings.StrDup(m_iMaxLineLengt h, "X"),
MyBase.Font).Width + 10

"Scott Kilbourn" <skilbourn@NO_SPAM_PLEASE.appliedsystems.com> wrote in
message news:uz**************@tk2msftngp13.phx.gbl...
This would work, except that I don't actually have a value in the TextBox at the time. I tried this...

g.MeasureString("XXXXXXXXXXXXXXXXXXXX", TextBox1.Font).Width + 10

And it worked perfectly. :)

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi,

Dim g As Graphics = TextBox1.CreateGraphics

TextBox1.Width = g.MeasureString(TextBox1.Text, TextBox1.Font).Width + 10

g.Dispose()
Ken
------------
"Scott Kilbourn" <skilbourn@NO_SPAM_PLEASE.appliedsystems.com> wrote in
message news:ut**************@TK2MSFTNGP12.phx.gbl...
Hi,

I'm writing a control that inherits from TextBox. I need to adjust the width of this control based on the number of characters in the control. I'm
having a hard time with this.

The problem (as I see it) is that the size of the font is in points, but the
size of the TextBox is in Pixels. I can't figure out a way to use

VB.NET to
convert.

Anyone give me a clue?

Thanks



Nov 20 '05 #5

P: n/a
Well, it mostly works, anyway. Depending on the max number of characters,
one or two characters are still going to the next line of the multiline
textbox.

"Scott Kilbourn" <skilbourn@NO_SPAM_PLEASE.appliedsystems.com> wrote in
message news:OL**************@tk2msftngp13.phx.gbl...
This works... m_iMaxLineLength is the value for the number of characters
per line.

gTemp.MeasureString(Strings.StrDup(m_iMaxLineLengt h, "X"),
MyBase.Font).Width + 10

"Scott Kilbourn" <skilbourn@NO_SPAM_PLEASE.appliedsystems.com> wrote in
message news:uz**************@tk2msftngp13.phx.gbl...
This would work, except that I don't actually have a value in the TextBox
at
the time. I tried this...

g.MeasureString("XXXXXXXXXXXXXXXXXXXX", TextBox1.Font).Width + 10

And it worked perfectly. :)

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi,

Dim g As Graphics = TextBox1.CreateGraphics

TextBox1.Width = g.MeasureString(TextBox1.Text, TextBox1.Font).Width +

10
g.Dispose()
Ken
------------
"Scott Kilbourn" <skilbourn@NO_SPAM_PLEASE.appliedsystems.com> wrote in message news:ut**************@TK2MSFTNGP12.phx.gbl...
> Hi,
>
> I'm writing a control that inherits from TextBox. I need to adjust the > width of this control based on the number of characters in the control. > I'm
> having a hard time with this.
>
> The problem (as I see it) is that the size of the font is in points, but > the
> size of the TextBox is in Pixels. I can't figure out a way to use

VB.NET
> to
> convert.
>
> Anyone give me a clue?
>
> Thanks
>
>



Nov 20 '05 #6

P: n/a
Do any of you guys know how to do the
same with printed text? The difference is
that I don't have an object of type

System.Drawing.Printing.PrintPageEventArgs

to use the measurestring method on.

Any ideas?

Thanks,

Jason.

On Tue, 20 Jan 2004 15:12:36 -0500, "Ken Tucker [MVP]"
<vb***@bellsouth.net> wrote:
Hi,

Dim g As Graphics = TextBox1.CreateGraphics

TextBox1.Width = g.MeasureString(TextBox1.Text, TextBox1.Font).Width + 10

g.Dispose()
Ken
------------
"Scott Kilbourn" <skilbourn@NO_SPAM_PLEASE.appliedsystems.com> wrote in
message news:ut**************@TK2MSFTNGP12.phx.gbl...
Hi,

I'm writing a control that inherits from TextBox. I need to adjust the
width of this control based on the number of characters in the control.
I'm
having a hard time with this.

The problem (as I see it) is that the size of the font is in points, but
the
size of the TextBox is in Pixels. I can't figure out a way to use VB.NET
to
convert.

Anyone give me a clue?

Thanks


Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.