前戏:针对上一篇列出来的功能点,今天和大家分享下这个自定义的公式是怎么设计的,由于我的第一篇博客在首页被管理员移走了,大家可以点击这里来跳转,看下第一篇的目录结构。本人作为老菜鸟,和大家分享的也是一些老菜鸟的想法,大神千万别喷我.
设计背景:当初为什么要设计这个自定义的计算公式呢,原因就是,这个价格是不确定的,计算方式也是不确定的,那如果在代码里面写死,那将来修改起来会比较麻烦,作为老菜鸟,我想偷懒了,也是为了省事。
如何设计:这对目前项目的情况,计算的过程应该是按照线性的方式来计算的,那么问题就简单了。
首先我们先要定义一个范围控件,这个控件的目的就是,在某个区间之内,设置固定的金额或者单价,
一下我列出了部分代码,供大家提供思路
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 ///20 21 /// 控件索引 22 23 /// 24 25 public int Index { get; set; } 26 27 28 29 /// 30 31 /// 是否验证成功 32 33 /// 34 35 public bool isRegistOK { get; set; } 36 37 38 39 /// 40 41 /// 是否启用关闭 42 43 /// 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 /// 62 63 /// 是否显示关闭 64 65 /// 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 /// 88 89 /// 交易类型 90 91 /// 1:金额 92 93 /// 2:单价 94 95 /// 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 /// 112 113 /// 构造 114 115 /// 116 117 /// 118 119 public RolesUserControl(string strLast) 120 121 { 122 123 InitializeComponent(); 124 125 this.tb_last1.Text = strLast; 126 127 } 128 129 130 131 /// 132 133 /// 鼠标移开TextBox 134 135 /// 136 137 /// 138 139 /// 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 /// 200 201 /// 点击关闭 202 203 /// 204 205 /// 206 207 /// 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 /// 232 233 /// 切换类型,是使用单价还是使用金额 234 235 /// 236 237 /// 238 239 /// 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 /// 278 279 /// 判断是否满足条件 280 281 /// 282 283 /// 284 285 /// 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 }
那么对于上限,其实他也是个线性的,那么我们也来建立一个控件
这个控件用来计算最后临界值。
下面是重点代码,供大家解读
1 ///2 3 /// 添加保存事件 4 5 /// 6 7 /// 8 9 /// 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 lbrd = new List (); 48 49 50 51 List _List = new List (); 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 > combinList = new Combin >(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