简体   繁体   English

在WPF vb中选择tabItem时设置文本框焦点

[英]Set textbox focus when tabItem selected in WPF vb

New to WPF, aware their are many questions on this already but none have worked. WPF的新手,知道他们已经对此有很多问题,但是没有一个起作用。 I would like a text box within a tab to get focus when the tab has been selected. 我希望标签被选中后,标签内的文本框能够获得焦点。

currently have 目前有

Private Sub TabControl1_Event(sender As Object, e As System.Windows.Controls.SelectionChangedEventArgs) 
_Handles TabControl1.SelectionChanged
    UpdateLayout()     'tried me.show() as well
    If TabControl1.SelectedIndex = 0 Then
        i = i + 1
        Title = "selected" + Convert.ToString(i)
        UserBox.Focus()
    End If
End Sub

the i increment and and title change are their just to see if the if loop is being entered and apparently it is, however the textbox (the only other control in the application) only gets focus when the program first starts. i递增和标题更改只是为了查看是否正在输入if循环,显然是,但是,文本框(应用程序中唯一的其他控件)仅在程序首次启动时获得焦点。

As I said I have seen lots of the other questions similar to this here but either they are not exactly the same or just do not work. 正如我说过的,我在这里已经看到许多其他类似的问题,但是它们不是完全相同,或者就是行不通。 I am using vb.net in a WPF application. 我在WPF应用程序中使用vb.net。 It seems like this should be very simple but cant for the life of me figure it out 看起来这应该很简单,但是我一生无法解决

It would be nice if this could be done via a tabItem event rather than a tabControl event also. 如果可以通过tabItem事件而不是通过tabControl事件来完成,那就太好了。

Update: IDK if it matters but I have seen others posting it and so this is my XAML 更新:IDK如果很重要,但是我看到其他人在发布它,所以这是我的XAML

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Burn In" Height="350" Width="525">
<TabControl Height="Auto" Name="TabControl1" Width="Auto">
    <TabItem Header="User" Name="UserTab">
        <Grid>
            <TextBox Height="25" HorizontalAlignment="Center" Margin="0" Name="UserBox" VerticalAlignment="Center" Width="250" AcceptsReturn="True" />
        </Grid>
    </TabItem>
    <TabItem Header="DUT" Name="DutTab">
        <Grid />
    </TabItem>
    <TabItem Header="Rack" Name="RackTab">
        <Grid />
    </TabItem>
    <TabItem Header="Programs" Name="ProgTab">
        <Grid />
    </TabItem>
</TabControl>
</Window>

I tested this code out on my computer and it worked fine. 我在计算机上测试了此代码,效果很好。

Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        If TabControl1.SelectedIndex = 1 Then
            TabControl1.Update()
            TextBox1.Focus()
        End If
    End Sub

I declared the change of the Tab using the TabControl1_SelectedIndexChanged sub routine. 我使用TabControl1_SelectedIndexChanged子例程声明了Tab的更改。 If you do not clarify that the TabControl1 is changed you need to click in the TabPage for changes to take place. 如果您不确定TabControl1是否已更改,则需要在TabPage中单击以进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM