Member Avatar for nssltd

Hello,

During development of one of my projects i have a tool strip bar when the user clicks a button the tool strip button = a URL of a website. but if the URL is a Google search page the text would be very long and you would only be able to display 2 URLs clearly.

I was wondering if there was such a way to have a maximum amount of characters available to use on a tool strip button, as there is on a text box.

I am using Microsoft Visual Studio 2008 Professional if that helps. In addition to that i am using it like this.

ToolStripButton b =
                          new ToolStripButton(el.InnerText, getFavicon(url), items_Click, el.GetAttribute("url"));
                b.ToolTipText = el.GetAttribute("url");
                b.Text = getCurrentBrowser().DocumentTitle.ToString();
                // Add a limit of characters to button here.
                b.MouseUp += new MouseEventHandler(b_MouseUp);
                linkBar.Items.Add(b);

Any help is much appreciated,

NSSLTD

Add this to your project:

public static class Methods {
    public static String Limit(this string s, int n) {
        return s.Substring(0, n > s.Length ? s.Length : n);
    }
}

This is an extension method that allows you to limit the length of a string. You can then just do this:

b.Text = getCurrentBrowser().DocumentTitle.ToString().Limit(50);
Member Avatar for nssltd

Right Okay ill give it go it's looking promising!

Member Avatar for nssltd

Hi ,

I have done as you said but i get this error message and i just dont understand it! Please may you assist me further with this?

Error	2	Extension methods must be defined in a top level static class; Methods is a nested class		44	38	PJSWebX

NSSLTD

You included the class declaration for Methods inside another class, something like this:

class MyClass {
    static class Methods {
 ...

The error is telling you that you can't do that, Methods needs to be a top level class.

Member Avatar for nssltd

Hi,

Sorry about the trouble i may be causing but i was wondering if you could help again? I have done as you asked and placed it here

//WebXplorer 2.0
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Xml;
using System.Net;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using System.Data;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Web;


namespace WBrowser
{

    public static class Methods
    {
        public static String Limit(this string s, int n)
        {
            return s.Substring(0, n > s.Length ? s.Length : n);

        }
    }

But when i build the application i get this error.

MissingManifestResourceException was Unhandle
Could not find any resources appropriate for the specified culture or the neutral culture.  Make sure "WBrowser.WBrowser.resources" was correctly embedded or linked into assembly "WBXplorer" at compile time, or that all the satellite assemblies required are loadable and fully signed.

Please help me further understand because i am completely baffled at the moment!

NSSLTD

p.s I have determined that this error only occurs when i place the code beneath the namespace area.

Member Avatar for nssltd

Hi i have solved the problem by placing your code in a different class.
Thanks for your help.

NSSLTD

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.