none
Can't Focus more then once. RRS feed

  • Question

  • Hey everyone! I have an annoying issue that I'm hoping you can help me with. I have a TabControl with 6 TabItems. Within the first tab there are a bunch of text boxes. I'd like to give my first text box focus. I have been trying over the last 2 days everything I find and most of what I can think of that is absurdly insane to even do. I had my best luck tonight realizing I totally missed an event supported. Well The event triggers fine and my if statement works but the second time the code runs it doesn't focus. I set a breakpoint for everytime that function runs and it does indeed trigger the if to change focus. I don't understand why it won't take focus after the first time it runs, I change tabs and then change back...Nothing is forcused at all. It's very annoying. Just like I can't get tab indexes to work properly for the life of me. Someone please tell me what is going on.

    		private void Main_GridR1_TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e) {
    			if(Main_GridR1_TabControl.SelectedIndex == 0) {
    				Main_GridR1_NASTT_GridC1R3_NewAssetTagM1_TextBox.Focus();
    			}
    		}

    Everytime Index 0 is pressed and this even triggers it runs as it did the first time yet the 2nd time and on that the if is triggered is does run the .Focus() line but will not actually give focus.

    Also strangely enough I decided to be really stupid and tell it to Focus two textboxes. When it ran the first time it focused only to the textbox I told it to focus to first and didn't refocus to the textbox it should have ended with focus to. It's like for some reason you can't Focus more then once per run. If I switched tabs and came back nothing would focus once again like in my original scenario. 

    Sunday, December 30, 2018 5:47 AM

Answers

  • I just created a new very simple project to see if it's just this. I found the problem! Not sure why it acts like this probably doesn't like to refocus an already focused object. So I had the same result with a quickly thrown together application just to basically do the same thing. So then I decided well what if I tell it in each tab what to focus on. As soon as I gave another if for index 1 and put a text box in there and set that to focus and when I'd switch back and forth it would perform as expected!
    • Marked as answer by fatalerror0x00 Sunday, December 30, 2018 3:06 PM
    Sunday, December 30, 2018 3:06 PM

All replies

  • For some reason even when the box does indeed get focus test is still false and when it doesn't get focus even though it should it's false. When I load my program selectionchange triggers and the focus is where I want it but only at that time.

    I also tried using the lostFocus event on my text box. None of that code runs when I switch back and tell it to focus but doesn't. I am so confused on what is going on.

    So in all that you mentioned Andrew I decided to make an event for GetFocus as well and you wouldn't believe it (Actually with all that's happening it comes as no surprise) When I switch back to that tab and it's supposed to Focus again the GetFocus event doesn't trigger that second time. It's being told to Focus but some reason immediately it's just not doing it even though that line of code says to since the getFocus event wasn't triggered after Focus isn't being given even when told to.

    Sunday, December 30, 2018 2:52 PM
  • I just created a new very simple project to see if it's just this. I found the problem! Not sure why it acts like this probably doesn't like to refocus an already focused object. So I had the same result with a quickly thrown together application just to basically do the same thing. So then I decided well what if I tell it in each tab what to focus on. As soon as I gave another if for index 1 and put a text box in there and set that to focus and when I'd switch back and forth it would perform as expected!
    • Marked as answer by fatalerror0x00 Sunday, December 30, 2018 3:06 PM
    Sunday, December 30, 2018 3:06 PM
  • Hi fatalerror0x00,

    I am glad to know that you resolve the issue and thanks for sharing the solution to us, it will be beneficial to other communities who have the similar issue.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 8, 2019 1:41 AM
    Moderator