Solved

Autostretch lable or textbox

Posted on 2004-04-10
7
512 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

12 Experts available now in Live!

Get 1:1 Help Now