当前位置: 移动技术网 > IT编程>开发语言>c# > 路径显示不下时,中间显示省略号

路径显示不下时,中间显示省略号

2019年08月26日  | 移动技术网IT编程  | 我要评论

开发环境:vs2012 c#

//路径显示不下时,中间显示省略号
class cshowshortpath
{
public cshowshortpath(string str)
{
//统一成反斜杠
str = str.replace('/', '\\');

//收集反斜杆的位置
list<int> indexs = new list<int>();
for (int i = 0; i < str.length; i++)
{
if ('\\' == str[i])
{
indexs.add(i);
}
}

//收集可能的显示形式
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<string> m_strcanshows = new list<string>();
private string getshortshow(ref string str, ref list<int> 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<int> indexs, int index)
{
return ( index >= 0 ) && ( index < indexs.count ) ;
}

};

下面的类,根据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;
}
}
}
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网