Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, March 4, 2010

Autocomplete in Silverlight with Visual Studio 2010

Last week I keep searching on how to use the autocomplete in silverligth with visual studio 2010 but most of the examples that I find they are using a textbox or combobox for the autocomplete. I tried to study those examples and apply to the single autocomplete from tools on my silverlight project. And now this is the result. I will use a database again from my previous post (Silverlight Simple DataBinding in DataGrid) to show how the autocomplete works with database.


This is the output:





















First, this is the setup for my autocomplete:

//The tags for autocompletebox on XAML



Second, my simple snippets:

//Event for the autocomplete to send a text string to my function
private void autoCompleteBox1_KeyUp(object sender, KeyEventArgs e)
{
autoCompleteBox1.Populating += (s, args) =>
{
args.Cancel = true;
var c = new Service1Client();
c.GetListByNameCompleted +=new EventHandler(c_GetListByNameCompleted);
c.GetListByNameAsync(autoCompleteBox1.Text);
};
}

//Getting result from database
void c_GetListByNameCompleted(object sender, GetListByNameCompletedEventArgs e)
{
autoCompleteBox1.ItemsSource = e.Result;
autoCompleteBox1.PopulateComplete();
}


The snippets above will show on how to use the autocompleteBox using the data from database that bind in DataGrid. But what if we want to show the result on DataGrid while the autocomplete changing the items source?

Ok just add one line to c_GetListByNameCompleted

void c_GetListByNameCompleted(object sender, GetListByNameCompletedEventArgs e)
{
autoCompleteBox1.ItemsSource = e.Result;
autoCompleteBox1.PopulateComplete();
dataGrid1.ItemsSource = e.Result;
}




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;
}