Code conversion (C#/VB) in Silverlight
Written by Ian Elliot   
Tuesday, 24 August 2010
Article Index
Code conversion (C#/VB) in Silverlight
Layout and code
Trying it out

 

Banner

 

The file handlers

All that remains now is to define the code that loads a file to textBox1 and allows the user to save the contents of textBox2 to a file. There is nothing strange new or odd about this code - but it is relatively recent that Silverlight allowed users to save and load files. You might think that it would be a security risk but as the user picks the file to load and the location to save to it probably isn't too bad.

The load file button is easy enough. First we setup the parameters for the OpenFileDialog:

private void button2_Click(
object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog1 =
new OpenFileDialog();
openFileDialog1.Filter =
"All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = false;

You can set the Filter to a range of options .cs, .vb and so on but in this case All Files seems like a reasonable default. The user can also only select one file at a time - you could change this so that multiple files were loaded but it doesn't seem to be a useful option.

Next we show the dialog box and process the result:

 bool? userClickedOK = 
openFileDialog1.ShowDialog();
if (userClickedOK == true)
{
Stream fileStream =
openFileDialog1.File.OpenRead();
using (StreamReader reader =
new StreamReader(fileStream))
{
textBox1.Text = reader.ReadToEnd();
}
fileStream.Close();
}
}

Notice the use of the ReadToEnd method to avoid having to write an input loop with a test for EOF - such convienences are well worth having!

The save file routine is just as simple:

private void button3_Click(
object sender, RoutedEventArgs e)
{
SaveFileDialog saveFileDialog1 =
new SaveFileDialog();
saveFileDialog1.Filter =
"All Files (*.*)|*.*";
saveFileDialog1.FilterIndex = 1;
bool? userClickedOK =
saveFileDialog1.ShowDialog();
if (userClickedOK == true)
{
Stream fileStream =
saveFileDialog1.OpenFile();
using (StreamWriter writer =
new StreamWriter(fileStream))
{
writer.Write(textBox2.Text);
}
fileStream.Close();
}
}

Again notice the use of Write to simply write everything in the TextBox out to the file without having to write a loop.

Trying it out

You can see the convertor in action in the following screen dump - converting its own code into VB .NET

convert

 

Of course as this is a Silverlight application you can try it out for yourself at our Resources and Tool section .  You need to keep in mind the initial comments about only trying to convert complete classes and only converting correct code.


To access the code for this project, once you have registered,  click on CodeBin.

 

If you would like to be informed about new articles on I Programmer you can either follow us on Twitter, on Facebook or you can subscribe to our weekly newsletter.

 

Banner


The Minimum Spanning Tree - Prim's Algorithm In Python

Finding the minimum spanning tree is one of the fundamental algorithms and it is important in computer science and practical programming. We take a look at the theory and the practice and discover how [ ... ]



Setting Up Site-To-Site OpenVPN

Setting up a point-to-point VPN is relatively easy but site-to-site is much more complicated involving certificates and more IP addresses than you can count. Find out how to do it using OpenVPN.


Other Projects

<ASIN:1430229799 >

<ASIN:1430224258 >

<ASIN:0470650923 >

<ASIN:1847199844 >

<ASIN:143022455X >



Last Updated ( Tuesday, 24 August 2010 )