473,387 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Dynamic Label height based on Content

Samishii23
246 100+
So I have a custom tool tip in my program. Its a Panel control that has a few Label controls. I have set my Panel to AutoSize based on content.

I tried to use the AutoSize property on one of the Labels, as this one holds the largest content, but that just extends its width. I would like to have it just expand height wise. The content I have ranges from a small sentence to a full paragraph. I haven't been able to find anything in searches. So now I ask the experts.

Should I use something like counting how many words are in the Panel, and scale it based on that? (I've tried this in the past, without good success) or is there a better way?
Mar 30 '10 #1

✓ answered by GaryTexmo

You should be able to measure the width/height of a string on a panel using the Graphics.MeasureString method. I'm not sure if that will handle word wrapping through... but worth a try! I'm guessing you are intending for your tool tip to eventually wrap the text after a certain point?

You can use the measure string method to find the total width and then divide that by the maximum width you want. I'm not sure if that takes into account he spacing between rows though.

9 6038
GaryTexmo
1,501 Expert 1GB
You should be able to measure the width/height of a string on a panel using the Graphics.MeasureString method. I'm not sure if that will handle word wrapping through... but worth a try! I'm guessing you are intending for your tool tip to eventually wrap the text after a certain point?

You can use the measure string method to find the total width and then divide that by the maximum width you want. I'm not sure if that takes into account he spacing between rows though.
Mar 30 '10 #2
tlhintoq
3,525 Expert 2GB
I'm a HUGE believer in building your own stuff.
I also believe that dismantling the code of those more experienced is a great way to learn.
And... I hate to re-invent the wheel.
Perhaps this project will help you in several ways.
http://www.codeproject.com/KB/dialog...ith_Image.aspx
Mar 30 '10 #3
Samishii23
246 100+
@Gary... I think you misunderstand. The only way I can get the text to wrap via height is to set the Height property of the Label. It wraps just fine.

@tlhintoq... Thanks for the link, I will check it out. I also agree with you about building my own stuff. Just need to learn more, and I'll be off to do so.
Mar 30 '10 #4
GaryTexmo
1,501 Expert 1GB
Sorry, then I don't understand what the problem is :(
Mar 30 '10 #5
Samishii23
246 100+
Sorry, then I don't understand what the problem is... you want a fixed width and want to set the height of the form so it fits all the text?
Thats pretty much exactly it.
Mar 30 '10 #6
GaryTexmo
1,501 Expert 1GB
ok, well that's what I was talking about :D Use MeasureString to get the width and height of the text. Divide the fixed width by the text width, multiply the height by that number, set your label to that number?
Mar 31 '10 #7
Samishii23
246 100+
@Gary, it seems that I can only use MeasureString with a draw event. I'm not using the ToolTip class. I'm just using a Panel that moves around based on which PictureBox the mouse is over. I don't actually use a Paint method. I only hide / show the Panel.

If you know of a way to still use it, or something else. I'd be glad to see it! :D

@tlhintoq. After reading through the source code of that nice custom tool tip project, I don't think it'll work for me. It calls for binding the ToolTip text to each button ( PictureBox for me ). That would be probably alot of over head on my program. Plus the tool tip has a couple bugs that I wouldn't know how to iron out. Lastly my Tool Tip text is dynamic, and will change alot. It looks like I would have to rebind the custom tool tip to the picture box every time I changed the text.

Thanks for the link is it is a cool project, and it'll defiantly be something i'll wanna learn from.
Apr 1 '10 #8
GaryTexmo
1,501 Expert 1GB
You should still be able to override the paint event on the panel and get the information you need :)

If you wanted, you could even use the DrawString method to draw your text too, but that's up to you I guess.
Apr 1 '10 #9
Samishii23
246 100+
Ok gary, thanks for the input, I finally was able to get around to implementing that Paint event idea of yours. The results aren't the best. But it works. :) Thanks for the help both of you!
Expand|Select|Wrap|Line Numbers
  1.         public void HandlePaint_ToolTip(object X, PaintEventArgs EX) {
  2.             SizeF SSize = new SizeF();
  3.             float TTHeight;
  4.  
  5.             SSize = EX.Graphics.MeasureString(ToolTipDisc.Text, ToolTipDisc.Font);
  6.             TTHeight = SSize.Width / (float)ToolTipDisc.Width + 1.5f;
  7.             ToolTipDisc.Height = ((int)SSize.Height * (int)TTHeight);
  8.             }
May 24 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: K | last post by:
I have found a script online that I want to use (I am new to PHP). It creates dynamic images based on the text that you pass it. However, no matter how I try, I can't get anything other than a...
7
by: Bil Muh | last post by:
Esteemede Developers, I would like to Thank All of You in advance for your sincere guidances. I am developing a software using Visual C++ .NET Standard Edition with Windows Form (.NET)...
1
by: Peter | last post by:
Hi, Does anyone know of how I could dynamically resize an iframe based on the content's height (for IE) so that no scrollbar appears for the iframe? The content will be from a different domain....
2
by: dawg1998 | last post by:
I have a page that creates dynamic textboxes based on the number of fields a user chooses to fill out. This process worked great when the page was standalone. However, when I move to a...
1
by: nsvmani | last post by:
Hi, i am trying to get the FileOpen dialogue window as soon as clicked href link I am using IE6 with ActiveX enabled. Just need to get the File Open dialogue window when i click on the HREF links.It...
1
by: Johanna | last post by:
I am a real php newbie, I am having to learn as I go along - no time for tutorials or books! Background to my problem: I am working on a Wordpress theme for my personal blog...
1
by: SunshineInTheRain | last post by:
The following code is dynamic create dropdownmenu which data within pulled from database However, the code work well on IE but not on Firefox. On Firefox, the whole mouseover and mouseout function...
6
by: Miro | last post by:
Is there a way to / or a mathematical formula to see if a font size is 'too big' for a label. I have a label that is docked to 'fill' a form, and I want to resize that font based on the width...
0
by: Czechtim | last post by:
Hello, I have problem with databinding. I created small application using structure that I need to demonstrate problem. I need to change content of label when changing content of property...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.