0

Hi ...
i hope you can help me finding a solution for my problem ...
i have a long text i want to paint draw into a Rectangle ...
i dont want to draw all the text, so i am trimming it ...
it`s working and everything is fine ...
but

1)how do i get the rest of the string now ?
and
2) how can i check if the text really trimmed or it did fit in the rectangle without trimming it ?

hope my problem is clear..
iam using VS2008, framework 3.5

thanks in advance

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by sknake
0

That depends a lot on how you're painting the rectangle. Here is a sort-of-generic way to measure given a control.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmDrawText : Form
  {
    public frmDrawText()
    {
      InitializeComponent();
    }

    private static Label GetLabel()
    {
      Label lbl = new Label();
      lbl.AutoSize = false;
      lbl.Location = new Point(0, 0);
      lbl.Size = new Size(100, 100);
      return lbl;
    }

    private static string GetReallyLongString()
    {
      StringBuilder sb = new StringBuilder();
      for (int i1 = 0; i1 < 5; i1++)
        sb.Append(Guid.NewGuid().ToString());
      return sb.ToString();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      Label lbl = GetLabel();
      this.Controls.Add(lbl);
      string reallyLongString = GetReallyLongString();
      DrawStringResult res = GetChopInfo(lbl, reallyLongString);
      lbl.Text = res.Displayed;
      
      StringBuilder sb = new StringBuilder();
      sb.AppendLine("Original string: " + reallyLongString);
      sb.AppendLine("Displayed      : " + res.Displayed);
      sb.AppendLine("Trimmed        : " + res.Trimmed);
      MessageBox.Show(sb.ToString().Trim());
    }

    private static DrawStringResult GetChopInfo(Control c, string s)
    {
      if (string.IsNullOrEmpty(s) || s.Length.Equals(0))
        return new DrawStringResult();

      using (Graphics g = c.CreateGraphics())
      {
        int pos = s.Length;
        while (pos > 0)
        {
          string txt = s.Substring(0, pos);
          SizeF sz = g.MeasureString(txt, c.Font);
          if (sz.Width > c.Width) //text is wider than our control. Time to chop!
            pos--;
          else
            break;
        }
        DrawStringResult res = new DrawStringResult();
        res.Displayed = s.Substring(0, pos);
        res.Trimmed = s.Substring(pos, s.Length - pos);
        return res;
      }
    }
  }
  public class DrawStringResult
  {
    public string Displayed { get; set; }
    public string Trimmed { get; set; }
    /// <summary>
    /// True if the entire string was shown, false if the text was trimmed
    /// </summary>
    public bool EntireStringShown
    {
      get { return string.IsNullOrEmpty(Trimmed); }
    }
    public DrawStringResult()
    {
      this.Displayed = string.Empty;
      this.Trimmed = string.Empty;
    }
    public DrawStringResult(string Displayed, string Trimmed)
    {
      this.Displayed = Displayed;
      this.Trimmed = Trimmed;
    }
  }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.