Script Example

 

For the complete list of Designer sample codes, please visit the Cognex Designer Support site.

The example below is taken from the AuditTrail sample sample application:


// must have user and password in local user database  - see user management help topic 
// place in script for mousedown action of a command button 


// will need to replace hardcoded variables in the if statement with text from a textbox and a passwordbox 
// example reference:  $Pages.Page.TextBoxUserName.Text and $Pages.Page.passwordbox.Text
string username = "Engineer";
string password = "eng";

//-------------------
//The Login And Log function

// Get current user before login starts
string prevUser = $System.Users.CurrentUsername;

if ($System.Users.Login(username, password))
{
	string msg = prevUser + " logged in as " + username + ".";
	$System.AuditTrail.Log(prevUser + " logged in as " + username, "Users");
	$HMI.ShowInformation( username + " has been successfully logged in.", "Login Success");
	return true;
}
else
{
	string msg = prevUser + " failed to login as " +  username + ".";
	$System.AuditTrail.Log(msg, "Users");
	$HMI.ShowError(msg, "Login Failed");
	return false;
}