Solved

Autostretch lable or textbox

Posted on 2004-04-10
7
513 Views
Last Modified: 2008-01-09
i'm looking for a multiline textbox or lable that would change its height (Size.Y) property to avoid vertical scrollbars! while being a perfect fit (no waisted space at the bottom!).

or

a windows control or a way to create an auto strech text control! It could be a textbox label or even drawing text using GDI! All I want is to be able to tell what’s the triangle size covered by the text! So I can resize the window containing the text control to surround the text without waited space! It should support multiline text as well!

a good example would be a list view that that would resize its self to avoid scroll bars!
0
Comment
Question by:keivan_b
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Accepted Solution

by:
caner_elci earned 180 total points
ID: 10800131
There is a method of Graphics class, which gives exactly what you need: MeasureString(). As far as I know, it accepts multi-line strings too.. Just try something like this in textBox1's TextChange event:

Graphics g = Graphics.FromHwnd( textBox1.Handle );
SizeF textSize = g.MeasureString( textBox1.Text, textBox1.Font, 0, StringFormat.GenericDefault );
textBox1.Width = (int)textSize.Width;
textBox1.Height = (int)textSize.Height;

I think this will help you.. But you may need to do some optimizations, adding textbox borders etc...

Caner ELCI
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10801194
Use DrawText API with DT_CALCRECT flag. To get hDC parameter use GetDC API.
0
 
LVL 4

Expert Comment

by:caner_elci
ID: 10801201
Isn't this C# board???
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 48

Expert Comment

by:AlexFM
ID: 10801224
Yes.
0
 

Author Comment

by:keivan_b
ID: 10801824
caner_elci, i tried your answer but it doesn't seem to care about the height as it resizes the text box too a long one so all the text fit in one line!

this is my form load hanndler:

this.textBox1.Text="asdasdads asdadada  asdadada asdadada asdadada asdadada asdadada asdadada asdadada asdadada asdadada asdadada asdadada asdadada asdadada asdadada asdadada asdadada asdadada asdadada asdadada asdadada asdadada asdadada asdadada";

Graphics g = Graphics.FromHwnd( textBox1.Handle );
SizeF textSize = g.MeasureString( textBox1.Text, textBox1.Font, 0, StringFormat.GenericDefault );
textBox1.Width = (int)textSize.Width;
textBox1.Height = (int)textSize.Height;
0
 

Author Comment

by:keivan_b
ID: 10801852
ok, i found a way to fix it!

MeasureString has an overload:
SizeF textSize = g.MeasureString(textBox1.Text, textBox1.Font,textBox1.Width);
0
 
LVL 4

Expert Comment

by:caner_elci
ID: 10806399
Well, you could also not set the width of textBox1.. Anyway, I think it works well now?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now