473,809 Members | 2,791 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determine proper width of Label?

Hi,

A Windows form that I'm developing will have two controls: Label1 and
Label2. Both controls are of type Label (go figure). The text of Label1 will
not be known until runtime and may change at times. When the MyText property
of the containing form is set, I want to:

a) Set the Label1.Text property appropriately.
b) Change the width of Label1 so that it is exactly wide enough to show
the text.
c) Move Label2 so that its left edge is equal to Label1's right edge +
1.

Steps (a) and (c) are easy, but I'm not sure how to approach step (b).
Given a string, how do I set the width of Label1 to a value that is exactly
large enough to show the string?

David
Nov 15 '05 #1
2 2086
Hi David,

Labels have an AutoSize property.

Regards,
Fergus
Nov 15 '05 #2
Thanks, guys.. I had no idea that the Label.AutoSize property even
existed. I was just about to implement my own solution using
Graphics.Measur eString(), but clearly setting AutoSize to true is the way to
go. Thanks again!

David

"David Sworder" <ds******@cts.c om> wrote in message
news:%2******** **********@TK2M SFTNGP11.phx.gb l...
Hi,

A Windows form that I'm developing will have two controls: Label1 and
Label2. Both controls are of type Label (go figure). The text of Label1 will not be known until runtime and may change at times. When the MyText property of the containing form is set, I want to:

a) Set the Label1.Text property appropriately.
b) Change the width of Label1 so that it is exactly wide enough to show the text.
c) Move Label2 so that its left edge is equal to Label1's right edge +
1.

Steps (a) and (c) are easy, but I'm not sure how to approach step (b).
Given a string, how do I set the width of Label1 to a value that is exactly large enough to show the string?

David

Nov 15 '05 #3

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

Similar topics

2
3180
by: Alex Shi | last post by:
In php, is there a way obtain the width of a charactor of a certain font? Alex -- ================================================== Cell Phone Batteries at 30-50%+ off retail prices! http://www.pocellular.com ==================================================
8
33911
by: Mario T. Lanza | last post by:
I'm not sure what I'm doing wrong. I have a form that has mnay input fields. Before each input field is a label enclosed in custom LABEL tags. Inside my CSS I have: LABEL { width: 120px; }
9
19536
by: Adam | last post by:
Can someone please help!! I am trying to figure out what a font is? Assume I am working with a fixed font say Courier 10 point font Question 1: What does this mean 10 point font Question 2: How do I determine how many characters I can get on a line Question 3: How do I determine how many lines I can get on a page. Assume no margins Question 4: Does their exist some kind of refernce table that will equate a font with chars/line and...
3
3545
by: Andre | last post by:
Hi, Did someone know why this code does'nt work (The TableCell Width). The cell don't have 600 and 150 like writed in width="600" and width="150". <asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="false" ShowHeader="false" ShowFooter="false"> <Columns>
2
10408
by: Charleees | last post by:
Hi all, I have a DataGrid with Template Columns..... There are LAbels,Linkbuttons in the Single Row.. I have to set the Constant Column width for those Template Columns in Grid... Wat actually happens is... when the Text size is too big... the column
1
12313
by: Patrick Lioi | last post by:
Label label = new Label(); label.Width = 1; label.AutoSize = true; label.Text = "A long string, requiring a width larger than 1."; //At this point, Width is still 1. What am I missing? Shouldn't AutoSize adjust the "Size" "Auto"matically to fit the string?
5
11817
by: BobLaughland | last post by:
Hi There, I am trying to get some fields to align on a web page, like this, To: From: Subject: Each of the fields above have a corresponding asp:Textbox control next to it that I cannot get to lign up. I have this code,
7
2427
by: Chris Riesbeck | last post by:
What's the right way to make sure checkboxes stick with their labels when the window is resized? Right now I'm trying <label><input ...text</labelwith white-space:nowrap on label, but this only seems to work in Firefox and IE 7. In IE 8 and Opera I get one long line, as if the nowrap applied outside the label elements.
0
9601
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10635
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10378
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10115
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9198
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
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.