Combo Box - Assign selected value to a variable

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
dheck2
Newbie
Posts: 3
Joined: Thu Jan 06, 2011 5:40 am

Combo Box - Assign selected value to a variable

Post by dheck2 » Thu Jan 06, 2011 5:46 am

I am new, and am writing my first Dialog. In my Dialog1, I have 1 combo box. I have 3 items in the box, and I want to select one of the items, and have that item assigned to a variable in my script.

For test purposes, its fine to use a messagebox to display the variable after the assignment.

I have followed the samples in the forum, but I can seem to get the value selected in the combo box assigned to a variable.

What is the secret?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Jan 06, 2011 9:12 am

Just query the Text and/or ItemIndex properties. Here's a simple example:

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Left = 560
  Top = 239
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  ClientHeight = 138
  ClientWidth = 245
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
  object MSComboBox1: tMSComboBox
    Left = 59
    Top = 18
    Width = 145
    Height = 21
    ItemHeight = 13
    TabOrder = 0
    Items.Strings = (
      'Red'
      'Orange'
      'Blue'
      'Green'
      'Yellow')
    ListText = 'Red'#13#10'Orange'#13#10'Blue'#13#10'Green'#13#10'Yellow'#13#10
  end
  object MSButton1: tMSButton
    Left = 61
    Top = 64
    Width = 75
    Height = 25
    Caption = 'OK'
    DoubleBuffered = True
    ModalResult = 1
    ParentDoubleBuffered = False
    TabOrder = 1
    DoBrowse = False
    BrowseStyle = fbOpen
  end
end
EndDialog>Dialog1

Show>Dialog1,r

GetDialogProperty>Dialog1,MSComboBox1,Text,strItemText
GetDialogProperty>Dialog1,MSComboBox1,ItemIndex,intSelectedIndex

MessageModal>Chosen Item: %intSelectedIndex%: %strItemText%
Note that if nothing was selected ItemIndex is -1.

Note also that with the default combo box the edit area can be modified and so it is possible to have returned -1 and a text value not in the list. You can set the box to NOT be editable by changing the Style property to csDropDownList.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

ABIVEN
Pro Scripter
Posts: 71
Joined: Sun Aug 07, 2005 7:22 pm
Location: PARIS

Using a drop-down list in a word document

Post by ABIVEN » Sun Jan 09, 2011 9:51 pm

Hello and excuse me

Suppose that you are in a word document and you stricly use (with a hotkey) the macro
Non-Modal Dialog Example ( help file : Dialog page).

You select 1 ; update and you get Fred 1 in the dialog box

How to directly retrieve Fred 1 in your word document ?

Thanks

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Jan 10, 2011 8:01 am

Can you clarify your question. Specifically what does this mean:
How to directly retrieve Fred 1 in your word document ?
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

ABIVEN
Pro Scripter
Posts: 71
Joined: Sun Aug 07, 2005 7:22 pm
Location: PARIS

Post by ABIVEN » Mon Jan 10, 2011 7:49 pm

For example -just in order to be very simple-

I am here in the reply post , I press the hotkey , I select in the combobox and it becomes :

I am here in the reply post good morning Fred 1 how do you do.

Thanks

Of course "good morning" would come from the first part of the macro "Fred 1" from the combobox part and "how do you do" the last part

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Jan 10, 2011 8:06 pm

So you could do something like:

SetFocus>Microsoft Word*
Send>good morning
Send>strItemText
Send>how do you do.

i.e. just send each part separately. If you prefer you can put it in one line like:

Send>good morning %strItemtext% how do you do.

Where strItemText comes from the GetDialogProperty function getting the value from the combo box.

Just make sure the correct window is focused.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

ABIVEN
Pro Scripter
Posts: 71
Joined: Sun Aug 07, 2005 7:22 pm
Location: PARIS

Post by ABIVEN » Tue Jan 11, 2011 2:18 pm

Thanks Marcus

Here an example of a drop down list :

Now fine with Word ;
but I would like to use it indifferently with word excel or ....for renaming a macro. Any Idea ?

Drop-Down List Example

Dialog>Dialog1
Caption= Drop-Down List
Top=113
Width=204
Left=16
Height=83

ComboBox=msComboBox1,54,22,91,select%CRLF%LOZERE%CRLF%VOSGES%CRLF%CORSE%CRLF%NORD

EndDialog>Dialog1

Show>Dialog1

let>a=1

Label>ActionLoop

Label>Loop
//Detect left mouse clicks
LibFunc>user32,GetAsyncKeyState,keystate,1
wait>0.1

If>keystate0
wait>0.1
let>a=a+1
endif

if>a=3,runaway

Goto>Loop

Label>runaway

GetDialogAction>Dialog1,r
wait>0.2
if>a=3,Update

Goto>ActionLoop


Label>Update
wait>0.1
SetFocus>Microsoft Word*
// This SetFocus>Microsoft Word* is HERE compulsory
wait>0.1
send>%Dialog1.msComboBox1%
wait>0.1
Exit

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Jan 11, 2011 4:23 pm

Instead of SetFocus you could get the active window before showing the dialog and then focus whatever that was:

GetActiveWindow>title,x,y
Show>Dialog1

..
..
..

SetFocus>title
Send>bla bla
...
...

Showing the dialog will of course change focus to it. So if we GetActiveWndow before showing the dialog and then focus whatever that window was, we can then send text to it. It now doesn't matter WHAT the window is.

Of course if you want it to only work with Excel or Word then you can proceed or not based on the contents of title returned by GetActiveWindow.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

ABIVEN
Pro Scripter
Posts: 71
Joined: Sun Aug 07, 2005 7:22 pm
Location: PARIS

Using a one-click drop-down list in any window

Post by ABIVEN » Tue Jan 11, 2011 6:53 pm

GREAT !

So one-click drop-down list in (quasi) any window Macro

Dialog>Dialog1
Caption= Drop-Down List
Top=113
Width=204
Left=16
Height=83

ComboBox=msComboBox1,54,22,91,select%CRLF%LOZERE%CRLF%VOSGES%CRLF%CORSE%CRLF%NORD

EndDialog>Dialog1
GetActiveWindow>title,x,y
Show>Dialog1

let>a=1

Label>ActionLoop

Label>Loop
//Detect left mouse click
LibFunc>user32,GetAsyncKeyState,keystate,1
wait>0.1

If>keystate0
wait>0.1
let>a=a+1
endif

if>a=3,runaway

Goto>Loop

Label>runaway
GetDialogAction>Dialog1,r
wait>0.2
if>a=3,Update

Goto>ActionLoop


Label>Update
wait>0.1
SetFocus>title
wait>0.2
send>%Dialog1.msComboBox1%
wait>0.1
Exit


Regards

.

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