PageRenderTime 48ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/CmsCheckin/ListClasses.cs

https://bitbucket.org/mahalowe/bvcms
C# | 260 lines | 242 code | 18 blank | 0 comment | 25 complexity | 25d266f91a2d17df2cf1e7f1b424e8c7 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0, BSD-3-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception, AGPL-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Net;
  10. using System.Xml.Linq;
  11. using System.Configuration;
  12. using System.IO;
  13. using System.Collections.Specialized;
  14. namespace CmsCheckin
  15. {
  16. public partial class ListClasses : UserControl
  17. {
  18. public ListClasses()
  19. {
  20. InitializeComponent();
  21. }
  22. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  23. {
  24. const int WM_KEYDOWN = 0x100;
  25. const int WM_SYSKEYDOWN = 0x104;
  26. if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
  27. {
  28. switch (keyData)
  29. {
  30. case Keys.PageUp:
  31. if (pgup.Visible)
  32. ShowPage(page - 1);
  33. return true;
  34. case Keys.PageDown:
  35. if (pgdn.Visible)
  36. ShowPage(page + 1);
  37. return true;
  38. case Keys.Escape:
  39. this.Swap(Program.home.family);
  40. Program.home.family.ShowFamily(FamilyId);
  41. return true;
  42. case Keys.S | Keys.Alt:
  43. Program.TimerReset();
  44. Program.CursorShow();
  45. return true;
  46. }
  47. }
  48. return base.ProcessCmdKey(ref msg, keyData);
  49. }
  50. private List<ClassInfo> list;
  51. private float points;
  52. private Font Labfont;
  53. private string Verdana;
  54. private XDocument x;
  55. DateTime time;
  56. int FamilyId;
  57. int PeopleId;
  58. int page;
  59. List<Control> controls = new List<Control>();
  60. bool ShowAllClasses;
  61. public bool JoiningNotAttending = false;
  62. public void ShowResults(int pid)
  63. {
  64. var url = "Checkin2/Classes/" + pid + Program.QueryString;
  65. if (ShowAllClasses)
  66. url += "&noagecheck=true";
  67. x = this.GetDocument(url);
  68. time = DateTime.Now;
  69. points = 14F;
  70. Verdana = "Verdana";
  71. FamilyId = x.Root.Attribute("fid").Value.ToInt();
  72. PeopleId = x.Root.Attribute("pid").Value.ToInt();
  73. if (x.Descendants("class").Count() == 0)
  74. {
  75. ClearControls();
  76. var lab = new Label();
  77. lab.Font = new Font(Verdana, points, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
  78. lab.Location = new Point(15, 200);
  79. lab.AutoSize = true;
  80. lab.Text = "Sorry, no classes found";
  81. this.Controls.Add(lab);
  82. controls.Add(lab);
  83. GoBackButton.Text = "Go Back";
  84. return;
  85. }
  86. list = new List<ClassInfo>();
  87. foreach (var e in x.Descendants("class"))
  88. {
  89. var hr = DateTime.Today;
  90. DateTime.TryParse(e.Attribute("hour").Value, out hr);
  91. var ci = new ClassInfo
  92. {
  93. display = e.Attribute("display").Value,
  94. oid = e.Attribute("orgid").Value.ToInt(),
  95. pid = PeopleId,
  96. nlabels = e.Attribute("nlabels").Value.ToInt(),
  97. hour = hr
  98. };
  99. var leadtime = double.Parse(e.Attribute("leadtime").Value);
  100. double howlate = -(Program.EarlyCheckin / 60d);
  101. if (ci.oid != 0 && leadtime <= Program.LeadTime && leadtime >= howlate)
  102. list.Add(new ClassInfo
  103. {
  104. display = e.Attribute("display").Value,
  105. oid = e.Attribute("orgid").Value.ToInt(),
  106. pid = PeopleId,
  107. nlabels = e.Attribute("nlabels").Value.ToInt(),
  108. hour = hr
  109. });
  110. }
  111. ShowPage(1);
  112. }
  113. public void ShowPage(int page)
  114. {
  115. ClearControls();
  116. this.page = page;
  117. var g = this.CreateGraphics();
  118. while (true)
  119. {
  120. var wid = 0;
  121. Labfont = new Font(Verdana, points, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
  122. foreach (var c in list)
  123. {
  124. var size = g.MeasureString(c.display, Labfont);
  125. wid = Math.Max(wid, (int)size.Width);
  126. }
  127. if (wid > 1000)
  128. {
  129. points -= 1F;
  130. continue;
  131. }
  132. break;
  133. }
  134. const int PageSize = 10;
  135. int srow = (page - 1) * PageSize;
  136. int erow = srow + PageSize;
  137. if (erow > list.Count)
  138. erow = list.Count;
  139. pgdn.Visible = list.Count > erow;
  140. pgup.Visible = srow > 0;
  141. int rowheight = 50;
  142. int top = 50;
  143. for (var r = srow; r < erow; r++)
  144. {
  145. var c = list[r];
  146. var ab = new Button();
  147. controls.Add(ab);
  148. ab.BackColor = SystemColors.ControlLight;
  149. ab.Font = Labfont;
  150. top += rowheight;
  151. ab.Location = new Point(10, top);
  152. ab.Size = new Size(1000, 45);
  153. ab.TextAlign = ContentAlignment.MiddleLeft;
  154. ab.UseVisualStyleBackColor = false;
  155. ab.Tag = c;
  156. ab.Text = c.display;
  157. this.Controls.Add(ab);
  158. ab.Click += new EventHandler(ab_Click);
  159. }
  160. Program.TimerStart(timer1_Tick);
  161. }
  162. void timer1_Tick(object sender, EventArgs e)
  163. {
  164. Program.TimerStop();
  165. Util.UnLockFamily();
  166. Program.ClearFields();
  167. ShowAllClasses = false;
  168. this.GoHome("");
  169. }
  170. void ab_Click(object sender, EventArgs e)
  171. {
  172. var ab = sender as Button;
  173. var c = ab.Tag as ClassInfo;
  174. if (JoiningNotAttending)
  175. Util.JoinUnJoin(c, true);
  176. else
  177. Util.AttendUnAttend(new Util.ClassCheckedInfo { c = c, ischecked = true });
  178. ShowAllClasses = false;
  179. JoiningNotAttending = false;
  180. if (Program.baseform.textbox.Parent is Home)
  181. {
  182. this.Swap(Program.home.family);
  183. Program.home.family.classlist.Add(c);
  184. Program.home.family.ShowFamily(FamilyId);
  185. }
  186. else if (Program.baseform.textbox.Parent is Home2)
  187. {
  188. this.Swap(Program.home2.family);
  189. Program.home2.family.classlist.Add(c);
  190. Program.home2.family.ShowFamily(FamilyId);
  191. }
  192. }
  193. private void GoBack_Click(object sender, EventArgs e)
  194. {
  195. JoiningNotAttending = false;
  196. if (Program.baseform.textbox.Parent is Home)
  197. {
  198. this.Swap(Program.home.family);
  199. Program.home.family.ShowFamily(FamilyId);
  200. }
  201. else if (Program.baseform.textbox.Parent is Home2)
  202. {
  203. this.Swap(Program.home2.family);
  204. Program.home2.family.ShowFamily(FamilyId);
  205. }
  206. ShowAllClasses = false;
  207. }
  208. private void ClearControls()
  209. {
  210. JoiningNotAttending = false;
  211. foreach (var c in controls)
  212. {
  213. this.Controls.Remove(c);
  214. c.Dispose();
  215. }
  216. controls.Clear();
  217. }
  218. private void pgdn_Click(object sender, EventArgs e)
  219. {
  220. ShowPage(page + 1);
  221. }
  222. private void pgup_Click(object sender, EventArgs e)
  223. {
  224. ShowPage(page - 1);
  225. }
  226. private void allclasses_Click(object sender, EventArgs e)
  227. {
  228. ShowAllClasses = true;
  229. ShowResults(PeopleId);
  230. }
  231. }
  232. public class ClassInfo
  233. {
  234. public string display { get; set; }
  235. public int oid { get; set; }
  236. public int pid { get; set; }
  237. public string mv { get; set; }
  238. public string bdays { get; set; }
  239. public int nlabels { get; set; }
  240. public DateTime? hour { get; set; }
  241. }
  242. }