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


Getting Started with Google Earth

Google Earth is more capable than Google Maps, but to use it you have to download a plug-in and learn a new API. This article explains that it's not so difficult and there are some easy to understand  [ ... ]



Getting started with Google Maps JavaScript

Google Maps is the basis of all Google Mapping products and it's easy to use in your website or web app. We take a quick look at how to get started and what features you can take advantage of.


Other Projects

<ASIN:1430229799 >

<ASIN:1430224258 >

<ASIN:0470650923 >

<ASIN:1847199844 >

<ASIN:143022455X >



Last Updated ( Tuesday, 24 August 2010 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.