2 New Text Opening Files Text Editor C
>> YOUR LINK HERE: ___ http://youtube.com/watch?v=WQV3csBrRw8
Let the user create new text and open text files. • Subscribe! ► https://www.youtube.com/c/QuasimodoPr... • Add a new item to the File Menu and name it “New”. You can easily change the order of such items. Also, we will add the “Open” item. • Now, double-click on “New”. We will need a string variable to store the CurrentFile name in it. Assign it an empty value. • In the “New” click event add a condition. If richttextbox contains some text, then we’ll warn the user about it and ask whether he/she wants to continue. If he/she hits the OK button, the app will erase the text. In other words, that will be a new text. Also, we will erase the value of the CurrentFile variable. • Now, let’s edit the “Open” item click event. Put your code into the try-catch block to make sure that the app won’t crash if there’s a problem with the file. We will write a condition in the “try” part. But before that add an “openfiledialog” control to the form. I’ll remove 1 from its name since we’ll use only 1 openfiledialog. Go back to the condition. • if(DialogResult.OK == openFileDialog.ShowDialog()) • then assign currentfile to openFileDialog.FileName. • And now we’ll make conditions for file types, since richTextBox supports different formats. For this purpose, we need to add System.IO to the .cs file of this form. You can write it manually at the top of your file or click a few buttons to let the Visual Studio add it for you. In different versions of Visual Studio, it is implemented in different ways. After that write: • if(Path.GetExtension(CurrentFile) == “.txt” || if(Path.GetExtension(CurrentFile) == “.cs”) • then call the richtextbox.LoadFile() method. In brackets, you write CurrentFile as the first argument. The second argument is how it should be opened. Write RichTextBoxStreamType.PlainText. • else richTextBox1.LoadFile(CurrentFile); //by default, richtextbox loads files as rtfs. • Now, let’s add the file’s name to the Window Title. • As you remember, I spoke about the try-catch block. Try and catch are always used together. “Try” is for action which is performed, and “catch” is for action which will be performed, if the action in “try” causes errors. That’s a good way to test your apps, since you can catch specific exceptions. If you need brief information, use the .Message property of the Exception. • Now your text editor can open txt, cs, and rtf files. • In the next part, you will deal with saving files and closing your app. • reddit - / learn_csharp • More Text Editor ► • How to Create a Text Editor • Latest code: https://github.com/QuasimodoPrograms/...
#############################
![](http://youtor.org/essay_main.png)