Jump to content

Right click and copy to clipboard


wrichards
 Share

Recommended Posts

I am trying to write a system that will take a name and return a list of members of staff. The idea is that users can right click on a row and the email address is copied to the clipboard. I have the following code:

 

#include <GUIConstantsEx.au3>
#include <mssql.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <WindowsConstants.au3>
#include <AutoItConstants.au3>
#include <GuiListView.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, 550, 160)
   Global $idListview = GUICtrlCreateListView("Deparment|E-Mail Address|Name|Telephone Number", 10, 10, 530, 150)
   Global $get_email
   for $count = 1 to $rset step 1
      GUICtrlCreateListViewItem($result[$count][0] & "|" & $result[$count][2] & "|" & $result[$count][3] & "|" & $result[$count][1], $idListview)
   Next
   Global $context  = GUICtrlCreateContextMenu($idListview)
   Global $menuitem = GUICtrlCreateMenuItem("Copy email address", $context)
   Global $copytext
   GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
   GUISetState(@SW_SHOW)
   GUISetState()
EndIf

While 1
    Switch GUIGetMsg()
        Case -3
            Exit
        Case $menuitem
            _ShowText()
    EndSwitch
 WEnd



 Func _ShowText()
    ClipPut($result[1][2])
EndFunc

Even though my code does work, I am unsure how to get the row the user has highlighted. Can anyone provide any answers?

Link to comment
Share on other sites

You can use _GUICtrlListView_GetSelectedIndices to get the selected row(s).

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <WindowsConstants.au3>
#include <AutoItConstants.au3>
#include <GuiListView.au3>

Global Const $VALUE_DEPARTMENT = 0
Global Const $VALUE_EMAIL = 1
Global Const $VALUE_NAME = 2
Global Const $VALUE_PHONE = 3

Global $title = "E-Mail address lookup"
Global $result[][4] = [["Dept1", "User1@mail.com", "User1", "555-555-5555"], ["Dept2", "User2@mail.com", "User2", "555-555-5555"], ["Dept3", "User3@mail.com", "User3", "555-555-5555"]]

Global $rset = UBound($result) - 1
Global $ControlID = GUICreate($title, 550, 160)
Global $idListview = GUICtrlCreateListView("Deparment|E-Mail Address|Name|Telephone Number", 10, 10, 530, 150)
Global $get_email
For $count = 0 To $rset
    GUICtrlCreateListViewItem($result[$count][0] & "|" & $result[$count][2] & "|" & $result[$count][3] & "|" & $result[$count][1], $idListview)
Next
Global $context = GUICtrlCreateContextMenu($idListview)
Global $menuitem = GUICtrlCreateMenuItem("Copy email address", $context)
Global $copytext
GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case -3
            Exit
        Case $menuitem
            _ShowText(_GUICtrlListView_GetSelectedIndices($idListview))
    EndSwitch
WEnd

Func _ShowText($vSelected)
    Local $sEmails = ""
    Local $sNames = ""
    If (IsArray($vSelected)) Then
        ; In case multiple items are selected
        For $i = 0 to UBound($vSelected) -1
            $sNames &= $result[$vSelected][$VALUE_NAME] & ", "
            $sEmails &= $result[$vSelected][$VALUE_EMAIL] & ", "
        Next
        $sEmails = StringTrimRight($sEmails, 2)
        $sNames = StringTrimRight($sNames, 2)
        ConsoleWrite($sNames & " put in clipboard" & @CRLF)
        ClipPut($sEmails)
    Else
        ConsoleWrite($result[$vSelected][$VALUE_NAME] & " put in clipboard" & @CRLF)
        ClipPut($result[$vSelected][$VALUE_EMAIL])
    EndIf
EndFunc   ;==>_ShowText

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...