About combo box items ?

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Okapi
Junior Coder
Posts: 28
Joined: Wed Dec 29, 2010 1:22 am

About combo box items ?

Post by Okapi » Fri Apr 08, 2011 2:37 pm

Hi all,
I've created a combo box with two items with the Dialog Designer like in code below. Now I want when an item is validated (selected then Enter), do something and combo box is closed. Can't find that in help. Thanks

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Left = 251
  Top = 104
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'IDs'
  ClientHeight = 145
  ClientWidth = 709
  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 = 4
    Top = 1
    Width = 701
    Height = 21
    AutoDropDown = True
    AutoCloseUp = True
    ItemHeight = 13
    TabOrder = 0
    Items.Strings = (
      'John'
      'Mark')
    ListText =
      'John'#13 +
      #10'Mark'#13#10
  end
end
EndDialog>Dialog1

Show>Dialog1,

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Apr 08, 2011 3:45 pm

Replace your Show>Dialog, line with the following. See HELP for AddDialogHandler

Code: Select all

AddDialogHandler>Dialog1,MSComboBox1,OnKeyPress,Close

Show>Dialog1,res1

SRT>Close
  SetDialogProperty>Dialog1,,Visible,False
  GetDialogProperty>Dialog1,MSComboBox1,Text,Result
  MDL>Result
  Exit>0
END>Close

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Apr 08, 2011 5:26 pm

A better option might be to add a sizeless button whose "Default" property is set to True. This makes the act of pressing Enter default to the button if the dialog is the currently active window. Then use AddDialogHandler with the button's OnClick property to process the subroutine

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Left = 487
  Top = 223
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'IDs'
  ClientHeight = 145
  ClientWidth = 709
  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 = 4
    Top = 1
    Width = 701
    Height = 21
    AutoDropDown = True
    AutoCloseUp = True
    ItemHeight = 13
    TabOrder = 0
    Items.Strings = (
      'John'
      'Mark')
    ListText = 'John'#13#10'Mark'#13#10
  end
  object MSButton1: tMSButton
    Width = 0
    Height = 0
    Default = True
  end
end
EndDialog>Dialog1

AddDialogHandler>Dialog1,MSButton1,OnClick,Close
AddDialogHandler>Dialog1,,OnClose,Quit

Show>Dialog1,res1

SRT>Close
  SetDialogProperty>Dialog1,,Visible,False
  GetDialogProperty>Dialog1,MSComboBox1,Text,Result
  MDL>Result
  Exit>0
END>Close

SRT>Quit
  Exit>0
END>Quit

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