7 Answers, 1 is accepted
By "set focus to a silverlight control" am I correct in assuming you're referring to the input focus such that typing goes into that control? First I'd like to better understand why you need this. The reason I ask is that most of our Silverlight automation commands automatically set the input focus ahead of time or operate in a manner that the input focus doesn't matter.
Anyway, the only way to do this is to perform a desktop mouse click. So get the the element you want the input focus on, then call
Manager.Desktop.Mouse.Click(MouseClickType.LeftClick, mytextBox.GetRectangle())
Greetings,Cody
the Telerik team
I had an autocomplete search box in my application. So when the popup is visible i cant click any button.
Thats the time i thought about the focus method. But i couldn't find. I solved the issue in another way.
Thanks for the information. But anyway you could have indlude Focus method too...In the next release can we expect that.
Thanks
Kiran
I am getting the bleow error when i used your code Manager.Desktop.Mouse.Click(MouseClickType.LeftClick, mytextBox.GetRectangle())
Argument '2': cannot convert from 'System.Drawing.RectangleF' to 'System.Drawing.Point'
Also i am refering to silverlight not HTML.
Thanks
Kiran
Sorry about that. Just use the System.Drawing.Rectangle.Round function to convert the RectangleF to a Rectangle:
Manager.Desktop.Mouse.Click(MouseClickType.LeftClick, System.Drawing.Rectangle.Round(mytextBox.GetRectangle()));
Sincerely yours,
Cody
the Telerik team
When i user your code
Manager.Desktop.Mouse.Click(MouseClickType.LeftClick, System.Drawing.Rectangle.Round(mytextBox.GetRectangle()));
It is clicking some other point,
But when i used the below code its working.
Manager.Desktop.Mouse.Click(
MouseClickType.LeftClick, mytextBox.GetScreenRectangle());
What is the differenec between GetScreenRectangle and GetRectangle.
Also i cant use this technic for focusing Button or anyother control which is having click event.
Better in the next release include this functionality also. Sometimes its needed for our Test cases
Thanks
Kiran
I have got a better solution to get focus on Button or Textbox
Button
userLookButton = app.Find.ByName<Button>("btnUserLookup");
AutomationMethod focusMethod = new AutomationMethod("Focus", typeof(bool));
userLookButton.InvokeMethod(focusMethod);
So i have written a Extension method for this and moved out to a seperate class
So now it can be called as show in below
Button userLookButton = app.Find.ByName<Button>("btnUserLookup");
userLookButton.Focus();
public
static
class
ExtensionMethods
{
public
static
void
Focus(
this
FrameworkElement ele, Manager manager)
{
manager.Desktop.Mouse.Click(MouseClickType.LeftClick, ele.GetScreenRectangle());
}
public
static
void
Focus(
this
Button button)
{
AutomationMethod clickMethod =
new
AutomationMethod(
"Focus"
,
typeof
(
bool
));
button.InvokeMethod(clickMethod);
}
public
static
void
Focus(
this
TextBox textBox)
{
AutomationMethod clickMethod =
new
AutomationMethod(
"Focus"
,
typeof
(
bool
));
textBox.InvokeMethod(clickMethod);
}
}
Thanks
Kiran
Best wishes,
Konstantin Petkov
the Telerik team