/
Form1.cs
69 lines (63 loc) · 2.65 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraRichEdit.Services;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.Commands;
using DevExpress.Utils;
using DevExpress.Utils.Commands;
namespace CustomCommand
{
public partial class Form1 : Form
{
public Form1() {
InitializeComponent();
MyFactoryHelper.SetMyNewCommandFactory(this.richEditControl1);
richEditControl1.CreateNewDocument();
richEditControl1.Document.AppendSingleLineText("Type at least 7 paragraphs to be able to save the document.");
}
}
#region #iricheditcommandfactoryservice
public static class MyFactoryHelper {
public static void SetMyNewCommandFactory(RichEditControl control) {
var myCommandFactory = new CustomRichEditCommandFactoryService(control, control.GetService<IRichEditCommandFactoryService>());
control.ReplaceService<IRichEditCommandFactoryService>(myCommandFactory);
}
}
public class CustomRichEditCommandFactoryService : IRichEditCommandFactoryService {
readonly IRichEditCommandFactoryService service;
readonly RichEditControl control;
public CustomRichEditCommandFactoryService(RichEditControl control,
IRichEditCommandFactoryService service) {
this.control = control;
this.service = service;
}
public RichEditCommand CreateCommand(RichEditCommandId id) {
if (id == RichEditCommandId.FileSave)
return new CustomSaveDocumentCommand(control);
return service.CreateCommand(id);
}
}
public class CustomSaveDocumentCommand : SaveDocumentCommand {
public CustomSaveDocumentCommand(IRichEditControl control)
: base(control) {
}
protected override void UpdateUIStateCore(ICommandUIState state) {
base.UpdateUIStateCore(state);
// Disable the command if the document has less than 5 paragraphs.
state.Enabled = Control.Document.Paragraphs.Count > 5;
}
protected override void ExecuteCore() {
// Cancel execution if the document contains less than 7 paragraphs.
if (Control.Document.Paragraphs.Count > 7)
base.ExecuteCore();
else MessageBox.Show(@"You should type at least 7 paragraphs to be able to save the document.",
"Please be creative", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
#endregion #iricheditcommandfactoryservice
}