Notepad Application Code Example
Notepad Application Code Example
The application uses a ColorDialog, which prompts the user to select a color. If the dialog returns OK and if there is any text selected in the TextArea, the selected text's color is changed to the color the user chose .
The open file functionality employs an OpenFileDialog to select a .txt file, setting the default directory to 'C:/Desktop'. Upon a valid file selection ('if (ofd.FileName != "")'), the application reads all lines from the file and iteratively appends each line plus a newline character to the TextArea. This ensures that the file's content is accurately loaded and displayed .
In the copy operation, only the selected text part is assigned to 'copyText', leaving 'cutText' as an empty string. Unlike cutting, the text remains unchanged in the TextArea after being copied. This differentiation ensures original text remains and the selected portion is stored for subsequent paste operations .
The application employs multiple namespaces such as System, System.IO, and System.Windows.Forms, among others. System.IO is crucial for file operations, providing classes for reading and writing files. System.Windows.Forms is essential for GUI components, enabling event-driven operations such as clicks and input handling. Together, these libraries enable a wide array of functionalities ranging from basic file management to complex UI interactions .
Both SaveFileDialog and OpenFileDialog serve as interface elements for user-friendly file handling. SaveFileDialog allows specifying file destination and name before writing content, ensuring careful file management. OpenFileDialog facilitates file selection for reading, with defined filters and directories to streamline user navigation and file access, thus enhancing overall user experience through convenience and clear navigation .
The undo functionality relies on a Stack of Actions to reverse operations. However, the existing code only pops an action from the stack without actually executing any logic to reverse the changes made. This suggests a limitation in its current implementation, where it lacks the necessary commands to revert state changes, making the undo operation incomplete and potentially ineffective .
The application allows font changes if the FontDialog returns OK from the user's font selection and if there is any selected text in the TextArea. The selected text's font is then changed to the font chosen by the user in the FontDialog .
The cutting function assigns the entirety of 'TextArea.Text' to the 'cutText' variable and clears 'copyText'. It then deletes the selected text from the textarea. For pasting, if 'cutText' is not empty, it inserts 'cutText' at the current selection start in the textarea; otherwise, it inserts contents of 'copyText' if 'cutText' is empty, reflecting typical cut-paste and copy-paste operations .
The Save functionality uses a SaveFileDialog to prompt the user to choose a location and name for the file to be saved. It filters the file types to 'Text file|*.txt' and sets the initial directory to 'C:/'. Once a valid file name is provided and the dialogue box is closed (hence the condition 'if(sfd.FileName != "")'), it opens a FileStream and writes the text area content into the file using ASCII encoding. Finally, it closes the file stream to ensure data is properly written and resources are released .
Actions are currently stored in a Stack named undoAction. However, for full undo capability, it needs a detailed command structure that records what each Action does and an 'Execute' method to reverse these operations. The current structure only allows removal from the stack, but lacks reversing logic, indicating the need for a more detailed command pattern to manage undo operations effectively .