一個6年的菜鳥,在4年以前作的一些功能(二)

前戲:針對上一篇列出來的功能點,今天和你們分享下這個自定義的公式是怎麼設計的,因爲個人第一篇博客在首頁被管理員移走了,你們能夠點擊這裏來跳轉,看下第一篇的目錄結構。本人做爲老菜鳥,和你們分享的也是一些老菜鳥的想法,大神千萬別噴我.html

 

設計背景:當初爲何要設計這個自定義的計算公式呢,緣由就是,這個價格是不肯定的,計算方式也是不肯定的,那若是在代碼裏面寫死,那未來修改起來會比較麻煩,做爲老菜鳥,我想偷懶了,也是爲了省事。ui

 

如何設計:這對目前項目的狀況,計算的過程應該是按照線性的方式來計算的,那麼問題就簡單了。this

首先咱們先要定義一個範圍控件,這個控件的目的就是,在某個區間以內,設置固定的金額或者單價,spa

 

 

 

一下我列出了部分代碼,供你們提供思路設計

    

  1 public partial class RolesUserControl :  UserControl
  2 
  3     {
  4 
  5         public delegate void DelLast(object o, DeleteEventArgs e);
  6 
  7  
  8 
  9         public event DelLast onLeftTextBox;
 10 
 11  
 12 
 13         public delegate void DeleteControl(object o, DeleteEventArgs e);
 14 
 15         public event DeleteControl onDelete;
 16 
 17  
 18 
 19         /// <summary>
 20 
 21         /// 控件索引
 22 
 23         /// </summary>
 24 
 25         public int Index { get; set; }
 26 
 27  
 28 
 29         /// <summary>
 30 
 31         /// 是否驗證成功
 32 
 33         /// </summary>
 34 
 35         public bool isRegistOK { get; set; }
 36 
 37  
 38 
 39         /// <summary>
 40 
 41         /// 是否啓用關閉
 42 
 43         /// </summary>
 44 
 45         public bool ShowClose
 46 
 47         {
 48 
 49             set
 50 
 51             {
 52 
 53                 this.pictureBox1.Enabled = value;
 54 
 55             }
 56 
 57         }
 58 
 59  
 60 
 61         /// <summary>
 62 
 63         /// 是否顯示關閉
 64 
 65         /// </summary>
 66 
 67         public bool ShowCloseVisible
 68 
 69         {
 70 
 71             set
 72 
 73             {
 74 
 75                 this.pictureBox1.Visible = value;
 76 
 77             }
 78 
 79         }
 80 
 81  
 82 
 83         private int _dropDownListType = 2;
 84 
 85  
 86 
 87         /// <summary>
 88 
 89         /// 交易類型
 90 
 91         /// 1:金額
 92 
 93         /// 2:單價
 94 
 95         /// </summary>
 96 
 97         public int DropDownListType { get { return this._dropDownListType; } }
 98 
 99  
100 
101         public RolesUserControl()
102 
103         {
104 
105             InitializeComponent();
106 
107         }
108 
109  
110 
111         /// <summary>
112 
113         /// 構造
114 
115         /// </summary>
116 
117         /// <param name="strLast"></param>
118 
119         public RolesUserControl(string strLast)
120 
121         {
122 
123             InitializeComponent();
124 
125             this.tb_last1.Text = strLast;
126 
127         }
128 
129  
130 
131         /// <summary>
132 
133         /// 鼠標移開TextBox
134 
135         /// </summary>
136 
137         /// <param name="sender"></param>
138 
139         /// <param name="e"></param>
140 
141         private void tb_last2_Leave(object sender, EventArgs e)
142 
143         {
144 
145             decimal d = 0.00m;
146 
147             if (!decimal.TryParse(this.tb_last2.Text, out d))
148 
149             {
150 
151                 isRegistOK = false;
152 
153                 this.tb_last2.Text = "請在這裏輸入格式爲24.5的數據";
154 
155                 return;
156 
157             }
158 
159  
160 
161             if (decimal.Parse(this.tb_last1.Text.Trim()) >= d)
162 
163             {
164 
165                 isRegistOK = false;
166 
167                 this.tb_last2.Text = "下限數據應該大於上限數據";
168 
169                 return;
170 
171             }
172 
173  
174 
175             if (onLeftTextBox != null)
176 
177             {
178 
179                 DeleteEventArgs dea = new DeleteEventArgs();
180 
181                 dea.ControlName = this.Name;
182 
183                 dea.Index = this.Index;
184 
185                 dea.StrText = this.tb_last2.Text.Trim();
186 
187                 this.onLeftTextBox(sender, dea);
188 
189             }
190 
191             this.isRegistOK = true;
192 
193  
194 
195         }
196 
197  
198 
199         /// <summary>
200 
201         /// 點擊關閉
202 
203         /// </summary>
204 
205         /// <param name="sender"></param>
206 
207         /// <param name="e"></param>
208 
209         private void pictureBox1_Click(object sender, EventArgs e)
210 
211         {
212 
213             if (this.onDelete != null)
214 
215             {
216 
217                 DeleteEventArgs e1 = new DeleteEventArgs();
218 
219                 e1.ControlName = this.Name;
220 
221                 e1.Index = this.Index;
222 
223                 onDelete(sender, e1);
224 
225             }
226 
227         }
228 
229  
230 
231         /// <summary>
232 
233         /// 切換類型,是使用單價仍是使用金額
234 
235         /// </summary>
236 
237         /// <param name="sender"></param>
238 
239         /// <param name="e"></param>
240 
241         private void ddl_type_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
242 
243         {
244 
245             if (this.ddl_type.Text == "金額")
246 
247             {
248 
249                 radLabel1.Text = "噸   金額:";
250 
251                 tb_amount.Visible = true;
252 
253                 tb_amount.Text = "";
254 
255                 this._dropDownListType = 1;
256 
257             }
258 
259             else
260 
261             {
262 
263                 radLabel1.Text = "噸   單價:";
264 
265                 tb_amount.Visible = false;
266 
267                 tb_amount.Text = "";
268 
269                 this._dropDownListType = 2;
270 
271             }
272 
273         }
274 
275  
276 
277         /// <summary>
278 
279         /// 判斷是否知足條件
280 
281         /// </summary>
282 
283         /// <param name="sender"></param>
284 
285         /// <param name="e"></param>
286 
287         private void tb_amount_Leave(object sender, EventArgs e)
288 
289         {
290 
291             decimal d = 0.00m;
292 
293             if (!decimal.TryParse(this.tb_amount.Text, out d))
294 
295             {
296 
297                 isRegistOK = false;
298 
299                 this.tb_amount.Text = "請在這裏輸入格式爲24.5的數據";
300 
301                 return;
302 
303             }
304 
305  
306 
307             if (onLeftTextBox != null)
308 
309             {
310 
311                 DeleteEventArgs dea = new DeleteEventArgs();
312 
313                 dea.ControlName = this.Name;
314 
315                 dea.Index = this.Index;
316 
317                 dea.StrText = this.tb_last2.Text.Trim();
318 
319                 this.onLeftTextBox(sender, dea);
320 
321             }
322 
323             this.isRegistOK = true;
324 
325         }
326 
327  
328 
329         private void tb_amount_KeyPress(object sender, KeyPressEventArgs e)
330 
331         {
332 
333             if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 190 || e.KeyChar == 110 || e.KeyChar == 13 || e.KeyChar == 8 || e.KeyChar == 46)
334 
335             { }
336 
337             else
338 
339             {
340 
341                 tb_amount.Text = "";
342 
343                 MessageBox.Show("請輸入數字");
344 
345                 return;
346 
347             }
348 
349         }
350 
351 }

 

 

 

那麼對於上限,其實他也是個線性的,那麼咱們也來創建一個控件code

 

 

這個控件用來計算最後臨界值。htm

下面是重點代碼,供你們解讀blog

  1 /// <summary>
  2 
  3         /// 添加保存事件
  4 
  5         /// </summary>
  6 
  7         /// <param name="sender"></param>
  8 
  9         /// <param name="e"></param>
 10 
 11         private void radButton2_Click(object sender, EventArgs e)
 12 
 13         {
 14 
 15             if (this.tb_RoleName.Text.Trim() == "")
 16 
 17             {
 18 
 19                 this.MessageBoxShow("請輸入角色名稱");
 20 
 21                 return;
 22 
 23             }
 24 
 25  
 26 
 27             //解析界面數據
 28 
 29             Model.AutoWater_Sys_BaseRoles br = new Model.AutoWater_Sys_BaseRoles();
 30 
 31             br.RolesName = this.tb_RoleName.Text.Trim();
 32 
 33             br.CreateBy = this.CurrentUserName;
 34 
 35             br.CreateOn = DateTime.Now;
 36 
 37             br.IsUsing = true;
 38 
 39             br.IsDefault = false;
 40 
 41             br.RowState = 1;
 42 
 43             br.ID = Guid.NewGuid();
 44 
 45  
 46 
 47             List<Model.AutoWater_Sys_BaseRolesDetail> lbrd = new List<Model.AutoWater_Sys_BaseRolesDetail>();
 48 
 49  
 50 
 51             List<RolesUserControl> _List = new List<RolesUserControl>();
 52 
 53             //開始範圍
 54 
 55             //rolesUserControl1 ,校驗數據是否正確
 56 
 57             _List.Add(this.rolesUserControl1);
 58 
 59             string strMsg="";
 60 
 61             bool isVaild = this.VaildRolesUserControl(this.rolesUserControl1, ref strMsg);
 62 
 63             if (!isVaild)
 64 
 65             {
 66 
 67                 _List.Clear();
 68 
 69                 MessageBoxShow(strMsg);
 70 
 71                 return;
 72 
 73             }
 74 
 75             //遍歷Panel
 76 
 77             foreach (Control c in this.radPanel1.Controls)
 78 
 79             {
 80 
 81                 isVaild = this.VaildRolesUserControl(c as RolesUserControl, ref strMsg);
 82 
 83                 if (!isVaild)
 84 
 85                 {
 86 
 87                     _List.Clear();
 88 
 89                     MessageBoxShow(strMsg);
 90 
 91                     return;
 92 
 93                 }
 94 
 95                 _List.Add(c as RolesUserControl);
 96 
 97             }
 98 
 99             string strValue1,strValue2,strValue3,strValues4;
100 
101             _List.ForEach(t => {
102 
103                 //Last1
104 
105                 strValue1 = (t.Controls.Find("tb_last1", true)[0] as RadTextBox).Text;
106 
107                 //Last2
108 
109                 strValue2 = (t.Controls.Find("tb_last2", true)[0] as RadTextBox).Text;
110 
111                 //Price
112 
113                 if (t.DropDownListType == 1)
114 
115                 {
116 
117                     strValue3 = (t.Controls.Find("tb_amount", true)[0] as RadTextBox).Text;
118 
119                 }
120 
121                 else
122 
123                 {
124 
125                     strValue3 = (t.Controls.Find("ddl_price", true)[0] as RadDropDownList).Text;
126 
127                 }
128 
129  
130 
131                 lbrd.Add(new Model.AutoWater_Sys_BaseRolesDetail() {
132 
133                  BaseTablesID=br.ID,
134 
135                   ID=Guid.NewGuid(),
136 
137                    BetweenValue=decimal.Parse(strValue2)-decimal.Parse(strValue1),
138 
139                     CountType=t.DropDownListType,
140 
141                      CountTypeValue=decimal.Parse(strValue3),
142 
143                       CreateBy=this.CurrentUserName,
144 
145                        CreateOn=DateTime.Now,
146 
147                         DownValue=decimal.Parse(strValue1),//下限不低於
148 
149                          UpValue = decimal.Parse(strValue2),//上限不超過
150 
151                           SortID=t.Index,
152 
153                 });
154 
155             });
156 
157  
158 
159             //檢查範圍N
160 
161             //單價
162 
163             strValues4 = (this.userLastRolesControl1.Controls.Find("ddl_price", true)[0] as RadDropDownList).Text;
164 
165             //噸數範圍
166 
167             strValue3= (this.userLastRolesControl1.Controls.Find("tb_last2", true)[0] as RadTextBox).Text;
168 
169             if (strValues4 != "" && strValue3!="")
170 
171             {
172 
173                 lbrd.Add(new Model.AutoWater_Sys_BaseRolesDetail() {
174 
175                  BaseTablesID=br.ID,
176 
177                   SortID=9,
178 
179                    BetweenValue=decimal.Parse(strValue3),
180 
181                     UpValue=0.00m,
182 
183                       DownValue = decimal.Parse(strValue3),
184 
185                       CreateOn=DateTime.Now,
186 
187                        CreateBy=this.CurrentUserName,
188 
189                         ID=Guid.NewGuid(),
190 
191                          CountTypeValue=decimal.Parse(strValues4),
192 
193                           CountType=3,
194 
195                 });
196 
197             }
198 
199  
200 
201             Combin<Model.AutoWater_Sys_BaseRoles, List<Model.AutoWater_Sys_BaseRolesDetail>> combinList = new Combin<Model.AutoWater_Sys_BaseRoles, List<Model.AutoWater_Sys_BaseRolesDetail>>(br,lbrd);
202 
203             bool isAddOK=SettingHelper.Init.AddBaseRoles(combinList);
204 
205             if (isAddOK)
206 
207             {
208 
209                 this.MessageBoxShow("創建規則成功!");
210 
211                 combinList = null;
212 
213                 lbrd.Clear();
214 
215                 br = null;
216 
217                 lbrd = null;
218 
219                 this.Close();
220 
221             }
222 
223             else
224 
225             {
226 
227                 this.MessageBoxShow("創建規則失敗!");
228 
229             }
230 
231         }

 

 

到此以上就是設置了定義計算規則的邏輯,是否是很簡單,有了這些數據的規則以後,接下來就是解析這些數據了,有興趣的朋友能夠本身設計下,而後本身解析,由於這些在老菜鳥看來這些都是兒科的玩意,好了今天就和你們分享到這裏,明天給你們分享下,我這個老菜鳥是如何開發發票設計器的,如何來動態設計發票模板,各位88索引

相關文章
相關標籤/搜索