WinForms UserControl can't Focus properly - c#

I have a WinForms UserControl that accepts keyboard input, and had a Scrollbar for scrolling, and everything was fine. Recently I swapped the Scrollbar control out for a custom scrollbar (also a UserControl), and now after clicking the custom scrollbar, my custom control loses focus and the only way to get it back is to click a different focusable control (like a TextBox) and then click back in my UserControl. If the scrollbar has focus and I click inside my UserControl to give it focus, I notice the LostFocus event is raised and the scrollbar keeps the focus
I tried setting the UserControl's Selectable control style to true, it didn't help.
Any idea why it would behave this way?

I figured it out. I used
SetStyle(ControlStyles.ContainerControl, false);
In the UserControl's constructor. Because it had the ContainerControl flag set by default, it would preferentially give the focus to a child control.

Related

TabStop property is not working for usercontrol inside the panel

I have a usercontrol which is placed inside one panel and some other controls like buttons in another panel. when the usercontrol is active and i move from other controls using tab key, usercontrol gets focus even when the TapStop property for the usercontrol is set to "False".
In another case, when i have the usercontrol placed in the form not inside the panel, usercontrol didnt get focused when moving from other controls using tab key.
Could anyone share me the details why the usercontrol is getting focused when it is only placed inside the panel?
Thanks and Regards,
Amal

ListView LostFocus Event isn't working fine

I've a listview and a textBLOCK inside it. I want to collapse the visibility of listview when I tap anywhere else on my screen. I tried to do this using LostFocus Event for my listview but it is fired ONLY when I selected an item. Why does it behave like that?
Thanks in advance!
The only way to do this as of right now, based on this seems to be to hook into the touch input directly from the CoreWindow and then each time compare the point on the touch input with the bounds of the ListView relative to the CoreWindow.
The WinRTXamlToolkit may assist in this by adding such extensions as GetBoundingRect to FrameworkElements.
I used the IsTabStop Property. Actually i have a user control in in which i'm having this Listview. I did the following steps:
I set the IsTabStop property of UserControl to "True".
Then I set the focus to the textblock when it is tapped by doing:
this.Focus(FocusState.Pointer);
Then created the LostFocus Event of my UserControl and Collapsed the visibility of ListView.
Try using TextBlock's Leave or LostFocus event because if you focus the TextBlock, the ListView losts focus but if you focus TextBlock when you focused something out of ListView, the ListView never becomes focus.

Open a Popup from a Scrollviewer

I'm making a WinRT application using XAML and I have a control that has a TextBox and a Popup that opens under it when the TextBox get focus.
The Width of the Popup is the same as the Width of the TextBox. This works fine but if I put my control in a ScrollViewer and zoom in the size of the popup doesn't change. I would like to achieve something similar to the standard ComboBox which changes the size of its Popup when it is nested in a ScrollViewer. What should I do?
I know that the popup must be part of the VisualTree but I'm not quite sure how to add it without changing the existing layout.
Thank you
To have the popup be parented in the TextBox you can either modify the template of the TextBox and put it in there (you could subclass TextBox to both add support for your dropdown logic and change the default template at the same time) or simply find the root Grid of the TextBox using VisualTreeHelper. By default TextBox has a Grid at its root, so you could get that and add the Popup to its Children.

WinRT, TextBox's focus, weird behavior

in one of my WinRT App's Views, I'd like the BottomAppBar to be displayed when a TextBoxloses focus. I played with the GotFocus and LostFocus events with which I can manually change the IsOpen property of the BottomAppBar accordingly.
Problem is, when the BottomAppBar is open, if the user clicks on the TextBox, the BottomAppBar is closed (standard AppBar behavior), but the TextBox is not focused (even though the user clicked right on it). The user needs to focus the TextBox again to be able to type something again.
If I hook myself up in the Closed event of the BottomAppBar to try and programmatically set the focus to the TextBox, it shortly becomes focused, but loses the focus right away and instead, its ScrollViewer gets focused.
Any idea why the TextBox loses the focus the second time?
Any idea how I can do what I'm trying to achieve?
Thanks!
Perhaps your BottomAppBar gets closed when you tap out of it and on the ScrollViewer and setting the focus to your TextBox gets overriden by the focus being set to the ScrollViewer right after that. If you only ever want focus to be on the TextBox - you could disable focus from ScrollViewer. You could also try to set the focus after a delay (either with await Task.Delay(50); or with await Dispatcher.RunAsync(() => /*set focus) so it might get set after the ScrollViewer gets focus or handle GotFocus on the ScrollViewer and set the focus back to the TextBox when you want it to keep the focus. Finally make sure that the TextBox can get focus at all.

problemwith tab index in windows application user control

I create an user control that has some textbox and buttons.the problem is when I use tab to traverse in My form when my usercontrol get focus the focus go inside of user control and buttons insid if it would focus.How I can simply go to next control after my user control not inside it?
thanks
If the controls have Focusable CanHaveFocus TabStop sort of properties, set them to false.
Also set the TabIndex properties of those controls to 0. I'm sure that the controls will be ghosts in terms of getting tab focus.
Set TabStop property on your custom user control elements which you want to exclude (eg. your button) to false.

Categories