开发者

Winforms Textbox focus with TabControl

VB.NET Windows Forms Project, VS2010.

I have a tabcontrol with several tabpages and on each tabpage there is a textbox. I want the focus (and insertion point) to go to the end of the text in the textbox when the tab is selected. If I make a junky project with the following code in each tab, it works fine:

Private Sub TabPage1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabPage1.Enter
  With TextBox1
    .SelectionStart = .Text.Length
    .Select()
  End With
End Sub

However, the same code is not working in my actual project. I am having tr开发者_运维技巧ouble debugging, because if I step through, the breakpoints and IDE mean the focus events don't fire in the same order. Any help to either get the result I want, or even to how to debug would be most appreciated.


Print out the order of events using System.Diagnostics.Debug.Write("FunctionName: xyz, ActiveControl selected:" & Me.ActiveControl) so you know the call stack order and which event steals the focus away. This will also help you to reproduce the event sequence when debugging in the IDE.

It will be simpler putting the Debug.Write statements at the beginning and end of most of your Tab Events, but if your interested here is a simple way to do your whole app: Aspect Oriented Programming Method Boundary events

My guess is that your cut-down repro project doesn't have an event that fires after the TabPage1_Enter, where as your actual project does and is somehow taking the focus away from the textbox... which the Debug.Write output will show. HTH

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜