An Encrypted String Data Type for Visual Studio LightSwitch: Listing 3

Calling the EncryptedString methods.

// Initialize the encryption logic once per control

// C# class
// BlankModule.EncryptionMethods ec = new BlankModule.EncryptionMethods();

// Visual Basic class
EncryptString ec = new EncryptString();

public EncryptedStringControl()
{
  InitializeComponent();
}

private void MyBizControl_GotFocus(object sender, RoutedEventArgs e)
{
  TextBox control = (TextBox)sender;
  string result = control.Text;

  if (string.IsNullOrEmpty(control.Text) == false &&
    control.Text.EndsWith("="))
  {
    result = ec.DecryptString(control.Text);
  }
  control.Text = result;
}


private void MyBizControl_LostFocus(object sender, RoutedEventArgs e)
{
  TextBox control = (TextBox)sender;
  string result = control.Text;

  if (string.IsNullOrEmpty(control.Text) == false &&
    control.Text.EndsWith("=") == false)
  {
    result = ec.EncryptString(control.Text);
  }
  control.Text = result;
}

About the Author

Joe Kunk is a Microsoft MVP in Visual Basic, three-time president of the Greater Lansing User Group for .NET, and developer for Dart Container Corporation of Mason, Michigan. He's been developing software for over 30 years and has worked in the education, government, financial and manufacturing industries. Kunk's co-authored the book "Professional DevExpress ASP.NET Controls" (Wrox Programmer to Programmer, 2009). He can be reached via email at joekunk@gmail.com.