The minor problems and improvement of the program in the book "Windows Application Advanced Programming-c# programming article"

Source: Internet
Author: User
Tags bool save file
Window| Programming | program | advanced | Question Tsinghua University Press, "Windows applications Advanced Programming-c# Programming", 1th edition of January 2003, Benny Johansen & Matthew Reynolds, etc, Zhangjie translation, In the eighth chapter of the file operation of a Notepad applet, there are three small problems, of which two of the same nature
Question one: StreamReader streamreader=new StreamReader (FileStream); When you open a file that is a Chinese character, there will be garbled characters.
When the file is opened, the contents of the read file are converted to the StreamReader type, using the. NET platform is the default encoding method, which results in inconsistent encoding type of reading, and the final data is of course garbled.
Replace the original sentence with StreamReader streamreader=new streamReader (Filestream,encoding.default); We can solve the problem.
In this sentence, Encoding.default is used to denote encoding type encoding in the text file
Question two:
private void Menufilesave_click (object sender, System.EventArgs e)
{
Savefilewithname (M_filename);
}

private void Savefilewithname (string filePath)
{
if (filepath!=null)
{
SaveFile (FilePath);
M_filename=filepath;
SetModified (FALSE);
}
Else

{
Savefiledialog1.filter= "Text files (*.txt) |*.txt| All Files (*.*) |*.* ";
Savefiledialog1.filterindex=1;
Savefiledialog1.restoredirectory=true;
if (Savefiledialog1.showdialog () ==dialogresult.ok)
{
SaveFile (Savefiledialog1.filename);
M_filename=savefiledialog1.filename;
SetModified (FALSE);
}
}
}
When you run the program without opening any text files, after editing, if you choose File--> Save As, there will be a problem when you press save, prompting the empty pathname to be illegal because the Menufilesave_click event is prompted when you press save. Execution Savefilewithname (m_filename), but M_filename is empty at this time because the file has not been opened, causing an error
The solution is simple, set a variable to determine whether the file has been opened
private bool isopened;
void setopened (bool m)
{
Isopened=m;
}

BOOL Isopened ()
{
return isopened;
}
Setopened (True) when the file is opened;
When a Save file event occurs,
private void Menufilesave_click (object sender, System.EventArgs e)
{
if (isopened ())
Savefilewithname (M_filename);
Else
Savefilewithname (NULL);
}
Decide whether to open the file, if it is open, use Savefilewithname (m_filename), or, if not, savefilewithname (null), the function is equivalent to "Save as", A SaveFileDialog will appear

Question three: Case DialogResult.Yes:SaveFileWithName (M_filename);
Acanceleventargs.cancel=false;
Break
Run the program without opening the file, edit it without saving the shutdown, will appear with 2 error prompts, for the same reason,
Improvement: Case DialogResult.Yes:if (isopened ())
Savefilewithname (M_filename);
Else
Savefilewithname (NULL);
Acanceleventargs.cancel=false;
Break

Finish




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.