字符串应该在页面的右侧分割

时间:2014-04-25 16:41:12

标签: c# string split arrays

我有一个保存在字符串(datn1,datn2)中的路径,并希望在页面上打印路径。所以我必须看一下,如果字符串对于页面的宽度太长而且如果它太长,它应该以新行开头。

首先,我将字符串拆分并保存在字符串数组中:

String datn1 = dateiName1, datn2 = dateiName1;
char[] Trennzeichen = { '\\' };
String[] folders1 = datn1.Split(Trennzeichen);
String[] folders2 = datn2.Split(Trennzeichen); 

我还有变量页面的边距:

float leftMargin, rightMargin, topMargin, bottomMargin, width, height;

现在我想在字符串数组的某些部分添加一个新字符串,看看它是否在页面上。当它到达正确的站点时,它应该换行...我怎么能这样做? 我的想法:

string path_new;
for (int i = 0; i <= folders1.Length; i++)
{
   If()//How can i say that he should look if the string is inside the margins?
   {
      path_new= folders1[i]+"//";
   }
   else
   {
      path_new= "\n" + folders1[i]+ "//";
   }
}

1 个答案:

答案 0 :(得分:0)

现在我找到了解决方案:

 private void DrawGraphic(Graphics g) 
{                    
    g.PageUnit = GraphicsUnit.Millimeter;
    String datn1 = dateiName1, datn2 = dateiName2;
                char[] Trennzeichen = { '\\' };
                String[] folders1, folders2;
                if (vergl.X == 1)
                   folders1 = datn1.Split(Trennzeichen);
                else
                   folders1 = new String[0];
                if (vergl.Y == 1)
                   folders2 = datn2.Split(Trennzeichen);
                else
                   folders2 = new String[0];

                if (pageSetupDialog1.PageSettings.Landscape == false)
                {
                  string path_new = "";
                  string path_new2 = "";

                for (int i = 0; i < folders1.Length; i++)
                {
                 string path_temp = path_new + folders1[i] + "//";
                 System.Drawing.Size size_path_temp = TextRenderer.MeasureText(path_temp, new Font("Verdana", 8f));  // get size of string path_temp (in pixel)    
                 double size_path_temp_width = Convert.ToDouble(size_path_temp.Width); 
                 double variable = Convert.ToDouble(rightMargin.ToString())*96/25.4d; //96 = dpi Anzahl

                if (size_path_temp_width < variable)
                 {
                  path_new += folders1[i] + "//";
                 }
                 else
                 {
                  path_new += System.Environment.NewLine + folders1[i] + "//";
                 }
                }

            for (int i = 0; i < folders2.Length; i++)
            {
              string path_temp = path_new2 + folders2[i] + "//";
              System.Drawing.Size size_path_temp = TextRenderer.MeasureText(path_temp, new Font("Verdana", 8f));  // get size of string path_temp (in pixel)    
              double size_path_temp_width = Convert.ToDouble(size_path_temp.Width);
              double variable = Convert.ToDouble(rightMargin.ToString()) * 96 / 25.4d; //96 = dpi

         if (size_path_temp_width < variable)
         {
          path_new2 += folders2[i] + "//";
         }
         else
         {
          path_new2 += System.Environment.NewLine + folders2[i] + "//";
         }
        } 
 g.DrawString(path_new, new Font("Verdana", 8f), new SolidBrush(Color.Black), leftMargin, topMargin+10);
 g.DrawString(path_new2, new Font("Verdana", 8f), new SolidBrush(Color.Black), 20, topMargin + 10 + bmp1.Height / 6 + 25);
} 

dateiName1和dateiName2是保存路径的字符串。 vergl.X和vergl.Y检查是否在

中读取了路径