473,543 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding the width of a text

Finding the width of a text

I need to find the width of a text. When the user change the font in a
textbox I want the textbox to fit the text automatically by changing
txtMyTextBox.Wi dth according to the actual width of the text. It can also be
useful to know the actual height when using multiline textbox. Is this
possible?

TIRislaa
Nov 21 '05 #1
8 1422
me.CreateGraphi cs.MeasureStrin g(txtMyTextBox. Text,me.Font)

Eric Dreksler

"Tor Inge Rislaa" <no************ *@rislaa.no> wrote in message
news:Ua******** ***********@new s2.e.nsc.no...
Finding the width of a text

I need to find the width of a text. When the user change the font in a
textbox I want the textbox to fit the text automatically by changing
txtMyTextBox.Wi dth according to the actual width of the text. It can also
be
useful to know the actual height when using multiline textbox. Is this
possible?

TIRislaa

Nov 21 '05 #2
Thank you, works OK!

TIRislaa

"Eric Dreksler" <ericd AT accessoneinc DOT com> skrev i melding
news:uG******** ******@TK2MSFTN GP12.phx.gbl...
me.CreateGraphi cs.MeasureStrin g(txtMyTextBox. Text,me.Font)

Eric Dreksler

"Tor Inge Rislaa" <no************ *@rislaa.no> wrote in message
news:Ua******** ***********@new s2.e.nsc.no...
Finding the width of a text

I need to find the width of a text. When the user change the font in a
textbox I want the textbox to fit the text automatically by changing
txtMyTextBox.Wi dth according to the actual width of the text. It can also be
useful to know the actual height when using multiline textbox. Is this
possible?

TIRislaa


Nov 21 '05 #3
Eric & TIRislaa,
Remember to be very certain to Dispose of the Graphics object that
CreateGraphics return, other wise you can have a serious resource leak &
your program could stop working...

Dim gr As Graphics = Me.CreateGraphi cs()
Dim sz As Size
Try
sz = gr.MeasureStrin g(txtMyTextBox. Text,me.Font)
Finally
gr.Dispose()
End Try

VS.NET 2005 (aka Whidbey due out later in 2005)
http://lab.msdn.microsoft.com/vs2005/ will include a Using statement to
simplify the above:

Dim sz As Size
Using gr As Graphics = Me.CreateGraphi cs()
sz = gr.MeasureStrin g(txtMyTextBox. Text,me.Font)
End Using

Hope this helps
Jay

"Eric Dreksler" <ericd AT accessoneinc DOT com> wrote in message
news:uG******** ******@TK2MSFTN GP12.phx.gbl...
me.CreateGraphi cs.MeasureStrin g(txtMyTextBox. Text,me.Font)

Eric Dreksler

"Tor Inge Rislaa" <no************ *@rislaa.no> wrote in message
news:Ua******** ***********@new s2.e.nsc.no...
Finding the width of a text

I need to find the width of a text. When the user change the font in a
textbox I want the textbox to fit the text automatically by changing
txtMyTextBox.Wi dth according to the actual width of the text. It can also
be
useful to know the actual height when using multiline textbox. Is this
possible?

TIRislaa


Nov 21 '05 #4
Do you then have to still dispose gr or has M'soft figured out yet how to
dispose of objects with user intervention?

"Jay B. Harlow [MVP - Outlook]" wrote:
Eric & TIRislaa,
Remember to be very certain to Dispose of the Graphics object that
CreateGraphics return, other wise you can have a serious resource leak &
your program could stop working...

Dim gr As Graphics = Me.CreateGraphi cs()
Dim sz As Size
Try
sz = gr.MeasureStrin g(txtMyTextBox. Text,me.Font)
Finally
gr.Dispose()
End Try

VS.NET 2005 (aka Whidbey due out later in 2005)
http://lab.msdn.microsoft.com/vs2005/ will include a Using statement to
simplify the above:

Dim sz As Size
Using gr As Graphics = Me.CreateGraphi cs()
sz = gr.MeasureStrin g(txtMyTextBox. Text,me.Font)
End Using

Hope this helps
Jay

"Eric Dreksler" <ericd AT accessoneinc DOT com> wrote in message
news:uG******** ******@TK2MSFTN GP12.phx.gbl...
me.CreateGraphi cs.MeasureStrin g(txtMyTextBox. Text,me.Font)

Eric Dreksler

"Tor Inge Rislaa" <no************ *@rislaa.no> wrote in message
news:Ua******** ***********@new s2.e.nsc.no...
Finding the width of a text

I need to find the width of a text. When the user change the font in a
textbox I want the textbox to fit the text automatically by changing
txtMyTextBox.Wi dth according to the actual width of the text. It can also
be
useful to know the actual height when using multiline textbox. Is this
possible?

TIRislaa



Nov 21 '05 #5
Dennis,
Do you then have to still dispose gr or has M'soft figured out yet how to
dispose of objects with user intervention? I suspect you meant "without user intervention".
The Using statement does the Dispose for you. It allows for "cleaner" code,
most of the time "cleaner" code is simpler code.

It is effectively the Try Finally statement, with a check for the variable
being Nothing. My sample really should have checked for Nothing, however due
to the way it was written, it was "not necessary" as the Try would not be
entered if the "Me.CreateGraph ics" throws an exception, hence I left it
off... I'm not sure (nor am I worried) if the End Using eliminates the check
for Nothing, when it deems it unnecessary... Including it is necessary to
avoid NullReferenceEx ceptions on the call to Dispose.
If you look at the IL for a Using statement you would see something very
similar to:

Using gr As Graphics = Me.CreateGraphi cs()
Dim gr As Graphics = Me.CreateGraphi cs()
Try
End Using Finally
If Not gr Is Nothing Then
gr.Dispose()
End If
End Try


Hope this helps
Jay

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:E0******** *************** ***********@mic rosoft.com... Do you then have to still dispose gr or has M'soft figured out yet how to
dispose of objects with user intervention?

"Jay B. Harlow [MVP - Outlook]" wrote:
Eric & TIRislaa,
Remember to be very certain to Dispose of the Graphics object that
CreateGraphics return, other wise you can have a serious resource leak &
your program could stop working...

Dim gr As Graphics = Me.CreateGraphi cs()
Dim sz As Size
Try
sz = gr.MeasureStrin g(txtMyTextBox. Text,me.Font)
Finally
gr.Dispose()
End Try

VS.NET 2005 (aka Whidbey due out later in 2005)
http://lab.msdn.microsoft.com/vs2005/ will include a Using statement to
simplify the above:

Dim sz As Size
Using gr As Graphics = Me.CreateGraphi cs()
sz = gr.MeasureStrin g(txtMyTextBox. Text,me.Font)
End Using

Hope this helps
Jay

"Eric Dreksler" <ericd AT accessoneinc DOT com> wrote in message
news:uG******** ******@TK2MSFTN GP12.phx.gbl...
> me.CreateGraphi cs.MeasureStrin g(txtMyTextBox. Text,me.Font)
>
> Eric Dreksler
>
> "Tor Inge Rislaa" <no************ *@rislaa.no> wrote in message
> news:Ua******** ***********@new s2.e.nsc.no...
>> Finding the width of a text
>>
>> I need to find the width of a text. When the user change the font in a
>> textbox I want the textbox to fit the text automatically by changing
>> txtMyTextBox.Wi dth according to the actual width of the text. It can
>> also
>> be
>> useful to know the actual height when using multiline textbox. Is this
>> possible?
>>
>> TIRislaa
>>
>>
>
>


Nov 21 '05 #6
> Dim gr As Graphics = Me.CreateGraphi cs()
Dim sz As Size
Try
sz = gr.MeasureStrin g(txtMyTextBox. Text,me.Font)
Finally
gr.Dispose()
End Try


Ok, how to know if it is managed or not,
that is Framework will garbage collect,
or it must be disposed in code manually?
Thanks,
Roger

Nov 21 '05 #7
Roger,
Generally when using a type for the first time I read the documentation for
that type to see if there are "special" requirements such as calling Dispose
on it.

A good general (very general) rule of thumb to follow is if it implements
IDisposable, call Dispose. The "problem" with this rule is if you inherit
from System.Componen tModel.Componen t, then you have implemented IDisposable
& Dispose may or may not be needed. Of course calling Dispose when it is not
needed is "better" then not calling it when it is needed!!

Note: implementing IComponent directly, rather then inheriting from
Component would allow you to exclude the IDisposable itself!

Hope this helps
Jay
"Roger" <ro***@pcsreven uecontrol.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Dim gr As Graphics = Me.CreateGraphi cs()
Dim sz As Size
Try
sz = gr.MeasureStrin g(txtMyTextBox. Text,me.Font)
Finally
gr.Dispose()
End Try


Ok, how to know if it is managed or not,
that is Framework will garbage collect,
or it must be disposed in code manually?
Thanks,
Roger


Nov 21 '05 #8
> Generally when using a type for the first time I read the documentation
for
Rule # 1.
Hope this helps
Jay


Thanks, Jay, it was.
Roger
Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
13770
by: Noam Dekers | last post by:
Hi all, I would like to find a word stored in a text file. Structure: I have one file named keyWords.txt that stores some key words I'm interested in finding. In addition I also have a file named textOrigin.txt in which I store the text to search in. I would like my prog to check if a certain word appears in the text and than to tell me...
5
3188
by: Matthew Robinson | last post by:
Can anybody see what is wrong with this code? apparently i have an 'empty delimiter' on the line that sets $pos - can anybody see what is wrong with it? $search_for = $_GET; $the_items_description = $row ; $pos = stristr($the_items_description, $search_for); if ($search_for == "") {
2
2206
by: TadPole | last post by:
Hi all, My main problems are::::::::: 1. Set a value within a block container that can be used and changed by subsequent templates/block-containers/tables etc.. 2. get/determine/find the setting that tell the process that the new top of the document region is now at the end of the last block-container used, this must be set in that last...
1
3155
by: Doug | last post by:
The html below shows DataList "DiscountList" nested within DataList "EventItemList". DiscountList contains a Label control. I'm trying to find the label, using FindControl, during EventList_ItemCreated (below the html), but it's always <undefined value> (null). Everything else works fine. Eventually I need to set the value of the label...
4
1517
by: News | last post by:
I have a page with many controls. Among these controls there is a table which is a datagrid with nested repeater inside. My problem is that I can not use DataGridCommandEventArgs to get datagrid elements because submit button is not in datagrid and uses onClick event btnSaveServicePlanUpdate. Here is the code: Sub...
10
1738
by: tshad | last post by:
I have a Datagrid with a column: <asp:HyperLinkColumn DataTextField="JobTitle" DataNavigateUrlField="PositionID" DataNavigateUrlFormatString="AddNewPositions.aspx?PositionID={0}" HeaderText="Job Title" Visible="True" SortExpression="JobTitle"/>
2
2149
by: Oenone | last post by:
I'm trying to create a form similar to a VB messagebox but with custom content. One of the things I want to be able to do is display some text in a label at the top of the messagebox form and have the form automatically expand to make room for it (which could be just a few words, or several paragraphs). In VB6 I used to use a useful trick...
1
1396
by: hyperian | last post by:
Hey, I've hit a problem and haven't had much luck in finding a solution. I have DIV tags that enclose text of variable length. This text is entered by the user, so it could be anything. I want to find the width of these DIV tags that contain this text, but nothing I have tried works. I've tried .width, and .offsetWidth, but with no luck. ...
15
1949
by: rhino | last post by:
I've put together a prototype of two-tiered CSS tabs that works really well in IE6, IE7, and FF2. It also works very well in Opera 9.27 _except_ that the placement of the lower tier of tabs is messed up. Both the XHTML and CSS validate without any errors or warnings. Can anyone help me figure out what is wrong? I'd be especially interested...
0
7399
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
7729
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...
0
7677
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...
0
5880
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4884
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3385
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
3386
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1809
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
0
626
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.