Solved

Autostretch lable or textbox

Posted on 2004-04-10
7
516 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 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

733 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