Solved

Only display the first 30 characters within a label

Posted on 2009-05-11
5
288 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

20 Experts available now in Live!

Get 1:1 Help Now