[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Autostretch lable or textbox

Posted on 2004-04-10
7
Medium Priority
?
523 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Accepted Solution

by:
caner_elci earned 540 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

649 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