[RESOLVED] [3.0/LINQ] Allow inherited control to be selectable and have focus
I have a control that inherits from UserControl and i want it to be selectable and to receive input focus. (I need to be able to give it focus and make it selectable through code).
I've tried setting the controlstyle to selectable, hiding the inherited CanSelect/CanFocus properties (using new bool ....) but even that doesn't give the control focus when i do MyControl.Focus() and ParentForm.ActiveControl = this.
Any ideas?
Re: [3.0/LINQ] Allow inherited control to be selectable and have focus
What you're doing is wrong. A UserControl is supposed to be a container for multiple child controls. That's what the UserControl class exists for. If you want to make a control that can receive focus then you should be inheriting the Control class, not the UserControl class.
Re: [3.0/LINQ] Allow inherited control to be selectable and have focus
Ahhh that explains it! Thanks for the tip. (I love being able to write a question before i go to bed and get an answer when i wake up!!).
Rating++
EDIT: Looks like you help me too often, can't give you rep until i've given it to others.
Re: [3.0/LINQ] Allow inherited control to be selectable and have focus
Hmm, i now have my class inheriting from Control, rather than UserControl and it still has CanFocus and CanSelect returning false even with me hiding the inherited versions and setting the selectable style for my control.
Any ideas, do i need to inherit some other control that allows focus?
Re: [3.0/LINQ] Allow inherited control to be selectable and have focus
I've never actually developed a custom control myself so I'm not sure of all the details. I think at least you have to call SetStyle in the constructor and set the Selectable style. I'd go to MSDN and do some reading on authoring custom controls.
Re: [3.0/LINQ] Allow inherited control to be selectable and have focus
I did have a search around before posting, but i'll have another go if no-one else has any ideas.
Re: [3.0/LINQ] Allow inherited control to be selectable and have focus
I used a .net reflector to see what CanFocus and CanSelect did internally.
Seems that they actually do some low-level looking at window handles.
Because of what i'm doing with the control and form (rendering it via XNA) the functions always return false. Soooo.... i've just created several methods that hide all the selection and focusing related functions.