Hello there folks,

I'm trying to make myself more C# literate. I've decided to branch myself towards the GUI subject for the moment and get myself more familiar with dialog boxes and what not.

So for the moment, I've created a form which has a RichTextBox. And I've added a Close button which if the RichTextBox is empty, then the form will simply close. If the RichTextBox has any form of text or whatever inside it, then a dialog box will appear asking the user whether or not they want to save changes.

So far, this is my code:

private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox.Text))
            {
                Close();
            }
            else
            {
                MessageBox.Show("Do you want to save changes?");
            }
        }

Now what my question to all of you generous people out there is, basically, How do I make the dialog box more dynamic by adding in a Cancel button and a Save button. Right now, the box does appear just fine, but I don't, of course, have any code which acknowledges the decision of the user. On Microsoft word or notepad, if someone types something in and then decides to exit, then a dialog box comes up asking whether or not they want to save changes, and if they do want to, then another dialog shows up which is a sort-of Windows Explorer and lets you choose a destination. But for now, I simply want to add a save, don't save and a cancel button on to the form.

How would I go about doing that?

Many thanks,

Usmaan

You can use "MessageBoxButtons." to include OK and CANCEL, so OK would confirm (through your text) that you want to save and Cancel will indicate a want to continue working.

MessageBox.Show("Please confirm the statement is accurate and sign", "Signature Problem", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

Your first dialog is close but should make use of the overriden method for MessageBox.Show like so:

DialogResult r = MessageBox.Show("Do you want to save changes?", "Save Changes?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

Then you can check the variable r to see which button the user pressed and take the appropriate action:

if (r == DialogResult.Yes)
  SaveChanges();
if (r != DialogResult.Cancel)
  Close();

Then in your SaveChanges() function, you can use a SaveFileDialog to prompt the user for where to save the file. Have a go at this before I just blurt out the answer...

Wow, that's amazing. Thank you so much hearthand and Darkagn.

Really appreciate it.

I'm going to eat with my family now and get back to this as soon as possible.

Thank you once again,

Usmaan

Okay i'll have a go at the openfiledialog, I think I can do that :P

Here is the code. Take a closer look, and please use a break point to go through all the code - and then let me know if there is anything to add.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Dec2Exercise
{
    public partial class Form1 : Form
    {
        int textLenght;

        public Form1()
        {
            InitializeComponent();
            //because the code has no "Open File" option, will call OpenNewFile method here!
            //Otherwise call it just after richTextBox is filled up with text:
            OpeningNewFile();
        }

        private void OpeningNewFile()
        {
            textLenght = this.richTextBox1.Text.Length;
        }

        private string MessageWhileClosing()
        {
            string text = null;
            int textLenght_New = this.richTextBox1.Text.Length;
            if (textLenght < textLenght_New)
            {             
                DialogResult dialog = MessageBox.Show("Do you want to save changes?", "Text Changed", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (dialog == DialogResult.Yes)
                    text = "saveYes";
                else if (dialog == DialogResult.No)
                    text = "saveNo";
                else if (dialog == DialogResult.Cancel)
                    text = "saveCancel";
            }
            return text;
        }

        private void ShowSaveFileDialog()
        {
            SaveFileDialog save = new SaveFileDialog();
            save.RestoreDirectory = true;
            save.Filter = "Word files (*.txt)|*.txt|All Files (*.*)|*.*";
            save.FilterIndex = 1;
            if (save.ShowDialog() == DialogResult.OK)
                MessageBox.Show("File on the path:\n" + save.FileName + " has been saved successfully.");
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            string strClosing = MessageWhileClosing();
            if (strClosing == "saveYes")
            {
                e.Cancel = true;
                ShowSaveFileDialog();
            }
            else if (strClosing == "saveCancel")
                e.Cancel = true;
            //else if (strClosing == "saveNo") NO NEED TO USE THIS - WILL CLOSE AUTOMATICALY!
            //    e.Cancel = false;
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}


bye,
Mitja
This article has been dead for over six months. Start a new discussion instead.