选择在文本框,只需一次点击的抽头上字 [英] Selecting the tapped-on word on a single click in textbox

查看:112
本文介绍了选择在文本框,只需一次点击的抽头上字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个Windows Phone 7的应用程序。我正好有许多文本框取值堆放在整个文本框供选择的ItemsControl 和行为是不统一的,即单点击任何单词在任何文本框不会选择抽头字。首先点击消耗为重点的文本框,然后另一个实际选择的话。但一旦文本框具有焦点,它是一个单一的点击来选择任意单词,直到用户希望在另一个文本框中选择一些其他的字。有没有一种办法,以消除呢?可能是通过提高假鼠标左键向下和向上在GotFocus事件事件?

In a Windows Phone 7 app. I happen to have many TextBoxs stacked in a ItemsControl and the behaviour across textboxes for selection is not uniform i.e. a single click on any word in any text box does not select the tapped word. First a click is consumed for focusing the text box and then another to actually select the word; but once the text box has focus, it's a single click to select any word within, until the user wants to select some other word in another textbox. Is there a way to neutralize this? May be by raising fake mouse left button down and up events on a GotFocus event?

我所做的是,在一个LeftMouseButtonDown(及以上)事件我存储事件参数。在一个的GotFocus,我试图与存储args设置为引发事件的,而是用来引发事件的事件处理程序变种总是空的,因此提高事件不会发生。我是新的C#,所以我不知道在哪里,我题外话。

What I did was, on a LeftMouseButtonDown (and up) Event I stored the event args. On a GotFocus, I tried to raise an event with the stored args, but the event handler var used to raise the event always is null, hence raise event doesn't happen. I'm new to C# so I'm not sure where I'm digressing.

推荐答案

只是找到了一个巧妙的方法!在它变得和的GotFocus 日常使用集中TextBox控件的一个水龙头 SelectionStart 文本框的财产可以得到当前字符具有插入符之前它。有了这些数据与空格字符的左,右边界,可以发现,因而选择了字。

Just found a neat trick! On a single tap of a TextBox control it gets focus and on GotFocus routine using SelectionStart property of TextBox one can get the current character which has the caret just before it. With this data the left and right boundaries with space character can be found and thus the word selected.

    private void textBox_GotFocus(object sender, RoutedEventArgs e)
    {
        TextBox txtBox = (TextBox)sender;
        char [] strDataAsChars = txtBox.Text.ToCharArray();
        int i = 0;
        for (i = txtBox.SelectionStart - 1; ((i >= 0) &&
                           (strDataAsChars[i] != ' ')); --i) ;
        int selBegin = i + 1;
        for (i = txtBox.SelectionStart; ((i < strDataAsChars.Length) &&
                                          (strDataAsChars[i] != ' ')); ++i) ;
        int selEnd = i;
        txtBox.Select(selBegin, selEnd - selBegin);
    }



在这里发布,以便它可以帮助别人以后。

Posted it here so that it may help someone later on.

这篇关于选择在文本框,只需一次点击的抽头上字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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