PageRenderTime 5ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/CmsCheckin/ListNames.cs

https://bitbucket.org/mahalowe/bvcms
C# | 222 lines | 202 code | 20 blank | 0 comment | 22 complexity | 9a1a2dcac9bdbc172433e1ed9486360f MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.ComponentModel;
  4using System.Drawing;
  5using System.Data;
  6using System.Linq;
  7using System.Text;
  8using System.Windows.Forms;
  9using System.Net;
 10using System.Xml.Linq;
 11using System.Configuration;
 12using System.IO;
 13using System.Collections.Specialized;
 14
 15namespace CmsCheckin
 16{
 17    public partial class ListNames : UserControl
 18    {
 19		UserControl nextcontrol;
 20		public ListNames(UserControl next)
 21        {
 22            InitializeComponent();
 23			this.nextcontrol = next;
 24        }
 25
 26        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
 27        {
 28            const int WM_KEYDOWN = 0x100;
 29            const int WM_SYSKEYDOWN = 0x104;
 30
 31            if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
 32            {
 33                switch (keyData)
 34                {
 35                    case Keys.PageUp:
 36                        if (pgup.Visible)
 37                            ShowResults(name, prev.Value);
 38                        return true;
 39                    case Keys.PageDown:
 40                        if (pgdn.Visible)
 41                            ShowResults(name, next.Value);
 42                        return true;
 43                    case Keys.Escape:
 44                        Program.TimerStop();
 45                        if (Program.HideCursor)
 46                            Cursor.Hide();
 47                        this.GoHome(string.Empty);
 48                        return true;
 49                    case Keys.S | Keys.Alt:
 50                        Program.TimerReset();
 51                        Program.CursorShow();
 52                        foreach (var c in sucontrols)
 53                        {
 54                            c.Enabled = true;
 55                            c.BackColor = Color.Coral;
 56                        }
 57                        return true;
 58                }
 59            }
 60            return base.ProcessCmdKey(ref msg, keyData);
 61        }
 62
 63        int? next, prev;
 64        string name;
 65        List<Control> controls = new List<Control>();
 66        List<Control> sucontrols = new List<Control>();
 67
 68        public void ShowResults(string match, int page)
 69        {
 70            ClearControls();
 71
 72            name = match;
 73            var x = this.GetDocument("Checkin2/NameSearch/" + name + "?page=" + page);
 74
 75            var points = 14F;
 76
 77            string Verdana = "Verdana";
 78            Font labfont;
 79            var g = this.CreateGraphics();
 80
 81            name = x.Root.Attribute("name").Value;
 82            next = x.Root.Attribute("next").Value.ToInt2();
 83            prev = x.Root.Attribute("prev").Value.ToInt2();
 84            pgdn.Visible = next.HasValue;
 85            pgup.Visible = prev.HasValue;
 86
 87            if (x.Descendants("person").Count() == 0)
 88            {
 89                var lab = new Label();
 90                lab.Font = new Font(Verdana, points, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
 91                lab.Location = new Point(15, 200);
 92                lab.AutoSize = true;
 93                lab.Text = "Not Found, try another name?";
 94                this.Controls.Add(lab);
 95                controls.Add(lab);
 96                GoBackButton.Text = "Try again";
 97                return;
 98            }
 99
100            const int WidName = 890;
101            while (true)
102            {
103                var wid = 0;
104                labfont = new Font(Verdana, points, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
105                foreach (var e in x.Descendants("person"))
106                {
107                    var s = e.Attribute("display").Value;
108                    var size = g.MeasureString(s, labfont);
109                    wid = Math.Max(wid, (int)size.Width);
110                }
111                if (wid > WidName)
112                {
113                    points -= 1F;
114                    continue;
115                }
116                break;
117            }
118            var row = 0;
119            foreach (var e in x.Descendants("person"))
120            {
121                var ab = new Button();
122                ab.BackColor = SystemColors.ControlLight;
123                ab.Font = labfont;
124                const int Gutter = 10;
125                ab.Location = new Point(Gutter, 100 + (row * 50));
126                ab.Tag = e.Attribute("fid").Value.ToInt();
127                var homephone = e.Attribute("home").Value;
128                var cellphone = e.Attribute("cell").Value;
129                var ph = homephone.HasValue() ? homephone : cellphone.HasValue() ? cellphone : "";
130                if (!ph.HasValue())
131                    ab.BackColor = Color.LightPink;
132
133                ab.Size = new Size(WidName, 45);
134                ab.TextAlign = ContentAlignment.MiddleLeft;
135                ab.UseVisualStyleBackColor = false;
136                ab.Text = e.Attribute("display").Value;
137                this.Controls.Add(ab);
138                ab.Click += new EventHandler(ab_Click);
139                controls.Add(ab);
140
141                row++;
142            }
143            Program.TimerStart(timer1_Tick);
144            this.Focus();
145        }
146
147        void timer1_Tick(object sender, EventArgs e)
148        {
149            Program.TimerStop();
150            Program.ClearFields();
151            this.GoHome("");
152        }
153
154        void ab_Click(object sender, EventArgs e)
155        {
156            Program.TimerStop();
157            var ab = sender as Button;
158            this.Swap(nextcontrol);
159			if (nextcontrol is ListFamily)
160				((ListFamily)nextcontrol).ShowFamily((int)ab.Tag);
161			else if (nextcontrol is ListFamily2)
162				((ListFamily2)nextcontrol).ShowFamily((int)ab.Tag);
163        }
164
165        private void GoBack_Click(object sender, EventArgs e)
166        {
167			if (Program.baseform.textbox.Parent is Home)
168			{
169				Program.home.textBox1.Text = name;
170				this.Swap(Program.home.namesearch);
171			}
172			else if (Program.baseform.textbox.Parent is Home2)
173			{
174				Program.home2.namesearch.textBox1.Text = name;
175				this.Swap(Program.home2.namesearch);
176			}
177        }
178
179        private void AddNewFamily_Click(object sender, EventArgs e)
180        {
181            Program.FamilyId = 0;
182            Program.editing = false;
183			if (Program.baseform.textbox.Parent is Home)
184				this.Swap(Program.home.first);
185			else if (Program.baseform.textbox.Parent is Home2)
186				this.Swap(Program.home2.first);
187        }
188
189        private void ClearControls()
190        {
191            foreach (var c in controls)
192            {
193                this.Controls.Remove(c);
194                c.Dispose();
195            }
196            controls.Clear();
197            sucontrols.Clear();
198            sucontrols.Add(bAddNewFamily);
199            bAddNewFamily.BackColor = SystemColors.Control;
200            bAddNewFamily.Enabled = false;
201        }
202        private void pgdn_Click(object sender, EventArgs e)
203        {
204            ShowResults(name, next.Value);
205        }
206
207        private void pgup_Click(object sender, EventArgs e)
208        {
209            ShowResults(name, prev.Value);
210        }
211
212        private void button2_Click(object sender, EventArgs e)
213        {
214            Program.TimerReset();
215            foreach (var c in sucontrols)
216            {
217                c.Enabled = true;
218                c.BackColor = Color.Coral;
219            }
220        }
221    }
222}