Possible ComboBox and ListBox Bug?

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Possible ComboBox and ListBox Bug?

Post by Rain » Sat Sep 29, 2012 12:20 am

I'm getting 2 different window handles when using the dialogs Combo and List Boxes.

I wrote this script to illustrate rather than trying to explain the problem/bug. Can any of you duplicate the problem I'm experiencing?

Instructions:

  • 1. Run the Script below
    2. Select a window from the List Box
    3. Select the same window from the Combo Box
    4. View Result


When I run it I get the following results

Image

Code: Select all



Dialog>Dialog1
object Dialog1: TForm
  Left = 247
  Top = 96
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  ClientHeight = 378
  ClientWidth = 672
  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 Label1: TLabel
    Left = 8
    Top = 8
    Width = 191
    Height = 13
    Caption = 'Step 1. Select a window from the list box'
  end
  object Label2: TLabel
    Left = 8
    Top = 184
    Width = 248
    Height = 13
    Caption = 'Step 2. Select the same window from the combo box'
    Visible = False
  end
  object Label3: TLabel
    Left = 8
    Top = 128
    Width = 657
    Height = 41
    AutoSize = False
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label4: TLabel
    Left = 8
    Top = 224
    Width = 657
    Height = 105
    AutoSize = False
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
    Visible = False
    WordWrap = True
  end
  object MSListBox1: tMSListBox
    Left = 8
    Top = 24
    Width = 657
    Height = 97
    ItemHeight = 13
    ScrollWidth = 2000
    TabOrder = 0
    SelectedIndex = -1
  end
  object MSComboBox1: tMSComboBox
    Left = 8
    Top = 200
    Width = 657
    Height = 21
    DropDownCount = 50
    TabOrder = 1
    Text = 'MSComboBox1'
    Visible = False
  end
  object MSButton1: tMSButton
    Left = 8
    Top = 344
    Width = 121
    Height = 25
    Caption = 'Trye Again'
    TabOrder = 2
    Visible = False
    DoBrowse = False
    BrowseStyle = fbOpen
  end
end
EndDialog>Dialog1


AddDialogHandler>Dialog1,MSListBox1,OnClick,DoListBox
AddDialogHandler>Dialog1,MSComboBox1,OnChange,DoComboBox
AddDialogHandler>Dialog1,MSButton1,OnClick,TryAgain


GetWindowList>winlist
SetDialogProperty>Dialog1,MSListBox1,Text,Select a window from the list%CRLF%%winlist%
SetDialogProperty>Dialog1,MSListBox1,SelectedIndex,0
SetDialogProperty>Dialog1,MSComboBox1,ListText,Select the same window from this list%CRLF%%winlist%
SetDialogProperty>Dialog1,MSComboBox1,ItemIndex,0

Show>Dialog1,Result



SRT>DoListBox
GetDialogProperty>Dialog1,MSListBox1,SelectedIndex,SelectResult
If>SelectResult>0
GetDialogProperty>Dialog1,MSListBox1,SelectedItems,WindowResult
IfWindowOpen>%WindowResult%
Let>Name1Exists=1
GetWindowHandle>WindowResult,Name1_hwnd
Let>WIN_USEHANDLE=1
SetFocus>Name1_hwnd
Let>WIN_USEHANDLE=0
SetDialogProperty>Dialog1,Label3,Caption,Was Focus Set to "%WindowResult%"?%CRLF%Window handle: %Name1_hwnd%%CRLF%Continue with step 2
SetDialogProperty>Dialog1,Label2,Visible,True
SetDialogProperty>Dialog1,Label3,Visible,True
SetDialogProperty>Dialog1,MSButton1,Visible,True
SetDialogProperty>Dialog1,MSComboBox1,Visible,True
Endif
Endif
END>DoListBox





SRT>DoComboBox
GetDialogProperty>Dialog1,MSComboBox1,ItemIndex,SelectResult
If>SelectResult>0
GetDialogProperty>Dialog1,MSComboBox1,Text,WindowResult
IfWindowOpen>WindowResult
GetWindowHandle>WindowResult,Name2_hwnd
Let>WIN_USEHANDLE=1
SetFocus>Name2_hwnd
Let>WIN_USEHANDLE=0
SetDialogProperty>Dialog1,Label4,Visible,True
SetDialogProperty>Dialog1,Label4,Caption,Was Focus Set to "%WindowResult%"?%CRLF%Window handle: %Name2_hwnd%%CRLF%%CRLF%MSListBox1 Handle Result: %Name1_hwnd%%CRLF%MSComboBox1 Handle Result: %Name2_hwnd%%CRLF%Are the handles different and was focus set to the window both times?%CRLF%If your answer is NO then we have a problem!
Endif
Endif
END>DoComboBox




SRT>TryAgain
SetDialogProperty>Dialog1,Label2,Visible,False
SetDialogProperty>Dialog1,Label3,Visible,False
SetDialogProperty>Dialog1,Label4,Visible,False
SetDialogProperty>Dialog1,MSButton1,Visible,False
SetDialogProperty>Dialog1,MSComboBox1,Visible,False

GetWindowList>winlist
SetDialogProperty>Dialog1,MSListBox1,Text,Select a window from the list%CRLF%%winlist%
SetDialogProperty>Dialog1,MSListBox1,SelectedIndex,0
SetDialogProperty>Dialog1,MSComboBox1,ListText,Select the same window from this list%CRLF%%winlist%
SetDialogProperty>Dialog1,MSComboBox1,ItemIndex,0
END>TryAgain




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

Post by JRL » Sat Sep 29, 2012 3:21 am

Rain,

Added one line to your "DoComboBox" subroutine and the results are consistent from listbox to combobox.

I don't believe this is a bug, its a Windows object thing. If you leave the dialog open after picking the same window name from the combobox (step three) look up the second erroneous handle using the "View System Windows" tool and you'll see that the erroneous handle belongs to this script's MScombobox1. You'll also see that the name of MScombobox1 is the same as the name of the window selected in the combobox. Since the combobox was the last window focused and since it had the same name as the window we want, it was the window whose handle was captured in the "DoComboBox" subroutine. By changing the name of the combobox to something benign just before the GetWindowHandle> line, we eliminate the combobox from the list of windows available to be selected.

Hope this makes sense.


Code: Select all

SRT>DoComboBox
GetDialogProperty>Dialog1,MSComboBox1,ItemIndex,SelectResult
If>SelectResult>0
GetDialogProperty>Dialog1,MSComboBox1,Text,WindowResult
IfWindowOpen>WindowResult

//Added this line
SetDialogProperty>Dialog1,MSComboBox1,Text,No Longer the same name


GetWindowHandle>WindowResult,Name2_hwnd
Let>WIN_USEHANDLE=1
SetFocus>Name2_hwnd
Let>WIN_USEHANDLE=0
SetDialogProperty>Dialog1,Label4,Visible,True
SetDialogProperty>Dialog1,Label4,Caption,Was Focus Set to "%WindowResult%"?%CRLF%Window handle: %Name2_hwnd%%CRLF%%CRLF%MSListBox1 Handle Result: %Name1_hwnd%%CRLF%MSComboBox1 Handle Result: %Name2_hwnd%%CRLF%Are the handles different and was focus set to the window both times?%CRLF%If your answer is NO then we have a problem!
Endif
Endif
END>DoComboBox

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Mon Oct 01, 2012 4:16 pm

JRL, thanks for your help.
You are right, it's not a bug. I think I had similar problem a couple of years ago but I was able to figure it out on my own. I wish I would have remembered, it would have saved me a couple of hours of debugging and messing with my code.

Another way to "fix" (See example below) it so combo box is not confused as a window is to Set WF_TYPE to 0 (No Child Windows) before retrieving the window handle and then back to 1 (Default) ALL Windows after retrieving the window handle of the selected window.

Code: Select all


Dialog>Dialog1
object Dialog1: TForm
  Left = 247
  Top = 96
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  ClientHeight = 378
  ClientWidth = 672
  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 Label1: TLabel
    Left = 8
    Top = 8
    Width = 191
    Height = 13
    Caption = 'Step 1. Select a window from the list box'
  end
  object Label2: TLabel
    Left = 8
    Top = 184
    Width = 248
    Height = 13
    Caption = 'Step 2. Select the same window from the combo box'
    Visible = False
  end
  object Label3: TLabel
    Left = 8
    Top = 128
    Width = 657
    Height = 41
    AutoSize = False
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label4: TLabel
    Left = 8
    Top = 224
    Width = 657
    Height = 105
    AutoSize = False
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
    Visible = False
    WordWrap = True
  end
  object MSListBox1: tMSListBox
    Left = 8
    Top = 24
    Width = 657
    Height = 97
    ItemHeight = 13
    ScrollWidth = 2000
    TabOrder = 0
    SelectedIndex = -1
  end
  object MSComboBox1: tMSComboBox
    Left = 8
    Top = 200
    Width = 657
    Height = 21
    DropDownCount = 50
    TabOrder = 1
    Text = 'MSComboBox1'
    Visible = False
  end
  object MSButton1: tMSButton
    Left = 8
    Top = 344
    Width = 121
    Height = 25
    Caption = 'Trye Again'
    TabOrder = 2
    Visible = False
    DoBrowse = False
    BrowseStyle = fbOpen
  end
end
EndDialog>Dialog1


AddDialogHandler>Dialog1,MSListBox1,OnClick,DoListBox
AddDialogHandler>Dialog1,MSComboBox1,OnChange,DoComboBox
AddDialogHandler>Dialog1,MSButton1,OnClick,TryAgain


GetWindowList>winlist
SetDialogProperty>Dialog1,MSListBox1,Text,Select a window from the list%CRLF%%winlist%
SetDialogProperty>Dialog1,MSListBox1,SelectedIndex,0
SetDialogProperty>Dialog1,MSComboBox1,ListText,Select the same window from this list%CRLF%%winlist%
SetDialogProperty>Dialog1,MSComboBox1,ItemIndex,0

Show>Dialog1,Result



SRT>DoListBox
GetDialogProperty>Dialog1,MSListBox1,SelectedIndex,SelectResult
If>SelectResult>0
GetDialogProperty>Dialog1,MSListBox1,SelectedItems,WindowResult
IfWindowOpen>%WindowResult%
GetWindowHandle>WindowResult,Name1_hwnd
Let>WIN_USEHANDLE=1
SetFocus>Name1_hwnd
Let>WIN_USEHANDLE=0
SetDialogProperty>Dialog1,Label3,Caption,Was Focus Set to "%WindowResult%"?%CRLF%Window handle: %Name1_hwnd%%CRLF%Continue with step 2
SetDialogProperty>Dialog1,Label2,Visible,True
SetDialogProperty>Dialog1,Label3,Visible,True
SetDialogProperty>Dialog1,MSButton1,Visible,True
SetDialogProperty>Dialog1,MSComboBox1,Visible,True
Endif
Endif
END>DoListBox





SRT>DoComboBox
GetDialogProperty>Dialog1,MSComboBox1,ItemIndex,SelectResult
If>SelectResult>0
GetDialogProperty>Dialog1,MSComboBox1,ListText,TheWindows
GetDialogProperty>Dialog1,MSComboBox1,Text,WindowResult
SetDialogProperty>Dialog1,MSComboBox1,Text,

IfWindowOpen>WindowResult
/*
Adding Let>WF_TYPE=0 (No Child Windows) ensures that the selected 
text of the combo box is not confused as a window.
*/
Let>WF_TYPE=0
GetWindowHandle>WindowResult,Name2_hwnd
Let>WIN_USEHANDLE=1
SetFocus>Name2_hwnd
Let>WIN_USEHANDLE=0
//Set WF_TYPE back to default
Let>WF_TYPE=1
SetDialogProperty>Dialog1,MSComboBox1,ListText,TheWindows
SetDialogProperty>Dialog1,MSComboBox1,ItemIndex,SelectResult
SetDialogProperty>Dialog1,Label4,Visible,True
SetDialogProperty>Dialog1,Label4,Caption,Was Focus Set to "%WindowResult%"?%CRLF%Window handle: %Name2_hwnd%%CRLF%%CRLF%MSListBox1 Handle Result: %Name1_hwnd%%CRLF%MSComboBox1 Handle Result: %Name2_hwnd%%CRLF%Are the handles different and was focus set to the window both times?%CRLF%If your answer is NO then we have a problem!
Endif
Endif
END>DoComboBox

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