Sunday, February 28, 2010

Silverlight Simple DataBinding in DataGrid

Silverlight at Visual Studio 2010 sample for simple binding a data to DataGrid. This sample is also for the beginners only. We will create a Databinding for DataGrid in a simple way using WCF and Linq to SQL.

For more details: http://docs.google.com/Doc?docid=0AQNzLAfQzOoaZGZrc25tY3BfMWhzMno3c2c4&hl=en

Tuesday, February 23, 2010

Browse Image and Drag It Silverlight

Hello Guys,

Early this morning I’m thinking about the draggable Image in Silverlight. I created a sample with simple snippets.

1. Create one Image, rectangle, and button.

2. Create an event handler for Image.

this.image1.MouseMove += new MouseEventHandler(image1_MouseMove);
this.image1.MouseLeftButtonDown += new MouseButtonEventHandler(image1_MouseLeftButtonDown);
this.image1.MouseLeftButtonUp += new MouseButtonEventHandler(image1_MouseLeftButtonUp);

3. Setting a variable

private bool isDragging = false; //dragging?
private Point mousePosition; //position of mouse

4. And now the events

void image1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
this.image1.ReleaseMouseCapture();
isDragging = false;
} //Release the mouse

void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
mousePosition = e.GetPosition(sender as UIElement);
this.image1.CaptureMouse();
isDragging = true;
} //Pressed the left mouse button

void image1_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
this.imgTranslateTransform.X = e.GetPosition(this).X - mousePosition.X;
this.imgTranslateTransform.Y = e.GetPosition(this).Y - mousePosition.Y;
}
} //Dragging the Image


private void btnBrowse_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();

BitmapImage imgSrc = new BitmapImage();
imgSrc.SetSource(ofd.File.OpenRead());
image1.Source = imgSrc;
textBlock1.Text = "You can now drag the Image";
} // Open/Browsing the Image (binding Image)

Friday, February 19, 2010

Simple Encrypt and Decrypt Password Silverlight

Hello Guys,


I have sample code here on how to create a simple encryption and decryption on Silverlight.
First, I created a PASSWORD from SL Controls. A Three Button for encryption, decryption, clear. A one textblock and one textbox.

Second, I created a simple function for Encryption and Decryption and variables.

byte[] _encryted;
byte[] _decryted;
string s;

private string EncrytedString(string str)
{
_encryted = System.Text.Encoding.Unicode.GetBytes(str);
s = Convert.ToBase64String(_encryted);
return s;
}

private string DecrytedString(string str)
{
_decryted = Convert.FromBase64String(str);
s = System.Text.Encoding.Unicode.GetString(_decryted, 0, _decryted.ToArray().Length);
return s;
}



Now the snippets.

//For the Encryption Button
private void btnEncrypt_Click(object sender, RoutedEventArgs e)
{
if (passwordBox1.Password == string.Empty)
{
MessageBox.Show("Please Enter your Password");
passwordBox1.Focus();
}
else
{
btnDecrypt.IsEnabled = true;
string s = EncrytedString(passwordBox1.Password);
txtEncrypted.Text = s;
}
}

//For the Decryption Button
private void btnDecrypt_Click(object sender, RoutedEventArgs e)
{
string s;
s = DecrytedString(txtEncrypted.Text);
MessageBox.Show(s);
}

//For the Clear Button
private void btnClear_Click(object sender, RoutedEventArgs e)
{
txtEncrypted.Text = string.Empty;
passwordBox1.Password = string.Empty;
passwordBox1.Focus();
btnDecrypt.IsEnabled = false;
}