Sunday, 8 May 2011

Dialog Controls in C#

Dialog Controls
                                    


                                    

Code for New Button

 private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Modified)
            {
                DialogResult result = MessageBox.Show("Do you wish to Save changes?", "Save Changes", 
MessageBoxButtons.YesNoCancel);

                if (result == DialogResult.Yes)
                    saveToolStripMenuItem_Click(sender, e);
                else if (result == DialogResult.No)
                {
                    richTextBox1.Text = "";
                }
                    if (result == DialogResult.Cancel)
                    {
                        
                    }

            } 
        }
Code For Open Button

 private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.LoadFile(openFileDialog1.FileName,
                  RichTextBoxStreamType.PlainText);
                saveFileDialog1.FileName = openFileDialog1.FileName;
            }

        }
Code for Save Button

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.FileName != "")
            {
                richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
            }
            else
            {
                saveAsToolStripMenuItem_Click(sender, e);
            }

        }
Code for Save As Button

 private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.SaveFile(saveFileDialog1.FileName,
                  RichTextBoxStreamType.PlainText);
            }

        }
Code for Exit Button

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Modified)
            {
                DialogResult result = MessageBox.Show("Do you wish to Save changes?", "Save Changes", 
MessageBoxButtons.YesNoCancel);

                if (result == DialogResult.Yes)
                    saveToolStripMenuItem_Click(sender, e);
                else
                    if (result == DialogResult.Cancel)
                    {
                    }
            }
            this.Close();

        }
Code for Form Closing

 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (richTextBox1.Modified)
            {
                DialogResult result = MessageBox.Show("Do you wish to Save changes?", "Save Changes", 
MessageBoxButtons.YesNoCancel);

                if (result == DialogResult.Yes)
                    saveToolStripMenuItem_Click(sender, e);
                else
                    if (result == DialogResult.Cancel)
                        e.Cancel = true;
            }

        }

No comments:

Post a Comment