Editeur type bloc-note (simplifié)

Soyez le premier à donner votre avis sur cette source.

Vue 9 830 fois - Téléchargée 655 fois

Description

Il s'agit d'un éditeur de texte avec différentes options :
- Copier, Couper, Coller
- Annuler & reprendre
- Couleur
- Police
...

Source / Exemple :


using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;

namespace DefaultNamespace
{
	/// <summary>
	/// Description of MainForm.	
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button copierbutton;
		private System.Windows.Forms.MenuItem coller;
		private System.Windows.Forms.MenuItem browse;
		private System.Windows.Forms.MenuItem redo;
		private System.Windows.Forms.MenuItem police;
		private System.Windows.Forms.Button nouveaubutton;
		private System.Windows.Forms.MenuItem copier;
		private System.Windows.Forms.Button ouvrirbutton;
		private System.Windows.Forms.MenuItem quitter;
		private System.Windows.Forms.Button couleurbutton;
		private System.Windows.Forms.MenuItem about;
		private System.Windows.Forms.StatusBar status;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem saveas;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem couper;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.OpenFileDialog openFile;
		private System.Windows.Forms.MenuItem nouveau;
		private System.Windows.Forms.MenuItem save;
		private System.Windows.Forms.Button couperbutton;
		private System.Windows.Forms.Button collerbutton;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.RichTextBox text;
		private System.Windows.Forms.FontDialog od_police;
		private System.Windows.Forms.MenuItem couleur;
		private System.Windows.Forms.SaveFileDialog saveFile;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem undo;
		private System.Windows.Forms.Button policebutton;
		private System.Windows.Forms.ColorDialog od_color;
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		[STAThread]
		public static void Main(string[] args)
		{
			Application.Run(new MainForm());
		}
		
		#region Windows Forms Designer generated code
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent() {
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
			this.od_color = new System.Windows.Forms.ColorDialog();
			this.policebutton = new System.Windows.Forms.Button();
			this.undo = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.saveFile = new System.Windows.Forms.SaveFileDialog();
			this.couleur = new System.Windows.Forms.MenuItem();
			this.od_police = new System.Windows.Forms.FontDialog();
			this.text = new System.Windows.Forms.RichTextBox();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.collerbutton = new System.Windows.Forms.Button();
			this.couperbutton = new System.Windows.Forms.Button();
			this.save = new System.Windows.Forms.MenuItem();
			this.nouveau = new System.Windows.Forms.MenuItem();
			this.openFile = new System.Windows.Forms.OpenFileDialog();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.couper = new System.Windows.Forms.MenuItem();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.saveas = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.status = new System.Windows.Forms.StatusBar();
			this.about = new System.Windows.Forms.MenuItem();
			this.couleurbutton = new System.Windows.Forms.Button();
			this.quitter = new System.Windows.Forms.MenuItem();
			this.ouvrirbutton = new System.Windows.Forms.Button();
			this.copier = new System.Windows.Forms.MenuItem();
			this.nouveaubutton = new System.Windows.Forms.Button();
			this.police = new System.Windows.Forms.MenuItem();
			this.redo = new System.Windows.Forms.MenuItem();
			this.browse = new System.Windows.Forms.MenuItem();
			this.coller = new System.Windows.Forms.MenuItem();
			this.copierbutton = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// od_color
			// 
			this.od_color.FullOpen = true;
			// 
			// policebutton
			// 
			this.policebutton.Enabled = false;
			this.policebutton.Image = ((System.Drawing.Image)(resources.GetObject("policebutton.Image")));
			this.policebutton.Location = new System.Drawing.Point(184, 8);
			this.policebutton.Name = "policebutton";
			this.policebutton.Size = new System.Drawing.Size(24, 24);
			this.policebutton.TabIndex = 10;
			this.policebutton.Click += new System.EventHandler(this.police_Click);
			// 
			// undo
			// 
			this.undo.Index = 0;
			this.undo.Shortcut = System.Windows.Forms.Shortcut.CtrlZ;
			this.undo.Text = "Annuler";
			this.undo.Click += new System.EventHandler(this.undo_Click);
			// 
			// menuItem2
			// 
			this.menuItem2.Enabled = false;
			this.menuItem2.Index = 2;
			this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
						this.couleur,
						this.police});
			this.menuItem2.Text = "Format";
			// 
			// saveFile
			// 
			this.saveFile.Filter = "Fichier texte (*.txt)|*.txt|Fichiers JScript (*.js)|*.js|Fichiers php (*.php,*.ph" +
"p3) |*.php;*.php3|Pages Web (*.htm,*.html)|*.htm;*.html|Tous (*.*)|*.*";
			// 
			// couleur
			// 
			this.couleur.Index = 0;
			this.couleur.Text = "Couleur";
			this.couleur.Click += new System.EventHandler(this.couleur_Click);
			// 
			// text
			// 
			this.text.AcceptsTab = true;
			this.text.DetectUrls = false;
			this.text.Enabled = false;
			this.text.Font = new System.Drawing.Font("Lucida Console", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.text.HideSelection = false;
			this.text.Location = new System.Drawing.Point(8, 40);
			this.text.Name = "text";
			this.text.Size = new System.Drawing.Size(824, 536);
			this.text.TabIndex = 0;
			this.text.Text = "";
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
						this.menuItem1,
						this.menuItem3,
						this.menuItem2,
						this.menuItem6});
			// 
			// collerbutton
			// 
			this.collerbutton.Enabled = false;
			this.collerbutton.Image = ((System.Drawing.Image)(resources.GetObject("collerbutton.Image")));
			this.collerbutton.Location = new System.Drawing.Point(136, 8);
			this.collerbutton.Name = "collerbutton";
			this.collerbutton.Size = new System.Drawing.Size(24, 24);
			this.collerbutton.TabIndex = 6;
			this.collerbutton.Click += new System.EventHandler(this.coller_Click);
			// 
			// couperbutton
			// 
			this.couperbutton.Enabled = false;
			this.couperbutton.Image = ((System.Drawing.Image)(resources.GetObject("couperbutton.Image")));
			this.couperbutton.Location = new System.Drawing.Point(112, 8);
			this.couperbutton.Name = "couperbutton";
			this.couperbutton.Size = new System.Drawing.Size(24, 24);
			this.couperbutton.TabIndex = 5;
			this.couperbutton.Click += new System.EventHandler(this.couper_Click);
			// 
			// save
			// 
			this.save.Enabled = false;
			this.save.Index = 2;
			this.save.Shortcut = System.Windows.Forms.Shortcut.CtrlS;
			this.save.Text = "Enregistrer";
			this.save.Click += new System.EventHandler(this.save_Click);
			// 
			// nouveau
			// 
			this.nouveau.Index = 0;
			this.nouveau.Shortcut = System.Windows.Forms.Shortcut.CtrlN;
			this.nouveau.Text = "Nouveau";
			this.nouveau.Click += new System.EventHandler(this.nouveau_Click);
			// 
			// openFile
			// 
			this.openFile.Filter = "Fichier texte (*.txt)|*.txt|Fichiers JScript (*.js)|*.js|Fichiers php (*.php,*.ph" +
"p3) |*.php;*.php3|Pages Web (*.htm,*.html)|*.htm;*.html|Tous (*.*)|*.*";
			this.openFile.Title = "Ouvrir un fichier";
			// 
			// menuItem4
			// 
			this.menuItem4.Index = -1;
			this.menuItem4.Text = "";
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 3;
			this.menuItem6.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
						this.about});
			this.menuItem6.Text = "?";
			// 
			// couper
			// 
			this.couper.Index = 2;
			this.couper.Shortcut = System.Windows.Forms.Shortcut.CtrlX;
			this.couper.Text = "Couper";
			this.couper.Click += new System.EventHandler(this.couper_Click);
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
						this.nouveau,
						this.browse,
						this.save,
						this.saveas,
						this.quitter});
			this.menuItem1.Text = "Fichier";
			// 
			// saveas
			// 
			this.saveas.Enabled = false;
			this.saveas.Index = 3;
			this.saveas.Text = "Enregistrer-sous";
			this.saveas.Click += new System.EventHandler(this.saveas_Click);
			// 
			// menuItem3
			// 
			this.menuItem3.Enabled = false;
			this.menuItem3.Index = 1;
			this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
						this.undo,
						this.redo,
						this.couper,
						this.copier,
						this.coller});
			this.menuItem3.Text = "Edition";
			// 
			// status
			// 
			this.status.Location = new System.Drawing.Point(0, 584);
			this.status.Name = "status";
			this.status.Size = new System.Drawing.Size(840, 22);
			this.status.TabIndex = 3;
			// 
			// about
			// 
			this.about.Index = 0;
			this.about.Shortcut = System.Windows.Forms.Shortcut.F1;
			this.about.Text = "A propos";
			this.about.Click += new System.EventHandler(this.about_Click);
			// 
			// couleurbutton
			// 
			this.couleurbutton.Enabled = false;
			this.couleurbutton.Image = ((System.Drawing.Image)(resources.GetObject("couleurbutton.Image")));
			this.couleurbutton.Location = new System.Drawing.Point(208, 8);
			this.couleurbutton.Name = "couleurbutton";
			this.couleurbutton.Size = new System.Drawing.Size(24, 24);
			this.couleurbutton.TabIndex = 9;
			this.couleurbutton.Click += new System.EventHandler(this.couleur_Click);
			// 
			// quitter
			// 
			this.quitter.Index = 4;
			this.quitter.Shortcut = System.Windows.Forms.Shortcut.AltF4;
			this.quitter.Text = "Quitter";
			this.quitter.Click += new System.EventHandler(this.quitter_Click);
			// 
			// ouvrirbutton
			// 
			this.ouvrirbutton.Image = ((System.Drawing.Image)(resources.GetObject("ouvrirbutton.Image")));
			this.ouvrirbutton.Location = new System.Drawing.Point(32, 8);
			this.ouvrirbutton.Name = "ouvrirbutton";
			this.ouvrirbutton.Size = new System.Drawing.Size(24, 24);
			this.ouvrirbutton.TabIndex = 8;
			this.ouvrirbutton.Click += new System.EventHandler(this.browse_Click);
			// 
			// copier
			// 
			this.copier.Index = 3;
			this.copier.Shortcut = System.Windows.Forms.Shortcut.CtrlC;
			this.copier.Text = "Copier";
			this.copier.Click += new System.EventHandler(this.copier_Click);
			// 
			// nouveaubutton
			// 
			this.nouveaubutton.Image = ((System.Drawing.Image)(resources.GetObject("nouveaubutton.Image")));
			this.nouveaubutton.Location = new System.Drawing.Point(8, 8);
			this.nouveaubutton.Name = "nouveaubutton";
			this.nouveaubutton.Size = new System.Drawing.Size(24, 24);
			this.nouveaubutton.TabIndex = 11;
			this.nouveaubutton.Click += new System.EventHandler(this.nouveau_Click);
			// 
			// police
			// 
			this.police.Index = 1;
			this.police.Text = "Police";
			this.police.Click += new System.EventHandler(this.police_Click);
			// 
			// redo
			// 
			this.redo.Index = 1;
			this.redo.Shortcut = System.Windows.Forms.Shortcut.CtrlY;
			this.redo.Text = "Refaire";
			this.redo.Click += new System.EventHandler(this.redo_Click);
			// 
			// browse
			// 
			this.browse.Index = 1;
			this.browse.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
			this.browse.Text = "Ouvrir";
			this.browse.Click += new System.EventHandler(this.browse_Click);
			// 
			// coller
			// 
			this.coller.Index = 4;
			this.coller.Shortcut = System.Windows.Forms.Shortcut.CtrlV;
			this.coller.Text = "Coller";
			this.coller.Click += new System.EventHandler(this.coller_Click);
			// 
			// copierbutton
			// 
			this.copierbutton.Enabled = false;
			this.copierbutton.Image = ((System.Drawing.Image)(resources.GetObject("copierbutton.Image")));
			this.copierbutton.Location = new System.Drawing.Point(88, 8);
			this.copierbutton.Name = "copierbutton";
			this.copierbutton.Size = new System.Drawing.Size(24, 24);
			this.copierbutton.TabIndex = 4;
			this.copierbutton.Click += new System.EventHandler(this.copier_Click);
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
			this.ClientSize = new System.Drawing.Size(840, 606);
			this.Controls.Add(this.nouveaubutton);
			this.Controls.Add(this.policebutton);
			this.Controls.Add(this.couleurbutton);
			this.Controls.Add(this.ouvrirbutton);
			this.Controls.Add(this.collerbutton);
			this.Controls.Add(this.couperbutton);
			this.Controls.Add(this.copierbutton);
			this.Controls.Add(this.text);
			this.Controls.Add(this.status);
			this.Location = new System.Drawing.Point(40, 40);
			this.Menu = this.mainMenu1;
			this.Name = "MainForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
			this.Text = "Editeur v.1.0";
			this.Click += new System.EventHandler(this.browse_Click);
			this.ResumeLayout(false);
		}
		#endregion
			
		private void browse_Click(object sender, System.EventArgs e)
		{
			if (openFile.ShowDialog()==DialogResult.OK)
			{
				status.Text=openFile.FileName;
				TextReader txt= new StreamReader(status.Text);
				string getText=txt.ReadToEnd();
				text.Text=getText;
			
			save.Enabled=true;
			saveas.Enabled=true;
			copierbutton.Enabled=true;
			couperbutton.Enabled=true;
			collerbutton.Enabled=true;
			policebutton.Enabled=true;
			couleurbutton.Enabled=true;
			menuItem2.Enabled=true;
			menuItem3.Enabled=true;
			text.Enabled=true;	
			}
			else
			{
			
			}
		}
	
		private void about_Click(object sender, System.EventArgs e)
		{
			MessageBox.Show("             © Tahiti_bob\n    http://cginvent.virtuelnet.net    ", "A propos", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
		}
		
		private void quitter_Click(object sender, System.EventArgs e)
		{
		      DialogResult dr =  MessageBox.Show("Voulez vous réellement quitter cette application ?", "Quitter l'application", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if(dr == DialogResult.Yes)
            { 
                Application.Exit(); 
            }
		}
	
		private void couleur_Click(object sender, System.EventArgs e)
		{
            if(od_color.ShowDialog()==DialogResult.OK)
            {
                text.SelectionColor = od_color.Color;
            }
		}
		
		private void police_Click(object sender, System.EventArgs e)
		{
            if(od_police.ShowDialog()==DialogResult.OK)
            {
                text.SelectionFont = od_police.Font;
            }
		}
		
		private void undo_Click(object sender, System.EventArgs e)
		{
            	text.Undo();
		}
		
		private void redo_Click(object sender, System.EventArgs e)
		{
                text.Redo(); 
		}
		
		private void copier_Click(object sender, System.EventArgs e)
		{
              	text.Copy();
		}
		
		private void couper_Click(object sender, System.EventArgs e)
		{
               	text.Cut();
		}
		
		private void coller_Click(object sender, System.EventArgs e)
		{
               	text.Paste(); 
		}
	
	
		private void save_Click(object sender, System.EventArgs e)
	 	{
           TextReader txt= new StreamReader(status.Text);
			string getText2=txt.ReadToEnd();
	 		
			if (text.Text!=getText2)
           	{
           	text.SaveFile(status.Text,RichTextBoxStreamType.PlainText);
           	}
           	else
           	{
           		if (saveFile.ShowDialog()==DialogResult.OK)
           		{
           		text.SaveFile(saveFile.FileName,RichTextBoxStreamType.PlainText);
           		}
          	 }
        }
        
        private void saveas_Click(object sender, System.EventArgs e)
		{
			if (saveFile.ShowDialog()==DialogResult.OK)
           	{
           	text.SaveFile(saveFile.FileName,RichTextBoxStreamType.PlainText);
           	status.Text=saveFile.FileName;
           	save.Enabled=true;
           	}
            else
            {
            
            }
           	
 		}  
 		
 		private void nouveau_Click(object sender, System.EventArgs e)
		{
			saveas.Enabled=true;
			copierbutton.Enabled=true;
			couperbutton.Enabled=true;
			collerbutton.Enabled=true;
			policebutton.Enabled=true;
			couleurbutton.Enabled=true;
			menuItem2.Enabled=true;
			menuItem3.Enabled=true;
			text.Enabled=true;	
		

	
		}

	}
}

Conclusion :


Bugs :

Lorsque l'on fait enregistrer, on a :
"An unhandled exception...The process can't access the file..... because it's being used by another program"

Si quelqu'un a la solution a ce pb...
Merci.

Codes Sources

A voir également

Ajouter un commentaire

Commentaires

cs_tahiti_bob
Messages postés
86
Date d'inscription
jeudi 8 juillet 2004
Statut
Membre
Dernière intervention
27 juin 2007
1
Merci ousta, j'ai résolu le problème de processus.

D'ici peu, je vais poster la v.2.0 de mon éditeur
(moins buggée et + de fonctinnalités).

@++
lumesh
Messages postés
564
Date d'inscription
jeudi 21 février 2002
Statut
Membre
Dernière intervention
7 novembre 2008

plus simple que notepad c'est un papier et un crayon !
cs_ousta
Messages postés
95
Date d'inscription
mardi 15 juin 2004
Statut
Membre
Dernière intervention
2 juin 2007

ca pourrait le faire ^^

noublie pas de rajouter txt.Close(); a la fin de ton private void browse_Click(object sender, System.EventArgs e) car comme tu dois surement le savoir ton fichier est ouvert en lecture dans le bloc note et donc tu peu pas lenregistrer vu que le processus est encore actif :)
pour ton save fais plutot comme ca
private void save_Click(object sender, System.EventArgs e)
{
saveFile.FileName = status.Text;
text.SaveFile(saveFile.FileName,RichTextBoxStreamType.PlainText);
}

voila bonne chance et noublie pas de fermer tes fd :)
cs_ousta
Messages postés
95
Date d'inscription
mardi 15 juin 2004
Statut
Membre
Dernière intervention
2 juin 2007

c'est simple, tu as deux processus, celui qui ouvre ton fichier (que tu oublie de fermer) et celui pour enregistrer ( qui ne peut enregistrer car tu na pas fermé le fichier)

Tu as juste a ouvrir ton fichier et fermer le "file descriptor" et apres ton enregistrement ne posera aucun probleme :)

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.