ListBox dialog control - how to loop and count

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
drunkenmonkey
Pro Scripter
Posts: 90
Joined: Fri Jan 31, 2020 10:52 am

ListBox dialog control - how to loop and count

Post by drunkenmonkey » Sat Mar 06, 2021 2:25 am

Hi, can someone help me and show me how to count the total items in a list.
How to loop, from the beginning of the list to the end of the list.
Thank you!

User avatar
Grovkillen
Automation Wizard
Posts: 1128
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: ListBox dialog control - how to loop and count

Post by Grovkillen » Sat Mar 06, 2021 6:39 am

Do you mean a list in an external application?
Let>ME=%Script%

Running: 15.0.27
version history

drunkenmonkey
Pro Scripter
Posts: 90
Joined: Fri Jan 31, 2020 10:52 am

Re: ListBox dialog control - how to loop and count

Post by drunkenmonkey » Sat Mar 06, 2021 9:06 am

Thank you for your response. I should have specify.
It's the MS ListBox control of the dialog.
I want to iterate thru the ListBox from the first item to the last.
Thank you!

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1378
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: ListBox dialog control - how to loop and count

Post by Dorian (MJT support) » Sat Mar 06, 2021 4:03 pm

Like this :

Code: Select all

GetDialogProperty>Dialog1,MSListBox1,Text,TheText
That gets all the contents. You can then use Separate on the result, if you wish. In the case of the sample below, we could separate by CRLF

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Left = 2322
  Top = 431
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  ClientHeight = 428
  ClientWidth = 1170
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -28
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 240
  TextHeight = 32
  object MSListBox1: tMSListBox
    Left = 513
    Top = 47
    Width = 242
    Height = 194
    Style = lbOwnerDrawFixed
    ItemHeight = 32
    Items.Strings = (
      'Red'
      'Blue'
      'Green'
      'Yellow')
    TabOrder = 0
    Text = 'Red'#13#10'Blue'#13#10'Green'#13#10'Yellow'#13#10
    SelectedIndex = -1
  end
  object MSButton1: tMSButton
    Left = 792
    Top = 190
    Width = 150
    Height = 50
    Caption = 'Go!'
    TabOrder = 9
    DoBrowse = False
    BrowseStyle = fbOpen
  end
end
EndDialog>Dialog1

AddDialogHandler>Dialog1,MSButton1,OnClick,DoClick
show>Dialog1,res

SRT>DoClick
  GetDialogProperty>Dialog1,MSListBox1,Text,TheText
  MDL>TheText

  Separate>TheText,CRLF,Elements

  let>theloop=0
  repeat>theloop
    let>theloop=theloop+1
    mdl>Elements_%theloop%
  Until>theloop,Elements_count
End>DoClick

Yes, we have a Custom Scripting Service. Message me or go here

drunkenmonkey
Pro Scripter
Posts: 90
Joined: Fri Jan 31, 2020 10:52 am

Re: ListBox dialog control - how to loop and count

Post by drunkenmonkey » Sat Mar 06, 2021 5:02 pm

That is great Dorian. Than you! :) :) :)
If I want to output de content of the listbox to an existing or new excel
what should I do.
Thank you again!

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1378
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: ListBox dialog control - how to loop and count

Post by Dorian (MJT support) » Sat Mar 06, 2021 5:51 pm

Just write it to Excel instead. Something like this should get you started.

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Left = 2322
  Top = 431
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  ClientHeight = 428
  ClientWidth = 1170
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -28
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 240
  TextHeight = 32
  object MSListBox1: tMSListBox
    Left = 513
    Top = 47
    Width = 242
    Height = 194
    Style = lbOwnerDrawFixed
    ItemHeight = 32
    Items.Strings = (
      'Red'
      'Blue'
      'Green'
      'Yellow')
    TabOrder = 0
    Text = 'Red'#13#10'Blue'#13#10'Green'#13#10'Yellow'#13#10
    SelectedIndex = -1
  end
  object MSButton1: tMSButton
    Left = 792
    Top = 190
    Width = 150
    Height = 50
    Caption = 'Go!'
    TabOrder = 9
    DoBrowse = False
    BrowseStyle = fbOpen
  end
end
EndDialog>Dialog1


AddDialogHandler>Dialog1,MSButton1,OnClick,DoClick



show>Dialog1,res




SRT>DoClick
  GetDialogProperty>Dialog1,MSListBox1,Text,TheText

XLCreate>d:\new.xlsx,1,xlbook
XLAddSheet>xlbook,MySheet


//  MDL>TheText

  Separate>TheText,CRLF,Elements

  let>theloop=0
  repeat>theloop
    let>theloop=theloop+1
//    mdl>Elements_%theloop%
    
    XLSetCell>xlbook,MySheet,%theloop%,1,Elements_%theloop%,res
    
    
    
  Until>theloop,Elements_count
End>DoClick

Yes, we have a Custom Scripting Service. Message me or go here

drunkenmonkey
Pro Scripter
Posts: 90
Joined: Fri Jan 31, 2020 10:52 am

Re: ListBox dialog control - how to loop and count

Post by drunkenmonkey » Sat Mar 06, 2021 6:14 pm

Thank you Dorian! This is amazing. I have enough
code snipets to play for the rest of the day.
:D :D :D :D :D :D :D :D :D :D :D :D :D :D

drunkenmonkey
Pro Scripter
Posts: 90
Joined: Fri Jan 31, 2020 10:52 am

Re: ListBox dialog control - how to loop and count

Post by drunkenmonkey » Mon Mar 08, 2021 2:51 am

Hi! I'm stock with my little project, can you help me.
I would like to use the variables in the dialog to automate Google and a website.
If it is hardcoded in sthe script it works. But when I start a the Google session
variables are not recognised.
This is a pricture of the dialog and the website.
Thank you again for your prImagecious help.

https://u.pcloud.link/publink/show?code ... OQDQrgXdeX

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1378
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: ListBox dialog control - how to loop and count

Post by Dorian (MJT support) » Mon Mar 08, 2021 11:03 am

If you see %VariableName% in place of the actual variable, it means that variable doesn't exist.

For example:

Code: Select all

let>VariableName=Hello
MessageModal>%VriableNam%
MessageModal>VriableNam
MessageModal>VariableName
MessageModal>%VariableName%
So go back to that part of your code and investigate.

Here are two resources that will help you :

https://help.mjtnet.com/article/7-using-the-debugger
https://help.mjtnet.com/article/42-usin ... t-creation
Yes, we have a Custom Scripting Service. Message me or go here

drunkenmonkey
Pro Scripter
Posts: 90
Joined: Fri Jan 31, 2020 10:52 am

Re: ListBox dialog control - how to loop and count

Post by drunkenmonkey » Mon Mar 08, 2021 12:35 pm

Thank you Dorian for your answer. Always helpful.
The value is not showed because the option of the combobox "Choisir le taux" is not fill".

The question remain anyway.
Now that the Dialog is build. I want to be able to use the assigned variable, and most important
the listbox items.

When I launch Google, and the website and I want to automate it's navigation, the Dialog variables
are not recognize. I'ts instance seems to be "detached" from the script.

Is there a way to make those Dialog variables be part again like a Dialog1.%varName%?

Thank you!

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1378
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: ListBox dialog control - how to loop and count

Post by Dorian (MJT support) » Mon Mar 08, 2021 3:20 pm

Let's take a step back and forget Google and the website for now,. Let's make sure the variables are what you think they are. (You could do this with the debugger I showed you in the previous reply).

If you simply use MessageModal>%MyVariable% (of course edit this), do you see what you expect? If no, then examine carefully with the debugger. If yes, then we need to take the next steps to enter those details into the browser.

If we're using IE we can use the Find IE element Wizard. Or for Chrome you can use the Chrome Functions. If it can simply be encoded into a URL you could ExecuteFile the URL.

Either way, any variables created in Macro Scheduler should be passed to another application as simply as creating a variable and typing it into Notepad.

Code: Select all

Let>MyVar=Hello

run>Notepad
WaitWindowOpen>Untitled*
SetFocus>Untitled*

Send>%MyVar%
Yes, we have a Custom Scripting Service. Message me or go here

drunkenmonkey
Pro Scripter
Posts: 90
Joined: Fri Jan 31, 2020 10:52 am

Re: ListBox dialog control - how to loop and count

Post by drunkenmonkey » Mon Mar 08, 2021 4:14 pm

Again Thank you Dorian! I had to changed the window title. In french it is "Sans titre - Bloc-notes"
Work perfect. Nothing like going back to basic.
I'll go and apply to my project. :D :D :D

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1378
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: ListBox dialog control - how to loop and count

Post by Dorian (MJT support) » Mon Mar 08, 2021 4:26 pm

Always happy to help (even if I don't always understand the question the first time). :)
Yes, we have a Custom Scripting Service. Message me or go here

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts