Solved

Only display the first 30 characters within a label

Posted on 2009-05-11
5
286 Views
Last Modified: 2012-05-06
Hello,

I have a label that displays data from a database.  I would like to add some code which would only display the first 30 characters and then at the end it would have a link that would open a message box and display all the text.

Is something like this easy to create?

Thanks.
0
Comment
Question by:andyw27
5 Comments
 
LVL 3

Expert Comment

by:mpf1748
ID: 24358475
You could very easily call <string>.Substring(0,30) to display the first 30 characters if your string is over 30 characters. You can then add a ToolTip object onto your form at design time, which will then expose a property on your label to set the ToolTipText, so that the full text will display on hover. You can call the ToolTipText property within code.
0
 
LVL 3

Expert Comment

by:mpf1748
ID: 24358491
I missed the message box. You could also show/hide a LinkLabel or a Button that will show the full text in a MessageBox. You could also make the original label a LinkLabel as well.
0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24358516
Yes. Let's say you've a label named 'lblName'
in the code behind do the following
if (<yourtext>.Length > 30)
{
lblName.Text = Left(<text>, 30) + "<a href='javascript: alert(\"" + <text+ "\")'>Click here to view the full name</a>";
}
else
{
lblName.Text =<text>;
}
0
 
LVL 9

Accepted Solution

by:
tillgeffken earned 500 total points
ID: 24358536
First 30 Characters will cut off words. In code is a function to get the first x words. Your choice.

label1.Text = FirstWords(myString, 10)

If you insist on using first 30 chars try:

label1.Text = myString.Substring(0, 29);

for the popup i don't know.. is this a ASP.Net webform or Windows Forms Application?

    public static string FirstWords(string input, int numberWords)

    {

        try

        {

            int words = numberWords;

            for (int i = 0; i < input.Length; i++)

            {

                if (input[i] == ' ')

                {

                    words--;

                }

                if (words == 0)

                {

                    return input.Substring(0, i);

                }

            }

        }

Open in new window

0
 

Author Comment

by:andyw27
ID: 24358707
Its a windows application
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to get a file from SqlServer DB 4 29
.NET Error 7 42
C# Error - Add Failed 12 52
How to Get total minutes between two hours 4 27
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 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

13 Experts available now in Live!

Get 1:1 Help Now