Forum Discussion

DainiusA's avatar
DainiusA
Contributor
3 years ago

split number into individual digits c# script and then use for loop

Hello,

 

I need a script to get me on the right road. I have a variable that parses a number from an SQL query example: 128 or 258 and any other combination possible.

 

I need to separate those numbers and click the coordinates(can't map the buttons because of overlapping windows).

Example how it should look:

 

Variable SQL query returns 2589

 

Separated number 2 needs to click the coordinates that are set.

Separated number 5 needs to click the other coordinates that are set 

and so on....

 

I think it is possible to do with a for loop. But I am quite new to scripting and don't know how to do it.

 

Or maybe is it possible to accomplish all of this through the KeywordTests ?

 

If my explanation is unclear do not hesitate to ask anything I will try to put all the information you need. 

2 Replies

  • Marsha_R's avatar
    Marsha_R
    Champion Level 3

    You can convert that number to a string

    https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqconvert/index.html

     

    then pick the string apart for your coordinates

    https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqstring/index.html

     

    Then convert the separate coordinates back to numbers using aqconvert again.

     

    Try the examples and then with your numbers and let us know if you get stuck.

     

     

    • DainiusA's avatar
      DainiusA
      Contributor

      Hello,

       

      Thank you for the right path. Now my code looks like this:

       

      function VariablesTest()
      {
      var Znr = Project["Variables"]["Znr"]("Znr");
      for (var i = 0; i < aqString["GetLength"](Znr); i++)
      Log["Message"]( aqString["GetChar"](Znr, i) );
      }

       

      And the output looks like this:

       

       

       

      I got stuck and try to understand how can I take the first, second and third digits separately and click the desired coordinates for each of them.

      Really appreciate your help.

       

      I have tried to do something like this, but i ended up pressing just the first coordinate:

       

      function VariablesTest()
      {
      var window = Aliases["UvsSco"]["HwndSource_Window"]["Window"];
      var richTextBox = window["exclusivecontrolled_localattendant_localattendanttexts_attenderrortext"];
      var Znr = Project["Variables"]["Znr"]("Znr");
      for (var i = 0; i < aqString["GetLength"](Znr); i++)
      Log["Message"]( aqString["GetChar"](Znr, [1]) );

      if(Znr, [1] == '1')
      richTextBox["Click"](97, 87); //1
      else if (Znr, [1] == '2')
      richTextBox["Click"](151, 86); //2
      else if (Znr, [1] == '3')
      richTextBox["Click"](210, 85); //3
      else if (Znr, [1] == '4')
      richTextBox["Click"](97, 128); //4
      }

       

      Edit:

       

      Found my mistake:

       

      Instead of if(Znr, [1] == '1'), replaced with  if( aqString["GetChar"](Znr, [1]) == '1') 

       

      and clicks the button I need. Thank you again for the right path !