?
Solved

Only display the first 30 characters within a label

Posted on 2009-05-11
5
Medium Priority
?
295 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
[X]
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
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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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 introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

771 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