473,568 Members | 2,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adjust width of a textbox based on number of characters?

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
6 12593
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_S PAM_PLEASE.appl iedsystems.com> wrote in
message news:ut******** ******@TK2MSFTN GP12.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
Hi,

Dim g As Graphics = TextBox1.Create Graphics

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

g.Dispose()
Ken
------------
"Scott Kilbourn" <skilbourn@NO_S PAM_PLEASE.appl iedsystems.com> wrote in
message news:ut******** ******@TK2MSFTN GP12.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
This would work, except that I don't actually have a value in the TextBox at
the time. I tried this...

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

And it worked perfectly. :)

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

Dim g As Graphics = TextBox1.Create Graphics

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

g.Dispose()
Ken
------------
"Scott Kilbourn" <skilbourn@NO_S PAM_PLEASE.appl iedsystems.com> wrote in
message news:ut******** ******@TK2MSFTN GP12.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
This works... m_iMaxLineLengt h is the value for the number of characters
per line.

gTemp.MeasureSt ring(Strings.St rDup(m_iMaxLine Length, "X"),
MyBase.Font).Wi dth + 10

"Scott Kilbourn" <skilbourn@NO_S PAM_PLEASE.appl iedsystems.com> wrote in
message news:uz******** ******@tk2msftn gp13.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 ("XXXXXXXXXXXXX XXXXXXX", TextBox1.Font). Width + 10

And it worked perfectly. :)

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

Dim g As Graphics = TextBox1.Create Graphics

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

g.Dispose()
Ken
------------
"Scott Kilbourn" <skilbourn@NO_S PAM_PLEASE.appl iedsystems.com> wrote in
message news:ut******** ******@TK2MSFTN GP12.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
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_S PAM_PLEASE.appl iedsystems.com> wrote in
message news:OL******** ******@tk2msftn gp13.phx.gbl...
This works... m_iMaxLineLengt h is the value for the number of characters
per line.

gTemp.MeasureSt ring(Strings.St rDup(m_iMaxLine Length, "X"),
MyBase.Font).Wi dth + 10

"Scott Kilbourn" <skilbourn@NO_S PAM_PLEASE.appl iedsystems.com> wrote in
message news:uz******** ******@tk2msftn gp13.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 ("XXXXXXXXXXXXX XXXXXXX", TextBox1.Font). Width + 10

And it worked perfectly. :)

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

Dim g As Graphics = TextBox1.Create Graphics

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

10
g.Dispose()
Ken
------------
"Scott Kilbourn" <skilbourn@NO_S PAM_PLEASE.appl iedsystems.com> wrote in message news:ut******** ******@TK2MSFTN GP12.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
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.PrintP ageEventArgs

to use the measurestring method on.

Any ideas?

Thanks,

Jason.

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

Dim g As Graphics = TextBox1.Create Graphics

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

g.Dispose()
Ken
------------
"Scott Kilbourn" <skilbourn@NO_S PAM_PLEASE.appl iedsystems.com> wrote in
message news:ut******** ******@TK2MSFTN GP12.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3390
by: Bob Alston | last post by:
In 2002, "GrayJay" posted the following code: I did this in a jazz record catalogue to find composers - On a form "frmComposers" Create a text box - txtFindComposer, and add the following sub Private Sub txtFindComposer_Change() Requery Me!.SetFocus
2
1773
by: takilroy | last post by:
Hi, I would like to set the width of a textbox from the server in an aspx form. I change the contents of the textbox from the server and would like to change the width accordingly. I know there is code out there to use creategraphics to do this, but it will not work for aspx files. It seems like such a simple thing. Does anyone have any...
0
1810
by: Oenone | last post by:
I would like to set the caret position in a (single-line) textbox to a position based on the number of pixels from the left of the control. For example, I want the caret to be positioned 50 pixels from the left (or as near to there as possible based on the characters in the control). I basically have the position in which the user has...
1
14319
by: papalarge | last post by:
Hey all... I've been using the following to calculate the height the textbox needs to be in order to grow it vertically to its necessary size. textbox1.Height = Me.CreateGraphics().MeasureString(textbox1.Text, textbox1.Font, textbox1.Width).Height However it's not appearing to work that well, especially when I'm using a RichTextbox...
1
2801
by: =?Utf-8?B?d2NsYXJrZTEwMQ==?= | last post by:
Hello all, I've created the following ASP.NET code sample which, when rendered, displays a 100% textbox within a single cell table. The problem is that Internet Explorer (6 or 7) truncates the right-hand edge of the textbox. The textbox renders correctly on Firefox and Netscape. <%@ Page Language="VB" AutoEventWireup="false"...
1
2344
by: Luzuko | last post by:
I would like to know how can i restrict textbox input in VB.net using code instead of VB.net controls. e.g If i want a user to type numbers only in a textbox(ID number textbox), how can i make sure that nothing else besides numbers are typed in the textbox
4
5645
by: DeanJo | last post by:
I am trying to develop a resize module i can include on my pages that calculates the size of input fields based on the screen resolution... I can already detect the screen resolution, and i have the width / height multiplier for every resolution i need to support... my problem is this... I want the include file to be totally universal so...
2
2473
by: Ruth Barn | last post by:
The issue is that the calculated text box does not actually requery until each of the associated listboxes have been clicked into and exited. I have an unbound form that looks up various pieces of a product number entered in an unbound text box (ProductNumber) and returns weights and lengths in unbound listboxes that are based on queries. The...
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.