Archive

Posts Tagged ‘Silverlight’

Silverlight: Create Popup Dialog using ChildWindow Class


This post is a walkthrough to create a login screen popup dialog using the ChildWindow Class

  • Right click your solution or folder
  • Add a new item
  • Select Silverlight from the right list of the Add New Item dialog

clip_image002

  • Add a Silverlight Child Window

clip_image004

  • Add couple of labels, a Textbox and a Password box

clip_image006

In this scenario, I have the username and password hardcoded in the code. Ideally, you validate the username and password via a service

  • Add the following code to the OK and Cancel Click event handlers
private void OKButton_Click(object sender, RoutedEventArgs e)
{
   if (textBox1.Text == "Username" && textBox2.Password == "Password")
   {
      this.DialogResult = true;
   }
   else
   {
      MessageBox.Show("Incorrect username and/or password", "Error", MessageBoxButton.OK);
   }
}

private void CancelButton_Click(object sender, RoutedEventArgs e)
{
   this.DialogResult = false;
}

In code, I had a login button on the MainView.

private void btnAdmin_Click(object sender, RoutedEventArgs e)
{
   var dialog = new LoginDlg();
   dialog.Closed += new EventHandler(dialog_Closed);
   dialog.Show();
}
void dialog_Closed(object sender, EventArgs e)
{
   var dialog = (LoginDlg)sender;
   if (dialog.DialogResult == true)
   {
      //Authorized
   }
   dialog.Closed -= new EventHandler(dialog_Closed);
}

Notice that the Show method doesn’t return a value. I have to listen/subscribe/handle the closed event.

Advertisements
Categories: .NET, Silverlight Tags: