Práctica 12. Bloc de Notas
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 Práctica_12
{
public partial class Form1 : Form
{
private string
m_sfileName="";
public Form1()
{
InitializeComponent();
clearFormText();
rtfMain.WordWrap=false;
mnuEdicionAjusteDeLinea.Checked = false;
}
private void
clearFormText()
{
this.Text="Bloc de notas -[sin-nombre]";
}
private
void mnuAyudaAcercaDe_Click(object sender, EventArgs
e)
{
//
Muestra el formulario frmAcercade
frmAcercade
Sam = new frmAcercade();
Sam.ShowDialog();
}
private void
mnuArchivoSalir_Click(object sender, EventArgs e)
{
if(rtfMain.Modified==true)
{
if(MessageBox.Show("El texto ha cambiado - realmente desea salir",
"Confirmar salida", MessageBoxButtons.YesNo, MessageBoxIcon.Question)==DialogResult.Yes)
{
Application.Exit();
}
}
else
{
Application.Exit();
}
}
private void
mnuArchivoNuevo_Click(object sender, EventArgs e)
{
if
(rtfMain.Modified == true)
{
if
(MessageBox.Show("El
texto ha cambiado - realmente desea crear un nuevo documento",
"Crear
documento", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
rtfMain.Clear();
m_sfileName = ""; // limpia
el nombre del archivo
clearFormText(); // y resetea el caption del formulario
}
}
else
{
rtfMain.Clear();
m_sfileName = "";
// limpia el nombre del archivo
clearFormText(); // y resetea el caption del formulario
}
}
private
void mnuArchivoAbrir_Click(object sender, EventArgs
e)
{
bool
bDoOpen = true;
//
muestra una caja de diálogo y abre el archivo
// pero
checa que la caja de texto no haya cambiado
if
(rtfMain.Modified == true)
{
if
(MessageBox.Show("El
texto ha cambiado - desea abrir otro documento",
"Confirmar
abrir documento", MessageBoxButtons.YesNo,
MessageBoxIcon.Question)
== DialogResult.No)
bDoOpen = false;
}
if
(bDoOpen == true)
{
dlgOpen.Filter= "Archivo de texto (*.txt)|*.txt|Todos los archivos
(*.*)|*.*";
dlgOpen.FilterIndex =1;
if
(dlgOpen.ShowDialog() == DialogResult.OK
&& dlgOpen.FileName.Length>0)
{
rtfMain.LoadFile(dlgOpen.FileName,RichTextBoxStreamType.PlainText);
rtfMain.Modified=false;
m_sfileName= dlgOpen.FileName;
setFormText(m_sfileName);
}
}
}
private
void setFormText(string
sFile)
{
//Crea
una instancia de una clase FileInfo
//y la
usa para no poner el path
System.IO.FileInfo
fInfo;
fInfo= new
System.IO.FileInfo(sFile);
this.Text
= "Bloc de notas - [" + fInfo.Name
+ "]";
}
private
void mnuArchivoGuardar_Click(object sender, EventArgs
e)
{
//Guarda
el archivo si tenemos un nombre de archivo
if
(m_sfileName != "")
rtfMain.SaveFile(m_sfileName, RichTextBoxStreamType.PlainText);
else
mnuArchivoGuardarComo_Click(sender, e);
}
private
void mnuArchivoGuardarComo_Click(object sender, EventArgs
e)
{
//Si
tenemos un nombre de archivo, se usa en el
//cuadro
de diálogo, si no se pone un nombre
//genérico
if
(m_sfileName != "")
dlgSave.FileName = m_sfileName;
else
dlgSave.FileName = "Documento1";
dlgSave.Filter="Archivos de texto (*.txt)|*.txt|Todos los archivos
(*.*)|*.*";
dlgSave.FilterIndex=1;
dlgSave.DefaultExt = "txt";
if
(dlgSave.ShowDialog() == DialogResult.OK
&&
dlgSave.FileName.Length > 0)
{
rtfMain.SaveFile(dlgSave.FileName,
RichTextBoxStreamType.PlainText);
rtfMain.Modified = false;
m_sfileName = dlgSave.FileName;
setFormText(m_sfileName);
}
}
private
void mnuEdicionDeshacer_Click(object sender, EventArgs
e)
{
rtfMain.Undo();
}
private
void mneEdicionRehacer_Click(object sender, EventArgs
e)
{
rtfMain.Redo();
}
private
void mnuEdicionSeleccionarTodo_Click(object sender, EventArgs
e)
{
rtfMain.SelectAll();
}
private
void Form1_Load(object
sender, EventArgs e)
{
}
private
void mnuEdicionCortar_Click(object sender, EventArgs
e)
{
rtfMain.Cut();
}
private
void mnuEdicionCopiar_Click(object sender, EventArgs
e)
{
rtfMain.Copy();
}
private
void mnuEdicionPegar_Click(object sender, EventArgs
e)
{
rtfMain.Paste();
}
private
void mnuEdicionLimpiar_Click(object sender, EventArgs
e)
{
if
(MessageBox.Show("Desea
eliminar todo el texto?",
"Confirmar
eliminar texto", MessageBoxButtons.OKCancel,
MessageBoxIcon.Question)
== DialogResult.OK)
rtfMain.Clear();
}
private void
mnuEdicionAjusteDeLinea_Click(object sender, EventArgs e)
{
if (rtfMain.WordWrap == true)
{
rtfMain.WordWrap = false;
mnuEdicionAjusteDeLinea.Checked = false;
}
else
{
rtfMain.WordWrap = true;
mnuEdicionAjusteDeLinea.Checked = true;
}
}
private void
mnuEdicionFuente_Click(object sender, EventArgs e)
{
dlgFont.Font = rtfMain.Font;
if (dlgFont.ShowDialog() == DialogResult.OK)
rtfMain.Font = dlgFont.Font;
}
private void
Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (rtfMain.Modified == true)
if(MessageBox.Show("El texto ha cambiado- desea salir?",
"Confirmar salida", MessageBoxButtons.YesNo,
MessageBoxIcon.Question)== DialogResult.No)
e.Cancel =true;
}
}
}
Clase para el formulario Acerca de...
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 Práctica_12
{
public partial class frmAcercade
: Form
{
public frmAcercade()
{
InitializeComponent();
}
private void
pictureBox1_Click(object sender, EventArgs e)
{
}
private void
btnOK_Click(object sender, EventArgs e)
{
Close();
}
}
}
No hay comentarios:
Publicar un comentario