これはトリッキーな..です
一つの方法は、仕事をするためにTimer
を設定することです..:
Timer cursTimer = new Timer();
void cursTimer_Tick(object sender, EventArgs e)
{
int cp = txtExpresion.GetCharIndexFromPosition(
txtExpresion.PointToClient(Control.MousePosition));
txtExpresion.SelectionStart = cp;
txtExpresion.SelectionLength = 0;
txtExpresion.Refresh();
}
Timer
はControl.MousePosition
関数を使用してカーソル位置を25msごとに決定し、キャレットを設定してTextBox
を更新します。
イベントで初期化し、TextBox
にフォーカスがあることを確認してください。最後に、あなたは、現在の選択で文字列を追加します。
private void txtExpresion_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(string)))
{
e.Effect = DragDropEffects.Copy;
txtExpresion.Focus();
cursTimer = new Timer();
cursTimer.Interval = 25;
cursTimer.Tick += cursTimer_Tick;
cursTimer.Start();
}
}
private void txtExpresion_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(System.String)))
{
cursTimer.Stop();
string Item = (System.String)e.Data.GetData(typeof(System.String));
string[] split = Item.Split(':');
txtExpresion.SelectedText = split[1]
}
}
それを解決するための別の方法は、マウスイベント通常のドラッグ&ドロップとコードのみを使用しますが、この1つは[OK]を私の最初のテストを働いていないことであろう。
更新
上記溶液は仕事をしながら、Timer
を使用すると、正確にエレガントないようです。 Rezaの答えに見られるように、DragOver
イベントを使用するほうがはるかに優れています。カーソルをペイントするのではなく、実際のI-beamをコントロールしてみましょう。
それはかなりMousMove
のように動作しますので、DragOver
イベントが移動中にすべての時間と呼ばれます:だから、ここで私はそれを行うための最善の方法であると信じて2つのソリューションの合併である:
private void txtExpresion_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(System.String)))
{
string Item = (System.String)e.Data.GetData(typeof(System.String));
string[] split = Item.Split(':');
txtExpresion.SelectionLength = 0;
txtExpresion.SelectedText = split[1];
}
}
private void txtExpresion_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(string)))
{
e.Effect = DragDropEffects.Copy;
txtExpresion.Focus();
}
}
private void txtExpresion_DragOver(object sender, DragEventArgs e)
{
int cp = txtExpresion.GetCharIndexFromPosition(
txtExpresion.PointToClient(Control.MousePosition));
txtExpresion.SelectionStart = cp;
txtExpresion.Refresh();
}
出典
2016-08-11 19:43:39
TaW
私のオリジナルのと@レザのソリューションの最善をマージし、更新の答えを参照してください! – TaW