【NET】Winform用户控件的初步封装之列表页控件

  1 public abstract partial class TListPager<TEntity, TRepository, TSqlStrConstruct> : UserControl
  2         where TEntity:Yom.Extend.Entity.EntityBase
  3         where TRepository : Yom.Extend.Repository.RepositoryBaseRepository<TEntity, TSqlStrConstruct>
  4         where TSqlStrConstruct : Huawei.Data.SqlStrConstruct
  5     {
  6         protected TRepository repository = System.Activator.CreateInstance<TRepository>();
  7         ToolTip toolTip;
  8         TSqlStrConstruct sqlStrConstruct;
  9         public TListPager()
 10         {
 11             InitializeComponent();
 12             if (this.IsContextMenu)
 13             {
 14                 this.lvList.ContextMenuStrip = this.cmsOperation;
 15             }
 16             this.wfpPager.PageSize = 20;
 17             this.wfpPager.PageChanged += new WinFormPager.PageChangeDelegate(
 18                 () =>
 19                 {
 20                     DataBind();
 21                 }
 22             );
 23             this.Dock = System.Windows.Forms.DockStyle.Fill;
 24             this.lvList.Dock = System.Windows.Forms.DockStyle.Top;
 25             this.lvList.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
 26                         | System.Windows.Forms.AnchorStyles.Left)
 27                         | System.Windows.Forms.AnchorStyles.Right)));
 28             this.lvList.ItemMouseHover += new ListViewItemMouseHoverEventHandler((object o, ListViewItemMouseHoverEventArgs ea) => {
 29                 if (!string.IsNullOrEmpty(ea.Item.ToolTipText)) {
 30                     if (this.toolTip == null) {
 31                         this.toolTip = new ToolTip();
 32                     }
 33                     this.toolTip.SetToolTip(this.lvList, ea.Item.ToolTipText);
 34                 }
 35             });
 36             this.cSearcher = this.CSearcher;
 37             if (this.cSearcher == null)
 38             {
 39                 this.pSearchContainer.Visible = false;
 40                 this.lvList.Location = new Point(0, 0);
 41                 //this.lvList.Height += this.pSearchContainer.Height;
 42             }
 43             else {
 44                 if (this.cSearcher.SearchAction == null)
 45                 {
 46                     this.cSearcher.SearchAction = (SearchArgs sa) =>
 47                     {
 48                         if (sa != null)
 49                         {
 50                             if (sa.Params != null)
 51                             {
 52                                 this.SearchParams = sa.Params.ToArray();
 53                             }
 54                             string whereStr = sa.SearchStr.ToString();
 55                             if (!string.IsNullOrEmpty(whereStr))
 56                             {
 57                                 if (string.IsNullOrEmpty(this.Where))
 58                                 {
 59                                     this.where = sa.SearchStr.ToString();
 60                                 }
 61                                 else
 62                                 {
 63                                     this.where = string.Format("{0} AND {1}", this.Where, sa.SearchStr.ToString());
 64                                 }
 65                             }
 66 
 67                         }
 68                         TListPager_Load(null, null);
 69                     };
 70                 }
 71                 this.cSearcher.Dock = DockStyle.Fill;
 72                 this.pSearchContainer.Height = this.cSearcher.Height;
 73                 this.pSearchContainer.Controls.Add(this.cSearcher);
 74                 this.lvList.Location = new Point(0, this.pSearchContainer.Height);
 75                 this.lvList.Height = this.Height - this.cSearcher.Height - this.wfpPager.Height;
 76             }
 77             editor = this.Editor;
 78             Init();
 79         }
 80         protected virtual void Init(){
 81             
 82         }
 83         protected TEditorBase<TEntity, TRepository, TSqlStrConstruct> editor;
 84         protected abstract TEditorBase<TEntity, TRepository, TSqlStrConstruct> Editor
 85         {
 86             get;
 87         }
 88          protected void TListPager_Load(object sender, EventArgs e)
 89         {
 90             this.wfpPager.CurrentPage = 1;
 91             DataBind();
 92         }
 93         protected abstract string Order
 94         {
 95             get;
 96             
 97         }
 98         protected bool IsContextMenu {
 99             get {
100                 return true;
101             }
102         }
103         protected string where;
104         protected abstract string Where
105         {
106             get;
107         }
108         protected virtual System.Data.IDataParameter[] SearchParams
109         {
110             get;
111             set;
112         }
113         protected abstract ColumnHeader[] Columns
114         {
115             get;
116         }
117         protected Type EntityType {
118             get {
119                 return typeof(TEntity);
120             }
121         }
122         protected virtual bool EnabledPage
123         {
124             get {
125                 return true;
126             }
127         }
128         public virtual void DataBind(int pageIndex) {
129             this.wfpPager.CurrentPage = pageIndex;
130             DataBind();
131         }
132         private void DataBind()
133         {
134             int count = 0;
135             TEntity[] tes;
136             if (string.IsNullOrEmpty(this.where)) {
137                 this.where = this.Where;
138             }
139             if (EnabledPage)
140             {
141                 tes = this.repository.FindAll(this.wfpPager.CurrentPage, this.wfpPager.PageSize, this.where, string.IsNullOrEmpty(this.Order)?string.Empty:this.Order, SearchParams, out count) as TEntity[];
142             }
143             else {
144                 tes = this.repository.FindAll(where, Order, this.SearchParams) as TEntity[];
145             }
146             this.lvList.Columns.Clear();
147             this.lvList.Items.Clear();
148             this.lvList.Columns.Add(new ColumnHeader(){ Text="序号",Width=40 });
149 
150             this.lvList.Columns.AddRange(this.Columns);
151             this.lvList.ShowItemToolTips = true;
152             int baseNo = this.wfpPager.PageSize * (this.wfpPager.CurrentPage - 1);
153             string[] itemArr ;
154             object propertyValue;
155             int columnIndexRecord;
156             ListViewItem toAdditem;
157             foreach (TEntity te in tes)
158             {
159                 itemArr = new string[this.Columns.Length + 1];
160                 itemArr[0] = (++baseNo).ToString();
161                 columnIndexRecord = 0;
162                 foreach (ColumnHeader ch in this.Columns) {
163                     columnIndexRecord++;
164                     if (string.IsNullOrEmpty(ch.Name)) {
165                         continue;
166                     }
167                     try
168                     {
169                         propertyValue = EntityType.GetProperty(ch.Name).GetValue(te, null);
170                     }
171                     catch {
172                         continue;
173                     }
174                     if (propertyValue is DateTime)
175                     {
176                         itemArr[columnIndexRecord] = DateTimeAction(ch.Name, (DateTime)propertyValue);
177                     }
178                     else
179                     {
180                         itemArr[columnIndexRecord] = propertyValue.ToString();
181                     }
182 
183                     
184                 }
185                 
186                 toAdditem = new ListViewItem(itemArr) { Name = EntityType.GetProperty(this.repository.GetEntityKeyName()).GetValue(te, null).ToString() };
187                 if (!string.IsNullOrEmpty(this.ItemTooltipFiledName))
188                 {
189                     toAdditem.ToolTipText = EntityType.GetProperty(this.ItemTooltipFiledName).GetValue(te, null).ToString();
190                 }
191                 toAdditem.Tag = te;
192                 ItemBeforeAdd(toAdditem);
193                 
194                 this.lvList.Items.Add(toAdditem);
195                 
196             }
197             if (EnabledPage)
198             {
199                 this.wfpPager.RecordCount = count;
200                 if (this.wfpPager.PageTotal.Equals(1) || count <= 0)
201                 {
202                     
203                     this.wfpPager.Visible = false;
204                     if (this.cSearcher == null)
205                     {
206                         this.lvList.Height = this.Height;
207                     }
208                     else
209                     {
210                         this.lvList.Height = this.Height - this.cSearcher.Height;
211                     }
212                 }
213                 else {
214                     this.wfpPager.Visible = true;
215                     if (this.cSearcher == null)
216                     {
217                         this.lvList.Height = this.Height - this.wfpPager.Height;
218                     }
219                     else
220                     {
221                         this.lvList.Height = this.Height - this.cSearcher.Height -this.wfpPager.Height;
222                     }
223                 }
224             }
225             else {
226                 this.wfpPager.Visible = false;
227                 if (this.cSearcher == null)
228                 {
229                     this.lvList.Height = this.Height;
230                 }
231                 else
232                 {
233                     this.lvList.Height = this.Height - this.cSearcher.Height;
234                 }
235 
236             }
237         }
238         protected virtual void ItemBeforeAdd(ListViewItem toAdditem)
239         { 
240             
241         }
242         protected virtual string ItemTooltipFiledName
243         {
244             get;
245             set;
246         }
247 
248         protected virtual string DateTimeAction(string propertyName,DateTime dt) {
249             try
250             {
251                 if (dt.Equals(DateTime.MinValue)) {
252                     return string.Empty;
253                 }
254                 return dt.ToString("yyyy-MM-dd HH:mm");
255             }
256             catch {
257                 try
258                 {
259                     return dt.ToString();
260                 }
261                 catch
262                 {
263                     return string.Empty;
264                 }
265                 //return string.Empty;
266             }
267         }
268 
269         protected virtual void cmsOperation_Opening(object sender, CancelEventArgs e)
270         {
271             if (this.editor == null && this.cmsOperation.Items.Count.Equals(4)) {
272                 this.cmsOperation.Items.RemoveAt(0);
273                 this.cmsOperation.Items.RemoveAt(0);
274                 this.cmsOperation.Items.RemoveAt(0);
275             }
276             if (this.cmsOperation.Items.Count.Equals(4))
277             {
278                 if (this.lvList.SelectedItems.Count <= 0)
279                 {
280                     this.cmsOperation.Items[1].Enabled = this.cmsOperation.Items[3].Enabled = false;
281                 }
282                 else
283                 {
284                     this.cmsOperation.Items[1].Enabled = this.cmsOperation.Items[3].Enabled = true;
285                 }
286             }
287             else {
288                 if (this.lvList.SelectedItems.Count <= 0)
289                 {
290                     this.cmsOperation.Items[0].Enabled = false;
291                 }
292                 else
293                 {
294                     this.cmsOperation.Items[0].Enabled = true;
295                 }
296             }
297         }
298         protected virtual bool DeleteValid(out string msg) {
299             msg = string.Empty;
300             return true;
301         }
302         protected virtual void cmsOperation_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
303         {
304             this.cmsOperation.Visible = false;
305             string key = e.ClickedItem.Tag == null ? e.ClickedItem.Text : e.ClickedItem.Tag.ToString();
306             switch (key)
307             {
308                 case "添加":
309                     //if (this.cmsOperation.Tag != null)
310                     //{
311                     //    (this.cmsOperation.Tag as Button).PerformClick();
312                     //}
313                     //else
314                     //{
315                     //    (this.Parent.Parent.Controls.Find("Edit", true)[0] as Button).PerformClick();
316                     //}
317                     editor = this.Editor;
318                     if (this.editor == null) {
319                         return;
320                     }
321                    
322                     this.Hide();
323                     this.Parent.Controls.Add(this.editor);
324                     break;
325                 case "编辑":
326                     //Button b;;
327                     //if (this.cmsOperation.Tag != null)
328                     //{
329                     //    b = this.cmsOperation.Tag as Button;
330                     //}
331                     //else
332                     //{
333                     //    b = (this.Parent.Parent.Controls.Find("Edit", true)[0] as Button);
334                     //}
335                     //b.Tag = this.lvList.FocusedItem.Name;
336                     //b.PerformClick();
337                     editor = this.Editor;
338                     if (this.Editor == null)
339                     {
340                         return;
341                     }
342                     
343                     if (this.lvList.SelectedItems.Count > 1) {
344                         MessageBox.Show("只能选择一项!");
345                         return;
346                     }
347                     if (this.lvList.SelectedItems.Count < 1)
348                     {
349                         MessageBox.Show("至少选择一项!");
350                         return;
351                     }
352                     this.Hide();
353                     this.Parent.Controls.Add(this.editor);
354                     this.editor.Key = this.lvList.SelectedItems[0].Name;
355                     break;
356                 case "删除":
357                     try
358                     {
359                         if (!Huawei.PortableComputer.Log.LogVar.UserName.Equals("admin"))
360                         {
361                             MessageBox.Show("只有超级管理员才有删除记录的权限。");
362                             return;
363                         }
364                     }
365                     catch { }
366                     string msg;
367                     if (!DeleteValid(out msg)) {
368                         if (!string.IsNullOrEmpty(msg)) {
369                             MessageBox.Show(msg);
370                             return;
371                         }
372                     }
373                     if (DialogResult.OK.Equals(MessageBox.Show("确定要删除吗?", "通知", MessageBoxButtons.OKCancel)))
374                     {
375                         TEntity entity;//= System.Activator.CreateInstance<TEntity>();
376                         if (sqlStrConstruct == null)
377                         {
378                             sqlStrConstruct = System.Activator.CreateInstance<TSqlStrConstruct>();
379                         }
380                         foreach (ListViewItem item in this.lvList.SelectedItems)
381                         {
382                             //this.EntityType.GetProperty(this.repository.GetEntityKeyName()).SetValue(entity, item.Name, null);
383                             entity = this.repository.FindBy(item.Name) as TEntity;
384                             this.repository.Delete(entity);
385                             if (sqlStrConstruct.TableName.Equals("TB_SYS_LOG"))
386                             {
387                                 continue;
388                             }
389                             Log.LogWriter.Write(string.Format("删除了信息,表为{0},主键为:{1}", sqlStrConstruct.TableName, item.Name));
390                         }
391                         TListPager_Load(sender, e);
392                     }
393                     break;
394             }
395         }
396         CSearcher cSearcher;
397         protected abstract CSearcher CSearcher
398         {
399             get;
400         }
401     }

 

你可能感兴趣的:(WinForm)