如何将焦点赋予 DataForm 中的 TextBox?

发布于 2024-08-16 13:01:42 字数 188 浏览 2 评论 0原文

我有一个小的 DataForm,我想将焦点设置在第一个 TextBox 上。我正在使用 2009 年 11 月的工具包。我已命名 TextBox 并尝试使用 DataForm 的已加载事件中的 .Focus() 。我看到它在一个光标“闪烁”时获得焦点,然后就消失了。我正在尝试弄清楚这是否是 DataForm 的产物或其他东西。有谁知道我是否应该能够做到这一点?

I've got a small DataForm and I want to set the focus on the first TextBox. I'm using the Novermber 2009 Toolkit. I've named the TextBox and tried using .Focus() from the DataForm's loaded event. I see it get focus for one cursor 'blink' and then it's gone. I'm trying to work out if this is an artefact of the DataForm or something else. Does anyone know if I should be able to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无尽的现实 2024-08-23 13:01:42

我成功使用的一个小技巧是订阅文本框的 Loaded 事件,然后在事件处理程序中,我使用如下代码设置焦点:

private void TextBox_Loaded(object sender, RoutedEventArgs e)
{
            TextBox usernameBox = (TextBox)sender;
            Dispatcher.BeginInvoke(() => { usernameBox.Focus(); });
}

A little trick I've used successfully is to subscribe to the Loaded event of the textbox, then in the event handler, I set the focus with code such as this:

private void TextBox_Loaded(object sender, RoutedEventArgs e)
{
            TextBox usernameBox = (TextBox)sender;
            Dispatcher.BeginInvoke(() => { usernameBox.Focus(); });
}
栩栩如生 2024-08-23 13:01:42

我尝试了很多建议,例如使用 Dispatcher、UpdateLayout 等,这些建议在各个网站上都有,但没有一个对我来说可靠。最后我决定如下:

private bool _firstTime = true;

    private void MyChildWindow_GotFocus(object sender, RoutedEventArgs e)
    {
        if (_firstTime)
        {
            try
            {
                var dataForm = MyDataForm;
                var defaultFocus = dataForm.FindNameInContent("Description") as TextBox;
                defaultFocus.Focus();
            }
            catch (Exception)
            {
            }
            finally
            {
                _firstTime = false;
            }
        }
    }

我知道不太漂亮……但它有效。在 SL4 中使用 Focus() 方法似乎存在计时问题。

I tried loads of suggestions e.g. using Dispatcher, UpdateLayout etc etc. floating around on various internet sites and none of them worked reliably for me. In the end I settled on the following:

private bool _firstTime = true;

    private void MyChildWindow_GotFocus(object sender, RoutedEventArgs e)
    {
        if (_firstTime)
        {
            try
            {
                var dataForm = MyDataForm;
                var defaultFocus = dataForm.FindNameInContent("Description") as TextBox;
                defaultFocus.Focus();
            }
            catch (Exception)
            {
            }
            finally
            {
                _firstTime = false;
            }
        }
    }

Not pretty I know...but it works. There appears to be a timing issue with using the Focus() method in SL4.

心意如水 2024-08-23 13:01:42

尝试调用我的自定义焦点设置函数(FocusEx)。

internal static class ControlExt 
{ 
    // Extension for Control 
    internal static bool FocusEx(this Control control) 
    { 
        if (control == null) 
                return false; 

        bool success = false; 
        if (control == FocusManager.GetFocusedElement()) 
                success = true; 
        else 
        { 
                // To get Focus() to work properly, call UpdateLayout() immediately before 
                control.UpdateLayout(); 
                success = control.Focus(); 
        } 

        ListBox listBox = control as ListBox; 
        if (listBox != null) 
        { 
                if (listBox.SelectedIndex < 0 && listBox.Items.Count > 0) 
                        listBox.SelectedIndex = 0; 
        } 

        return success; 
    } 
} 

那应该对你有用。

吉姆·麦柯迪

YinYangMoney

Try calling my custom focus setting function (FocusEx).

internal static class ControlExt 
{ 
    // Extension for Control 
    internal static bool FocusEx(this Control control) 
    { 
        if (control == null) 
                return false; 

        bool success = false; 
        if (control == FocusManager.GetFocusedElement()) 
                success = true; 
        else 
        { 
                // To get Focus() to work properly, call UpdateLayout() immediately before 
                control.UpdateLayout(); 
                success = control.Focus(); 
        } 

        ListBox listBox = control as ListBox; 
        if (listBox != null) 
        { 
                if (listBox.SelectedIndex < 0 && listBox.Items.Count > 0) 
                        listBox.SelectedIndex = 0; 
        } 

        return success; 
    } 
} 

That should work for you.

Jim McCurdy

YinYangMoney

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文