CNS 1400: Lab #26
The objective of this lab is to learn how to use the File Open
Dialog and read data from a file and display it in a TextBox.
Create a new Windows Forms Application. Add a MenuStrip to the Form and include the standard Exit and About
Add a File option to the
MenuStrip. Under the File option add a menu item with the text Open.
Then place a TextBox control someplace in the middle of the Form.
When you are done, your interface should look something like the image below:
Adding a File Open Dialogue
- Double click on the Open menu item.
- This will open up the code editor and create a method to handle the event that will get
generated when a user clicks on Open.
- Here you will add the code to create a File Open Dialogue box and read some data from the file.
When you are done, your event handler code should look something like this (I have added some
line numbers for convenience in discussing the code):
1 private void openToolStripMenuItem_Click(object sender, EventArgs e)
3 Stream myStream = null;
4 OpenFileDialog openFileDialog1 = new OpenFileDialog();
6 openFileDialog1.InitialDirectory = "c:\\" ;
7 openFileDialog1.Filter = "text files (*.txt)|*txt" ;
8 if(openFileDialog1.ShowDialog() == DialogResult.OK)
10 if ((myStream = openFileDialog1.OpenFile()) != null)
12 StreamReader data = new StreamReader(myStream);
13 textBox.Text = data.ReadLine( );
- Be sure to add a using System.IO; statement at the top of the file.
- Line 3 declares a Stream reference. The Open method returns a Stream reference that we will store in reference variable myStream.
- Line 4 creates an OpenFileDialogue object.
- Line 6 defines the initial directory to use when the File Open dialogue is displayed.
- Line 7 defines the file extensions to show in the file dialogue.
- Line 8 displays the File Open dialogue and checks the return value to make sure that this operation worked.
- Line 10 opens the file and assigns the reference to the stream object to myStream. If the file did not open
for some reason the reference will be null.
- Line 12 creates a StreamReader object using the Stream myStream as its parameter.
- Line 13 reads one line of data from the file and stores it in the Text property of the TextBox object. Here I have
named that TextBox object textBox.
Build test and your program to make sure it meets the specifications provided in this lab.
When you submit this lab, add a comment to your submission that answers the following questions.
(1) What class would you use when constructing an object to write data to a file?
(2) Write down one important reason for writing data to a file.
File(s) to Submit:
Place your complete project folder in a zip file and name the zip file lab_26_your-initials_V1.0.zip.
For example, I would name my file lab_26_RKD_V1.0.zip.
Submit this assignment as Lab #26 on Canvas.
|Assignment meets the submission guidelines.
o All source code files contain a complete file prologue
o All methods have a complete method prologue
o Source code files contain declaration that you did not copy code
o Project has been properly submitted to Canvas
o Code meets style guidelines
|The program works correctly and meets all requirements.
|You have answered the questions in this lab correctly.