Returning object name from function and accessing properties/methods
Hello,
This should be simple but so far I am striking out. I have a function that will search the current form for a button that I pass in as a variable:
function FindBtn(BtnName) // Pass in Name of Control { var btn,p; p = Sys.Process("M3.AKShell"); var PropName = "Name"; // Reference the control's Name property var PropValue = BtnName; // The Name property value for the control btn = p.findChild(PropName,"WinFormsObject(" + PropValue + ")",200000,true); // Look for the button on the current form if (btn.exists) {
Log.Message(btn.Name + " was successfully found"); return btn.FullName; } else { Log.Error("The " + BtnName + " button was not found"); } }
This seems to work well. I can follow along and see that it finds the control I am looking for. I am then trying to call the button's ClickButton event, which is not working. No error is returned, the log shows the steps as passing. Here is my calling function:
function clickContactTab() { var VndrBtn; VndrBtn = FindBtn(APContactTab); VndrBtn["ClickButton"]();//<--This line is not working }
I can see that the FullName is returned to the calling function, however, I am receiving the error:
Object doesn't support this property or method
and the button is never actually clicked. What am I missing? :)
Change the FindBtn function to return the button object instead of the button's FullName:
if (btn.exists) { Log.Message(btn.Name + " was successfully found"); return btn; }
Also, I think
btn = p.findChild(PropName, "WinFormsObject(" + PropValue + ")", 200000, true);
can be simplified into:
btn = p.FindChild("NativeClrObject.Name", BtnName, 200000, true);
As Helen pointed out you're returning the name.
You need to return the object in your function.
You may also want to make it a little more generic by allowing the passing of the process and property name.
Also return an empty stub object so subsequent checks on the returned object will not fail.
function FindBtn(ProcessName,PropName,PropValue) // Pass in property name and Name of Control { var btn,p; btn = Sys.Process(ProcessName).findChild(PropName,"WinFormsObject(" + PropValue + ")",200000,true); // Look for the button on the current form if (btn.exists) { Log.Message(btn.Name + " was successfully found."); return btn; } else { Log.Error("Object not found.", "Property specified: " + PropName + "\nValue specified: " + PropValue); return Utils.CreateStubObject(); } }