When the path is not displayed, the ellipsis sign is displayed in the middle

Time:2019-10-8

Development environment: VS2012 C#

// When the path is not displayed, the ellipsis sign is displayed in the middle
class CShowShortPath
{
public CShowShortPath(string str)
{
// Unify into backslash
str = str.Replace(‘/’, ‘\\’);

// Collect the position of backslash rod
List indexs = new List();
for (int i = 0; i < str.Length; i++)
{
if (‘\\’ == str[i])
{
indexs.Add(i);
}
}

// Collect possible display forms
m_strCanShows.Add(str);
for (int j = indexs.Count / 2, i = j – 1; ; )
{
m_strCanShows.Add(GetShortShow(ref str, ref indexs, i, j));
if ((!ValidIndex(indexs,i)) && (!ValidIndex(indexs,j)) )
{
break;
}
if ((indexs.Count – 1 – j) > (i – 0))
{
j++;
}
else
{
i–;
}
}

}
public List m_strCanShows = new List();
private string GetShortShow(ref string str, ref List indexs, int indexLeft, int indexRight)
{
string str1 = “”, str2 = “”;
if (ValidIndex(indexs,indexLeft))
{
str1 = str.Substring(0, indexs[indexLeft]);
}
if (ValidIndex(indexs,indexRight))
{
str2 = str.Substring(indexs[indexRight] + 1, str.Length – indexs[indexRight] – 1);
}
return str1 + “…” + str2;
}

private bool ValidIndex( List indexs, int index)
{
return ( index >= 0 ) && ( index < indexs.Count ) ;
}

};

The following class displays the file path according to the width of TextBox:
public class CShow
{
public static void ShowSinglePathIfNoSpace(string strPath, TextBox txtBox)
{
ImeBase.CShowShortPath show = new ImeBase.CShowShortPath(strPath);
for (int i = 0; i < show.m_strCanShows.Count; i++)
{
int iNeedWidth = TextRenderer.MeasureText(show.m_strCanShows[i], txtBox.Font).Width;
if (txtBox.Width > iNeedWidth)
{
txtBox.Text = show.m_strCanShows[i];
break;
}
}
}
}

Recommended Today

Details of multi-path and large capacity hard disk mount under CentOS

I. application environment and requirementsBlade servers connect HP storage through fiber switches, forming a 2×2 link The storage capacity of the operating system for CentOS 6.4 64 bit mount is 2.5t Based on this application environment, two problems need to be solved: In order to ensure the stability and transmission performance of the link, multi-path […]