菜单和文件对话框 [英] menus and file dialog

查看:72
本文介绍了菜单和文件对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

菜单项:新建",打开",保存",另存为"和退出"
我需要做的事情:
-文本框将接受Enter键和Tab键.

-新增:如果文本框的内容未更改,请立即清除内容,并将文件名设置为默认的".更新表单标题.如果文本框的内容已更改,请使用MessageBox询问用户是否希望放弃所做的更改.如果用户同意,请清除文本框的内容,否则返回.

-打开:检查以确定文本框的内容是否已更改.如果有,请使用MessageBox询问用户是否要放弃所做的更改.如果用户不同意,请返回.如果用户同意,请使用OpenFileDialog选择文件.如果用户取消或关闭OpenFileDialog,则返回而不读取文件.否则,将文本文件读入文本框,通过显示MessageBox来处理所有异常.更新表单标题以包括新文件的文件名,不包含路径.

-保存:使用当前文件名保存文本框的内容.显示使用MessageBox生成的所有异常.此菜单项最初将被禁用,并且仅当存在有效的文件名并且自上次保存或加载以来要保存的文本已更改时才启用.

-另存为:使用默认文件名"* .txt"打开SaveAsDialog.如果SaveAsDialog已关闭或选择了取消,则什么也不做.否则,使用选定的文件名保存文本框的内容.显示使用MessageBox发生的任何异常.用新文件名更新表单的标题.此菜单项最初将被禁用,并且仅在自上次保存或加载以来要保存的文本发生更改时才启用.

到目前为止,我有这个,其余的我都不知道该怎么办.
对此我感到抱歉.我知道有很多要问的

谢谢您的帮助...

menu items: New, Open, Save, Save As and Exit
Things i need to do:
-The textbox will accept the Enter key and Tab key.

-New: If the contents of the textbox have not been changed, immediately clear the contents, and set the file name to the default "". Update the form caption. If the contents of the textbox have been changed, use a MessageBox to ask the user if they wish to discard the changes. If the user agrees, clear the contents of the textbox, otherwise, return.

-Open: Check to determine if the contents of the textbox have been changed. If they have, use a MessageBox to ask the user if they wish to discard the changes. If the user disagrees, return. If the user agrees, use the OpenFileDialog to select a file. If the user cancels or closes the OpenFileDialog, return without reading in a file. Otherwise, read in the text file into the textbox, handling any exceptions by displaying a MessageBox. Update the form caption to include the file name of the new file, without the path.

-Save: Save the contents of the textbox using the current file name. Display any exceptions generated using a MessageBox. This menuitem will be initially disabled, and will only be enabled when there is a valid file name and the text to be saved has changed since the last save or load.

-Save As: Open the SaveAsDialog with a default file name of "*.txt". If the SaveAsDialog is closed or cancel is selected, do nothing. Otherwise, save the contents of the textbox using the selected file name. Display any exceptions that occur using a MessageBox. Update the caption of the form with the new file name. This menuitem will be initially disabled, and will only be enabled when the text to be saved has changed since the last save or load.

so far i have this, the rest i don''t know what to do.
I''m sorry for this. i know is a lot to ask

thank you for the help...

private void newToolStripMenuItem_Click(object sender, EventArgs e)
      {
          txtMain.Clear();
      }

      private void openToolStripMenuItem_Click(object sender, EventArgs e)
      {
          //Shows the openFileDialog
          openFileDialog1.ShowDialog();
          //Reads the text file
          System.IO.StreamReader OpenFile = new System.IO.StreamReader(openFileDialog1.FileName);
          //Displays the text file in the textBox
          txtMain.Text = OpenFile.ReadToEnd();
          //Closes the proccess
          OpenFile.Close();
      }

      private void saveToolStripMenuItem_Click(object sender, EventArgs e)
      {
          //Shows the openFileDialog
          openFileDialog1.ShowDialog();
          //Reads the text file
          System.IO.StreamReader OpenFile = new System.IO.StreamReader(openFileDialog1.FileName);
          //Displays the text file in the textBox
          txtMain.Text = OpenFile.ReadToEnd();
          //Closes the proccess
          OpenFile.Close();
      }

      private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
      {
          //Open the saveFileDialog
          saveFileDialog1.ShowDialog();
          //Determines the text file to save to
          System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(saveFileDialog1.FileName);
          //Writes the text to the file
          SaveFile.WriteLine(txtMain.Text);
          //Closes the proccess
          SaveFile.Close();
      }

      private void exitToolStripMenuItem_Click(object sender, EventArgs e)
      {
          Application.Exit();

推荐答案

逻辑上:

  1. 向您的Form或Document类添加一个布尔变量.
  2. 打开Document时,将布尔变量设置为False.
  3. 当您对Document集进行任何更改时,
  4. 将变量保存为True.
  5. 当您保存文档(保存"或另存为")时,将变量设置为False.
  6. 当用户关闭表单时,请检查该变量是否为true.它表示文档很脏(即需要保存),因此您显示消息框并采取相应的措施.

  1. Add a boolean variable to your Form or Document class.
  2. When you open a Document set the boolean variable to False.
  3. When you make any changes to the Document set the variable to True.
  4. When you save the document (Save or Save As) set the variable to False.
  5. When the user closes your form, check the variable and if it is true it indicates that the document is dirty (i.e. needs saving) so you show the Message box and act accordingly.


这篇关于菜单和文件对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆