I am trying to perform some tests when testing on strings (I want to get the char index from offset x), but I am encountering problems with the dimension string.
This is essentially the code I use
StringFormat sf = new StringFormat(StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.NoWrap | StringFormatFlags.LineLimit);
e.Graphics.MeasureString("test string", this.Font, new SizeF(xHitTestPosition, this.Font.Height), sf, out charFitted, out linesFilled);
The charFitted value should be set to the number of characters that it can fit in the size it gives (I give it a size based on the point at which I am trying to hit the test).
This works fine until the area is large enough to hold the string "test". at this point, charFitted jumps from 3 ('tes') to 8 ('test'). It basically always includes all the spaces, regardless of the place that he gave.
I tried communicating with StringFormat settings but nothing helps ...
I have included a test application below that demonstrates this

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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
trackBar1.Maximum = this.ClientRectangle.Width;
}
protected override void OnPaint(PaintEventArgs e)
{
string sample = "abc def";
int charFitted, linesFilled;
e.Graphics.DrawString(sample, this.Font, Brushes.Black, PointF.Empty);
e.Graphics.DrawLine(Pens.Red, trackBar1.Value, 0, trackBar1.Value, 100);
StringFormat sf = new StringFormat(StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.NoWrap | StringFormatFlags.LineLimit);
sf.Trimming = StringTrimming.Character;
e.Graphics.MeasureString(sample, this.Font, new SizeF(trackBar1.Value, this.Font.Height), sf, out charFitted, out linesFilled);
textBox1.Text = "[" + sample.Substring(0, charFitted) + "]";
base.OnPaint(e);
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
Invalidate();
}
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.trackBar1 = new System.Windows.Forms.TrackBar();
this.textBox1 = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
this.SuspendLayout();
this.trackBar1.Location = new System.Drawing.Point(13, 184);
this.trackBar1.Name = "trackBar1";
this.trackBar1.Size = new System.Drawing.Size(259, 45);
this.trackBar1.TabIndex = 0;
this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
this.textBox1.Location = new System.Drawing.Point(22, 229);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(237, 20);
this.textBox1.TabIndex = 1;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.trackBar1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TrackBar trackBar1;
private System.Windows.Forms.TextBox textBox1;
}
}