[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 525
  • Last Modified:

Autostretch lable or textbox

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
keivan_b
Asked:
keivan_b
  • 3
  • 2
  • 2
1 Solution
 
caner_elciCommented:
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
 
AlexFMCommented:
Use DrawText API with DT_CALCRECT flag. To get hDC parameter use GetDC API.
0
 
caner_elciCommented:
Isn't this C# board???
0
Industry Leaders: 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!

 
AlexFMCommented:
Yes.
0
 
keivan_bAuthor Commented:
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
 
keivan_bAuthor Commented:
ok, i found a way to fix it!

MeasureString has an overload:
SizeF textSize = g.MeasureString(textBox1.Text, textBox1.Font,textBox1.Width);
0
 
caner_elciCommented:
Well, you could also not set the width of textBox1.. Anyway, I think it works well now?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now