這個問題來自論壇。
第一反應是SelectionStart屬性,結果發現在分方向選擇內容時返回錯誤值,真正答案如下:
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
namespace WindowsApplication5
...{
public partial class Form1 : Form
...{
public Form1()
...{
InitializeComponent();
}
[DllImport("user32.dll")]
private static extern bool GetCaretPos(out Point ppt);
private void Form1_Load(object sender, EventArgs e)
...{
this.textBox1.Text = "jinjazz";
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
...{
Point p;
GetCaretPos(out p);
int i=this.textBox1.GetCharIndexFromPosition(p);
this.Text = i.ToString();
}
}
}