Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Only display the first 30 characters within a label

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

840 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