Trying to get the .Pressed to work for a button - help?

Class library usage, coding and language questions.
Chrispy
Posts: 5
Joined: Tue Mar 25, 2008 1:43 pm

Trying to get the .Pressed to work for a button - help?

Post by Chrispy » Wed Mar 26, 2008 12:35 pm

I'm get a return when a button has been pressed. I had this working but now fails everytime I try and get the pressed return from calc.

Code: Select all

//Perform click test to see if the button rasies a click event
                Button ClickButton = (Button)form.FindButton(button.ControlId);
                ClickButton.Click();
                Console.WriteLine("BUTTON PRESSED?: " + ClickButton.Pressed);
                
I've tried the example also from your Getting started guide but this is incorrect.
Button button = form.FindButton("button1");
This specifies an error as your trying to specify a string and not an int value.

I'm currently evaluating this and any help with trying to sort it out would be great.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Post by Support Team » Wed Mar 26, 2008 1:42 pm

The Button.Pressed property returns the current state of the button. I.e. if you click the button in the previous line, that does not necessarily mean the button is still pressed.
Chrispy wrote:Button button = form.FindButton("button1");
This is another overload of the Control.FindButton method that searches for .NET Windows Forms controls by their name. As this overload is only useful for .NET Windows Forms controls, you can't use it with the Windows Calculator (though your code should compile anyway).

Regards
Alex
Ranorex Support Team

Chrispy
Posts: 5
Joined: Tue Mar 25, 2008 1:43 pm

Post by Chrispy » Wed Mar 26, 2008 2:40 pm

So how would I be able to click a button and retrieve the current state?

Any advise would be very helpfull.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Post by Support Team » Wed Mar 26, 2008 3:11 pm

Well, you can use the Mouse.ButtonDown and Mouse.ButtonUp methods:

Code: Select all

Mouse.MoveToControl(button);
Mouse.ButtonDown(MouseButtonType.LeftButton);
bool buttonPressed = button.Pressed;
Mouse.ButtonUp(MouseButtonType.LeftButton);
However, if you want to check, whether the button has been clicked, I'd recommend checking if your application has reacted to the click rather than recognizing the click itself.

Alex
Ranorex Support Team

t4k9
Posts: 10
Joined: Wed Mar 07, 2012 11:00 pm

Re: Trying to get the .Pressed to work for a button - help?

Post by t4k9 » Tue Oct 02, 2012 10:11 pm

Implemented the above suggestion:

Code: Select all

Mouse.MoveTo(_rexButton);
Mouse.ButtonDown(MouseButtons.Left);
var p1 = _rexButton.Pressed;
Mouse.ButtonUp(MouseButtons.Left);
var p2 = _rexButton.Pressed;
return (p1 || p2);
I was not abled to get the Pressed property to return true. It always returns false. Under what condition is the Pressed true?

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Trying to get the .Pressed to work for a button - help?

Post by Support Team » Wed Oct 03, 2012 12:57 pm

Hi,

When the button is pressed it should return true, in our case the code worked as expected.
Which Ranorex version are you using?
Can you see the attribute "Pressed" when you track the element with Spy?

Regards,
Markus
Ranorex Support Team
.
Image

t4k9
Posts: 10
Joined: Wed Mar 07, 2012 11:00 pm

Re: Trying to get the .Pressed to work for a button - help?

Post by t4k9 » Wed Oct 03, 2012 4:23 pm

Hi, we're using Ranorex 3.2.1. We do see the Pressed property in the spy too. The button was pressed (based on the effects of the application), but the pressed property was never true.

t4k9
Posts: 10
Joined: Wed Mar 07, 2012 11:00 pm

Re: Trying to get the .Pressed to work for a button - help?

Post by t4k9 » Wed Oct 03, 2012 4:41 pm

Also, it was a wpf button. Would that make a difference?

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Trying to get the .Pressed to work for a button - help?

Post by krstcs » Wed Oct 03, 2012 8:15 pm

If you button is a "Momentary" button (it changes back to the un-clicked stated on it's own when the mouse button is released) then you will not see a change in the Pressed attribute.

.Pressed checks to see if the button is CURRENTLY in the pressed state. If it is a toggle button, it should work properly (assuming the developer coded the attributes to reflect that state). If it is not a toggle button, Ranorex will not be able to see the change due to the nature of mouse input and momentary controls.

What you need to do is test for the action that your button invokes, not for the button press itself.
Shortcuts usually aren't...

t4k9
Posts: 10
Joined: Wed Mar 07, 2012 11:00 pm

Re: Trying to get the .Pressed to work for a button - help?

Post by t4k9 » Wed Oct 03, 2012 8:58 pm

krstcs wrote:If you button is a "Momentary" button (it changes back to the un-clicked stated on it's own when the mouse button is released) then you will not see a change in the Pressed attribute.
Would a "Momoentary" button's Pressed state always be false?

The question is: what is the state of the button while the mouse is in the process of being clicked? i.e.,

Code: Select all

Mouse.ButtonDown(MouseButtons.Left);
I am sure there may be many other ways to check whether a button's being clicked. For the purpuse of this discussion, I am asking about the behaviour of the Pressed property with respect to a regular button.

User avatar
artur_gadomski
Posts: 207
Joined: Mon Jul 19, 2010 6:55 am
Location: Copenhagen, Denmark
Contact:

Re: Trying to get the .Pressed to work for a button - help?

Post by artur_gadomski » Thu Oct 04, 2012 8:18 am

A simple test both with spy and with code:
Ranorex.Button b =Host.Local.FindSingle(
"/form[@title='Calculator']/element/container[@caption='' and @class='#32770' and @instance='2']/button[@text='' and @controlid='138']");
Console.WriteLine(b.Pressed);
b.MoveTo();
Mouse.ButtonDown(MouseButtons.Left);
Console.WriteLine(b.Pressed);
Mouse.ButtonUp(MouseButtons.Left);
Console.WriteLine(b.Pressed);
Shows that windows Calculator buttons behave as expected. So maybe your button implementation is broken or it overrides default behavior for some reason.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Trying to get the .Pressed to work for a button - help?

Post by Support Team » Thu Oct 04, 2012 8:25 am

If the test artur_gadomski posted does not produce the desired output (i.e. "False", "True", "False"), then please post a snapshot of the button and tell us which technology it is made of (e.g. WinForms, WPF, Flex, ...). Thank you!

Regards,
Alex
Ranorex Team
.
Image

t4k9
Posts: 10
Joined: Wed Mar 07, 2012 11:00 pm

Re: Trying to get the .Pressed to work for a button - help?

Post by t4k9 » Thu Oct 04, 2012 3:20 pm

Thanks for all your timely replies and insightful help.

Arthur's code works! However, if I changed the path to the button I test, the output becomes false, false, false. I think I know the reason now. Calculator's button has flavorname of win32 and my button has wpf. I believe that is the root cause of the problem I am experiencing.

Can one verify if that is the case? Also, would the latest version of Ranorex solve such issue? We are currently using Ranorex 3.2.1. Thanks in advance.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Trying to get the .Pressed to work for a button - help?

Post by Support Team » Mon Oct 08, 2012 1:14 pm

Hi,

That is the case, Button.Pressed unfortunately doesn't work for WPF buttons, except if the "TogglePattern" would be implemented for the button.

Regards,
Markus
Ranorex Support Team
.
Image