2011-09-30 13 views
17

TextBoxに貼り付ける内容をどのように動的に変更できますか?ここでテキストボックスの貼り付け内容を変更します

は、私がイベントをサブスクライブする方法である:ここでは

DataObject.AddPastingHandler (uiTextBox, TextBoxPaste); 

は、私は、イベントハンドラを定義する方法である:私はVB.netにかなりのビットを使用し

private void TextBoxPaste (object sender, DataObjectPastingEventArgs args) 
{ 
    string clipboard = args.DataObject.GetData (typeof (string)) as string; 

    Regex nonNumeric = new System.Text.RegularExpressions.Regex (@"\D"); 
    string result = nonNumeric.Replace (clipboard, String.Empty); 

    // I can't just do "args.DataObject.SetData (result)" here. 
} 

答えて

15

私は2つの方法が考えられますが、いずれも非常に魅力的ではありません:)そして、両方の方法では、貼り付けコマンドを取り消すことが含まれます。

最初の方法は、貼り付けコマンドをキャンセルしてから、resultが貼り付けられた場合に貼り付け後のテキストがどのようになるかを計算することです。

private void TextBoxPaste(object sender, DataObjectPastingEventArgs args) 
{ 
    string clipboard = args.DataObject.GetData(typeof(string)) as string; 

    Regex nonNumeric = new System.Text.RegularExpressions.Regex(@"\D"); 
    string result = nonNumeric.Replace(clipboard, String.Empty); 

    int start = uiTextBox.SelectionStart; 
    int length = uiTextBox.SelectionLength; 
    int caret = uiTextBox.CaretIndex; 

    string text = uiTextBox.Text.Substring(0, start); 
    text += uiTextBox.Text.Substring(start + length); 

    string newText = text.Substring(0, uiTextBox.CaretIndex) + result; 
    newText += text.Substring(caret); 
    uiTextBox.Text = newText; 
    uiTextBox.CaretIndex = caret + result.Length; 

    args.CancelCommand(); 
} 

もう1つの方法は、貼り付けコマンドをキャンセルし、クリップボード内のテキストを変更してから貼り付けを再実行することです。これはまた、実際の貼り付けコマンドと手動で呼び出された貼り付けコマンドとで異なる必要があります。このようなもの

bool m_modifiedPaste = false; 
private void TextBoxPaste(object sender, DataObjectPastingEventArgs args) 
{ 
    if (m_modifiedPaste == false) 
    { 
     m_modifiedPaste = true; 
     string clipboard = args.DataObject.GetData(typeof(string)) as string; 

     Regex nonNumeric = new System.Text.RegularExpressions.Regex(@"\D"); 
     string result = nonNumeric.Replace(clipboard, String.Empty); 

     args.CancelCommand(); 

     Clipboard.SetData(DataFormats.Text, result); 
     ApplicationCommands.Paste.Execute(result, uiTextBox); 
    } 
    else 
    { 
     m_modifiedPaste = false; 
    } 
} 
+0

私はまた、あなたの最初の方法のラインに沿って考えていました。 2番目の方法は、クリップボードの内容を変更するのでかなり悪化します。いずれにせよ、これはまさに私が探していたものです、ありがとう! – Dave

+0

答えをありがとう。私はちょうど最初のメソッド(9行から3行)の短いバージョンを投稿したい: 'int caret = uiTextBox.CaretIndex; uiTextBox.Text = uiTextBox.Text.Insert(uiTextBox.SelectionStart、result); uiTextBox.CaretIndex =キャレット+ result.Length; ' – newman

+0

送信者は、ペーストイベントが発生しているテキストボックスです。特定のテキストボックスを明示的に参照するのではなく、送信者を' TextBox'(または何でも) 。 – Will

0

を、私はこれをテストしてみました私はラメだから、私はコントロールがフォーカスを取得した新しいテキストにクリップボードを設定:)

string oClipboard; 

    private void TextBox1_GotFocus(object sender, System.EventArgs e) 
    { 
     oClipboard = Clipboard.GetText(); 
     Clipboard.SetText("foo"); 
    } 

    private void TextBox1_LostFocus(object sender, System.EventArgs e) 
    { 
     Clipboard.SetText(oClipboard); 
    } 

C#ビットは、私がコンバータを使用しました。古い値が保存されます。後でコントロールがフォーカスを失うと、クリップボードは古い値に戻されます。

23

DataObjectがフリーズしているため、args.DataObject.SetData( "some data")を呼び出すことはできません。

private void TextBoxPaste(object sender, DataObjectPastingEventArgs e) { 
     string text = (String)e.DataObject.GetData(typeof(String)); 
     DataObject d = new DataObject(); 
     d.SetData(DataFormats.Text, text.Replace(Environment.NewLine, " ")); 
     e.DataObject = d; 
} 
+2

これははるかに洗練されたソリューションです –

0

@フレドリックのコードのほんの一部の修正、私は彼の方法の両方を試してきたので、:何ができることは全くのDataObjectを交換です。

最初のものは、単に短縮版

private void TextBox_Pasting(object sender, DataObjectPastingEventArgs e) 
{ 
    string clipboard = e.DataObject.GetData(typeof(string)) as string; 
    Regex nonNumeric = new System.Text.RegularExpressions.Regex (@"\D"); 
    string result = nonNumeric.Replace(clipboard, string.Empty); 

    int caret = CaretIndex; 
    Text = Text.Substring(0, SelectionStart) + result + 
     Text.Substring(SelectionStart + SelectionLength); 
    CaretIndex = caret + result.Length; 

    e.CancelCommand(); 
} 

で、もう一つは、私はなぜそれがある、私のカスタムTextBoxコントロールの中にあるものを使用していたクリップボードの内容

private string oldClipboardContent { get; set; } = ""; 
private bool pasteModified { get; set; } = false; 

private void TextBox_Pasting(object sender, DataObjectPastingEventArgs e) 
{ 
    if (pasteModified) 
    { 
     pasteModified = false; 
    } 
    else 
    { 
     pasteModified = true; 

     string text = (string)e.DataObject.GetData(typeof(string)); 
     oldClipboardContent = text; 

     Regex nonNumeric = new System.Text.RegularExpressions.Regex (@"\D"); 
     text = nonNumeric.Replace(text, string.Empty); 
     e.CancelCommand(); 

     Clipboard.SetData(DataFormats.Text, text); 
     ApplicationCommands.Paste.Execute(text, this); 

     Clipboard.SetData(DataFormats.Text, OldClipboardContent); 
     oldClipboardContent = ""; 
    } 
} 

を保ちながら更新されます最初に名前を書かずにTextBoxのプロパティにアクセスすることができます。

関連する問題