Thursday, 10 January 2013

Send an Email in Lightswitch


How to send HTML mail in Visual Studio Lighswitch?

 For this you have to import two class in your application

using System.Net;
using System.Net.mail;

 public static void CreateOutlookEmail(string toAddress, string subject, string body)
       {
           dynamic olMailItem = 0;
           dynamic olFormatPlain = 1;
           dynamic olFormatHTML = 2;
           try
           {
               dynamic outlook;
               if (AutomationFactory.IsAvailable)
               {
                   try
                   {
                       // Get the reference to the open Outlook App
                       outlook = AutomationFactory.GetObject("Outlook.Application");
                   }
                   catch
                   {
                       // If Outlook isn't open, then an error will be thrown.
                       //  Try to open the application
                       outlook = AutomationFactory.CreateObject("Outlook.Application");
                   }
                   if (outlook != null)
                   {
                       // Create the email
                       //  Outlook object model (OM) reference:
                       //  msdn.microsoft.com/.../ff870566.aspx
                       dynamic mail = outlook.CreateItem(olMailItem);
                       // With...
                       if (body.ToLower().Contains("<html>"))
                       {
                           mail.BodyFormat = olFormatHTML;
                           mail.HTMLBody = body;
                       }
                       else
                       {
                           mail.BodyFormat = olFormatPlain;
                           mail.Body = body;
                       }
                       mail.Recipients.Add(toAddress);
                       mail.Subject = subject;
                       mail.Save();
                       mail.Display();
                       // mail.Send()
                   }
               }
           }
           catch (Exception ex)
           {
               throw new InvalidOperationException("Failed to create email.", ex);
           }
       }

  for more detail please Click Here.

Saturday, 5 January 2013

Drag and Drop in Silverlight with example

1. Create a Rectangle and set the RenderTransform to a TranslateTransform.

<Rectangle x:Name="rect" Width="50" Height="50" Fill="Blue" HorizontalAlignment="Left" VerticalAlignment="Top">
            <Rectangle.RenderTransform>
                <TranslateTransform x:Name="rectTranslateTransform" X="0" Y="0" />
            </Rectangle.RenderTransform>
    </Rectangle>


2. Register the MouseLeftButtonDown, MouseLeftButtonUp, and the MoseMove events for the rectangle.

  this.rect.MouseLeftButtonDown += new MouseButtonEventHandler(rect_MouseLeftButtonDown);
  this.rect.MouseLeftButtonUp += new MouseButtonEventHandler(rect_MouseLeftButtonUp);
  this.rect.MouseMove += new MouseEventHandler(rect_MouseMove);


3. In the MouseLeftButtonDown and MouseLeftButtonUp event you will capture and release the capture of the mouse, respectively.  Also, you will need to store if the mouse is captured.  


// Stores if the mouse is captured
private Boolean isRectMouseCapture = false;

void rect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    this.rect.ReleaseMouseCapture();
    isRectMouseCapture = false;
}

void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    this.rect.CaptureMouse();
    isRectMouseCapture = true;
}
 
4. Finally, update the position if the mouse is captured and the mouse is moving.

void rect_MouseMove(object sender, MouseEventArgs e)
{
    if (isRectMouseCapture)
    {
        this.rectTranslateTransform.X = e.GetPosition(this).X;
        this.rectTranslateTransform.Y = e.GetPosition(this).Y;
    }
}
 

Results.


 

Maintain mouse position on object while dragging

The above example snaps the pointer to the top left of the object.  Face it, it’s annoying, what you really want is the mouse to maintain the position on the element while dragging. To solve this problem is simple.

1. Add a variable to save the position when the user clicks the object.

private Point clickPosition;

2. Save the position of where the mouse is located on the MouseLeftButtonDown event.  The updated MouseLeftButtonDown event looks like:

void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    clickPosition = e.GetPosition(sender as UIElement);
    this.rect.CaptureMouse();
    isRectMouseCapture = true;
}

3. Finally, in MouseMove event subtract the clickPosition from the mouse position.
    this.rectTranslateTransform.X = e.GetPosition(this).X - clickPosition.X;
    this.rectTranslateTransform.Y = e.GetPosition(this).Y - clickPosition.Y;