Solved

Only display the first 30 characters within a label

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
linq, c# 8 37
c# ftp code 3 35
asp.net repeater 2 17
Fastest way to get file count in MS Server 2012 3 13
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

730 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