更改文本框粘贴内容 [英] Change paste contents in Textbox

查看:126
本文介绍了更改文本框粘贴内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何动态地改变所将在文本框中粘贴的内容。

下面是我如何订阅事件:

  DataObject.AddPastingHandler(uiTextBox,TextBoxPaste);
 

下面是我如何定义事件处理程序:

 私人无效TextBoxPaste(对象发件人,DataObjectPastingEventArgs参数)
{
    串剪贴板= args.DataObject.GetData(typeof运算(字符串))的字符串;

    正则表达式非数值=新System.Text.RegularEx pressions.Regex(@\ D);
    字符串结果= nonNumeric.Replace(剪贴板的String.Empty);

    //我不能只是做args.DataObject.SetData(结果)在这里。
}
 

解决方案

我能想到的两种方式,其中没有一个是非常有吸引力:)而两种方式包括取消粘贴命令。

第一种方式是将取消粘贴命令,然后计算文本看起来像浆糊后,如果结果粘贴来代替。

 私人无效TextBoxPaste(对象发件人,DataObjectPastingEventArgs参数)
{
    串剪贴板= args.DataObject.GetData(typeof运算(字符串))的字符串;

    正则表达式非数值=新System.Text.RegularEx pressions.Regex(@\ D);
    字符串结果= nonNumeric.Replace(剪贴板的String.Empty);

    INT开始= uiTextBox.SelectionStart;
    INT长度= uiTextBox.SelectionLength;
    INT插入符= uiTextBox.CaretIndex;

    字符串文本= uiTextBox.Text.Substring(0开始);
    文字+ = uiTextBox.Text.Substring(起始+长度);

    字符串newText = text.Substring(0,uiTextBox.CaretIndex)+结果;
    newText + = text.Substring(尖);
    uiTextBox.Text = newText;
    uiTextBox.CaretIndex =插入符号+ result.Length;

    args.CancelCommand();
}
 

另一种方法是将取消粘贴命令,更改文本在剪贴板中,然后重新执行粘贴。这也需要你真正的粘贴命令和手动调用粘贴命令之间的不同。事情是这样的。

 布尔m_modifiedPaste = FALSE;
私人无效TextBoxPaste(对象发件人,DataObjectPastingEventArgs参数)
{
    如果(m_modifiedPaste ==假)
    {
        m_modifiedPaste = TRUE;
        串剪贴板= args.DataObject.GetData(typeof运算(字符串))的字符串;

        正则表达式非数值=新System.Text.RegularEx pressions.Regex(@\ D);
        字符串结果= nonNumeric.Replace(剪贴板的String.Empty);

        args.CancelCommand();

        Clipboard.SetData(DataFormats.Text,结果);
        ApplicationCommands.Paste.Execute(结果,uiTextBox);
    }
    其他
    {
        m_modifiedPaste = FALSE;
    }
}
 

How can I dynamically change the contents of what will be pasted in the TextBox.

Here is how I subscribe to the event:

DataObject.AddPastingHandler (uiTextBox, TextBoxPaste);

Here is how I define the event handler:

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.
}

解决方案

I can think of two ways, none of which are very attractive :) And both ways include canceling the paste command.

The first way would be to cancel the paste command and then calculate what the text would look like after the paste if result was pasted instead.

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();
}

The other way would be to cancel the paste command, change the text in the Clipboard and then re-execute paste. This would also require you to differ between the real paste command and the manually invoked paste command. Something like this

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;
    }
}

这篇关于更改文本框粘贴内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆