find usercontrol that a control resides in - winforms

I know that if I have a control, I can do controlobj.FindForm to get a handle on the form it resides in. but is there a similar method to find out the UserControl that a control is in?
thanks
nat

ended up recursing my way up the parent tree until I hit a usercontrol

Related

How to know if the UserControl is active other than using IsFocused

I am working on a WPF project, and I am trying to fire an event every time some userControls get active or inactive.
These userControls have many other controls inside of them.
I tried to achieve this using the userControl events GotFocus and LostFocus, but these events are not working in the way I need since the userControl loses the focus when I work with controls inside of it.
So, my question is: Is there a way to mantain a userControl as Active while the user works with controls inside of it, and, when the user goes to another userControl this first one gets Inactive???
Thank you in advance.
I could solve my problem thank to the comments of #LPL and #Rachel.
I had to use the event UIElement.IsKeyboardFocusWithinChanged and it worked perfectly.
At first I had a problem which was that the callback method was being raised infinitely, but the actual problem was that I was showing a MessageBox every time the event IsKeyboardFocusWithinChanged raised, so, this caused that the IsKeyboardFocusWithin property changed and it created an infinite loop. But thanks to Rachel's advice I could figure out how to solve it.
I am not sure but one workarounnd can be on lost focus of the control check if the control that got focus is child of your control if it is write just return if it is not then just write the logic what you want on lost focus of your control. I hope this will help

How to retemplate a user control

I wrote a user control for my project. Now I want to have a template for it where everything is moved around.
How would I go about doing this? I'm looking and it seems impossible? I see many paths on how to do this but what would be the best way?
Some ideas I have:
Rewrite the user control as a control so it can be templated
Create a second user control, but have it databind to the same viewmodel
You should take the first option. Rewrite the original control as "Silverlight Templated Control" borrowing from the original UserControl xaml to help you set up the initial default template.
Review the MSDN topic: How to: Create a New Control by Creating a ControlTemplate
This forms a good basis. However that example doesn't make enough use of TemplateBinding, in your implementation you should make wider use of that.

Silverlight Panel OnChildAdded event?

I'm trying to derive from the Silverlight Panel control to add some custom logic whenever a control is added to the Panel. I can't seem to find an "OnChildAdded" event (Or something similar) on the Children collection. Does anyone have any suggestions for how I can tell when a child control is added to a Panel or do I have to write my own container control?
Thanks,
Paul
Asked before here - unfortunately you can't get notification with the existing UIElementCollection Children property, but you could try a custom implementation.

WPF usercontrol

I facing very huge problem now,
that is I have usercontrol (created by me) which I have added on to a window...
on click of button I wann show another usercontrol on window, total how can I access the window object in my control (usercontrol)
Thanks all
You can use Window.GetWindow. However, you might want to consider a redesign where the Window is responsible for its content, not something hosted inside the Window.
You could do that by accessing the Parent property.
But why not raising an event from the UserControl which is handled in the window, and there you add the other control?
Or you could create an event and a delegate in the usercontrol and handle it in the Window.

WPF How to get a UserControl to inherit a Button?

I created a UserControl which consists of a couple ellipses and labels. I added it just fine to another form and things were looking pretty snazzy.
Then I went to start adding some event handlers and discovered that the control I made did not expose a Click event. Oops. Easy to fix, right? Just go back to the code-behind on the UserControl I made and let it inherit Button.
Unfortunately, doing this caused a "Partial declarations of MyControl must not specify different base classes" message. This came as a surprise since I haven't declared any other base classes. Searching for the Partial in question returned no results either.
Is there a way to defeat this issue? If not, what would be the easiest workaround here? My goal is simply to get a Click event on the UserControl.
Yes, you've declared other base classes :) Just trust compiler :)
When you writing <UserControl ...></UserControl> in XAML, you are subclassing UserControl. If you want to subclass a Button, use <Button ...></Button> instead and ": Button" in code-behind file.
But I'm strongly encourage you to not subclass Button, its an overkill for you task. You can use MouseLeftButtonUp event instead of the Click event.

Resources