如果语句不工作 AutoIT

If statement not working AutoIT

我在编写代码时遇到问题。我有一个 table,其中列出了工作人员的详细信息。当您 select 记录并右键单击时,我正在尝试将电子邮件地址复制到剪贴板。我有以下代码:

 #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

我原以为 for 循环中的 IF 语句可以解决问题,但是,当我 运行 我的代码时,它只是复制最后一行电子邮件地址列中的任何内容,而实际上我想要它可以在您右键单击特定行时复制电子邮件地址,但我不确定该怎么做。

您的代码有两个主要问题。第一个是 MouseClick() 不检查是否按下鼠标按钮,而是发送鼠标按钮单击。由于发送鼠标按钮点击将成功,因此 MouseClick($MOUSE_CLICK_RIGHT)==1 将评估为真。对于您创建的每个列表视图项目,您都将电子邮件地址放在剪贴板上。

第二个问题是if语句放错了地方。它在您创建每个列表视图项后立即执行。

相反,如下更改您的 While 语句

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