wrichards0
wrichards0

Reputation: 187

If statement not working AutoIT

I am having trouble with the code I am writing. I have a table which lists staff member details. I am trying to have an email address copy to the clipboard when you select a record and right click. I have the following code:

 #include <GUIConstantsEx.au3>
 #include <mssql.au3>
 #include <MsgBoxConstants.au3>
 #include <Array.au3>
 #include <WindowsConstants.au3>
 #include <AutoItConstants.au3>
 global $title = "E-Mail address lookup"
 global $name = InputBox($title,"Please type the name of the person you wish to find")
 global $sqlCon = _MSSQL_Con("server", "username", "password", "directory-plus")
 global $result = _MSSQL_GetRecord($sqlCon, "autoit_view","*", "WHERE cn LIKE '%" & StringStripWS($name,3) & "%'")
 if StringLen(StringStripWS($name,3)) < 1 then
 MsgBox(0, $title, "Name cannot be empty")
 Else
 Global $rset = UBound($result) - 1
 Global $ControlID = GUICreate($title, 500, 150)
 Global $idListview = GUICtrlCreateListView("Deparment|E-Mail Address|Name|Telephone Number", 10, 10, 480, 150)
 for $count = 1 to $rset step 1
      GUICtrlCreateListViewItem($result[$count][0] & "|" & $result[$count][2] & "|" & $result[$count][1] & "|" & $result[$count][2], $idListview)
      if MouseClick($MOUSE_CLICK_RIGHT)==1 Then
           ClipPut($result[$count][2])
      EndIf
 Next
 GUISetState(@SW_SHOW)
 GUISetState()

While 1
Global $Msg = GUIGetMsg()
Switch $Msg
    Case -3, $ControlID
        Exit
EndSwitch
 WEnd
EndIf

I would have thought my IF statement within the for loop would do the trick but, when I run my code, it just copies whatever is in email address column in the last row when in fact I want it to copy the email address when you right click on a particular row but I'm not sure how to do it.

Upvotes: 0

Views: 372

Answers (1)

Richard
Richard

Reputation: 1131

Your code has two major problems. The first is that MouseClick() doesn't check for the mouse button being pressed, but instead sends a mouse button click. Since sending a mouse button click is going to succeed, MouseClick($MOUSE_CLICK_RIGHT)==1 will evaluate to true. For each list view item you create, you are putting the email address on the clipboard.

The second problem is that the if statement is in the wrong place. It's executed just after you create each list view item.

Instead, change your While statement as follows

While 1
 Global $Msg = GUIGetMsg()
 Switch $Msg
  Case -3, $ControlID
   Exit
  case $GUI_EVENT_SECONDARYDOWN   
   $selecteditem=StringSplit(GUICtrlRead(GUICtrlRead($idListview)),"|")
   if @error=0 and $selecteditem[0]>1 Then
    ClipPut($selecteditem[2])
   EndIf
 EndSwitch
WEnd

Upvotes: 1

Related Questions