Click here to Skip to main content
15,888,803 members
Articles / Programming Languages / C#

Customizable Drag-drop ToolStrip

Rate me:
Please Sign up or sign in to vote.
4.90/5 (7 votes)
5 Dec 2011CPOL3 min read 32.7K   2.8K   29  
.NET ToolStrip allows drag and drop customizaton
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ToolStripCustomPlus
{
    class UniqueString
    {
        public UniqueString(string sOriginal)
        {
            original = sOriginal;
            existings = new List<string>();
        }

        public UniqueString(string sOriginal, string[] Existings)
        {
            original = sOriginal; 
            existings.AddRange(Existings);
        }

        public void AddExisting(string sExisting)
        {
            existings.Add(sExisting);
        }

        public bool GetUniqueString(out string sUnique)
        {
            if (existings.Contains(original))
            {
                sUnique = original;
                return false;
            }  
            
            //remove brackets and contents
            string originalNoBracketSub = original;
            int nIndexOfLastOpenBracket = original.LastIndexOf("(");
            int nIndexOfLastCloseBracket = original.LastIndexOf(")");

            if ((nIndexOfLastOpenBracket != -1) &&
                (nIndexOfLastCloseBracket != -1) &&
                (nIndexOfLastOpenBracket < nIndexOfLastCloseBracket))
            {
                originalNoBracketSub = original.Remove(nIndexOfLastOpenBracket);
            }

            if (existings.Contains(originalNoBracketSub))
            {
                sUnique = originalNoBracketSub;
                return true;
            }  
            
            int nCopyNum = 2;
            while (existings.Contains(originalNoBracketSub + "(" + nCopyNum.ToString() + ")"))
            {
                nCopyNum++;
            }
            sUnique = originalNoBracketSub + "(" + nCopyNum.ToString() + ")";
            return true;
        }

        private string original;
        private List<string> existings;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions