Forum Discussion

Dewayne_Pinion's avatar
Dewayne_Pinion
Contributor
10 years ago

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 Nam...
  • HKosova's avatar
    10 years ago

    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);
  • Ryan_Moran's avatar
    Ryan_Moran
    10 years ago

    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();
        }
    }