啊鑫
2024-07-11 afbf8700d137710713db61955879d0f5acb73738
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#region
 
using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
using CSFrameworkV5.Interfaces;
 
///*************************************************************************/
///*
///* 文件名    :ucAttachment.cs                              
///* 程序说明  : 附件管理自定义控件
///*               这个控件只是个载体, 具体业务由实现IAttachmentStorage接口的类完成. 
///* 原创作者  :www.csframework.com 
///* 
///* Copyright 2006-2021 C/S框架网 www.csframework.com
///*
///**************************************************************************/
 
#endregion
 
namespace CSFrameworkV5.Library.UserControls
{
    /// <summary>
    ///     提交附件资料的事件
    /// </summary>
    /// <param name="data">附件资料</param>
    public delegate void PostAttachmentData(object data);
 
    /// <summary>
    ///     附件管理自定义控件.
    /// </summary>
    public partial class ucAttachment : UserControl
    {
        //是否立即提交数据
        private bool _ImmediatelyPost;
 
        private PostAttachmentData _OnPostAttachmentData;
 
        //附件管理存储策略
        private IAttachmentStorage _Storage;
 
        public ucAttachment()
        {
            InitializeComponent();
        }
 
        /// <summary>
        ///     即时提交数据
        /// </summary>
        [DefaultValue(false)]
        [Description("即时提交数据")]
        public bool ImmediatelyPost
        {
            get => _ImmediatelyPost;
            set => _ImmediatelyPost = value;
        }
 
        /// <summary>
        ///     附件管理策略
        /// </summary>
        public IAttachmentStorage StorateStrategy
        {
            get => _Storage;
            set => _Storage = value;
        }
 
        private void btnAdd_Click(object sender, EventArgs e)
        {
            var dlg = new OpenFileDialog();
            if (DialogResult.OK == dlg.ShowDialog())
            {
                //检查文件名是否存在
                if (_Storage.Exists(Path.GetFileName(dlg.FileName)))
                {
                    Msg.Warning("附件已经存在!");
                }
                else
                {
                    _Storage.AddFile(dlg.FileName);
                    if (_ImmediatelyPost && _OnPostAttachmentData != null)
                        _OnPostAttachmentData(_Storage.AttachmentStorage);
                }
            }
        }
 
        private void btnDel_Click(object sender, EventArgs e)
        {
            if (gvSummary.IsValidRowHandle(gvSummary.FocusedRowHandle))
            {
                if (Msg.AskQuestion("确定要删除此附件吗?"))
                {
                    var fileName =
                        gvSummary.GetDataRow(gvSummary.FocusedRowHandle)[
                            "FileName"].ToStringEx();
                    _Storage.DeleteFile(fileName);
                    if (_ImmediatelyPost && _OnPostAttachmentData != null)
                        _OnPostAttachmentData(_Storage.AttachmentStorage);
                }
            }
            else
            {
                Msg.Warning("请选择一个附件!");
            }
        }
 
        private void btnSaveAs_Click(object sender, EventArgs e)
        {
            if (gvSummary.IsValidRowHandle(gvSummary.FocusedRowHandle))
            {
                var fileName =
                    gvSummary.GetDataRow(gvSummary.FocusedRowHandle)["FileName"]
                        .ToStringEx();
                _Storage.SaveAs(fileName);
            }
            else
            {
                Msg.Warning("请选择一个附件!");
            }
        }
 
        /// <summary>
        ///     绑定表格控件的数据源
        /// </summary>
        public void DoBindDataSource()
        {
            if (_Storage != null)
            {
                gcSummary.DataSource = null;
                gcSummary.DataSource = _Storage.AttachmentStorage;
            }
        }
 
        /// <summary>
        ///     取消修改
        /// </summary>
        public void DoRejectChanges()
        {
            _Storage.AttachmentStorage.RejectChanges();
            DoBindDataSource();
        }
 
        /// <summary>
        ///     设置按钮状态
        /// </summary>
        /// <param name="currentState">按钮状态</param>
        public void DoSetButtonState(UpdateType currentState)
        {
            var onEditMode = currentState == UpdateType.Modify ||
                             currentState == UpdateType.Add;
            btnAdd.Enabled = onEditMode;
            btnDel.Enabled = onEditMode;
            gvSummary.OptionsBehavior.Editable = onEditMode;
            menuDrop.Enabled = onEditMode; //删除
 
            //不受状态控制
            btnSaveAs.Enabled = true; //另存为
            menuOpen.Enabled = true;
            menuSaveAs.Enabled = true;
        }
 
        private void menuOpen_Click(object sender, EventArgs e)
        {
            if (gvSummary.IsValidRowHandle(gvSummary.FocusedRowHandle))
            {
                var fileName =
                    gvSummary.GetDataRow(gvSummary.FocusedRowHandle)["FileName"]
                        .ToStringEx();
                _Storage.OpenFile(fileName);
            }
            else
            {
                Msg.Warning("请选择一个附件!");
            }
        }
 
        /// <summary>
        ///     保存附件数据
        /// </summary>
        [Description("保存附件数据")]
        public event PostAttachmentData OnPostAttachmentData
        {
            add => _OnPostAttachmentData += value;
            remove => _OnPostAttachmentData -= value;
        }
 
        private void ucAttachment_Load(object sender, EventArgs e)
        {
            //
        }
 
        private void ucAttachment_SizeChanged(object sender, EventArgs e)
        {
            colFileTitle.Width = Width - colIconSmall.Width - 15;
        }
    }
}