ホテル管理システム
ogi
yesterday 1a1c8e71fcd14858f595029f089b2d4a00202b32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using HotelPms.Share.Util;
using System.Drawing;
using HotelPms.Share.Windows.Component;
 
namespace HotelPms.Share.Windows.Util
{
    public class CTextBoxArray
    {
        private class ItemsComparer : IComparer<CTextBox>
        {
            public int Compare(CTextBox x, CTextBox y)
            {
                if (x == null)
                {
                    return (y == null ? 0 : -1);
                }
                else
                {
                    if (y == null)
                    {
                        return 1;
                    }
                    else
                    {
                        return x.TabIndex.CompareTo(y.TabIndex);
                    }
                }
            }
        }
        private ItemsComparer itemsComparer = new ItemsComparer();
 
        public delegate void TBArrayEventHandler(object sender, int index, CTextBoxArrayBaseEventArgs e);
        public delegate bool TBArrayResultEventHandler(object sender, int index, CTextBoxArrayBaseEventArgs e);
        public delegate void TBArrayValidatingEventHandler(object sender, int index, CancelEventArgs e);
        public delegate void TBArrayKeyDownEventHandler(object sender, int index, KeyEventArgs e);
        public delegate void TBArrayPreviewKeyDownEventHandler(object sender, int index, PreviewKeyDownEventArgs e);
        public delegate void TBArrayKeyPressEventHandler(object sender, int index, KeyPressEventArgs e);
 
        public event TBArrayKeyPressEventHandler KeyPress;
        public event TBArrayKeyDownEventHandler KeyDown;
        public event TBArrayPreviewKeyDownEventHandler PreviewKeyDown;
        public event TBArrayKeyDownEventHandler BeforeAutoNextFocus;        
        public event TBArrayKeyDownEventHandler AfterAutoNextFocus;        
        public event TBArrayValidatingEventHandler Validating;        
        public event TBArrayEventHandler TextChanged;
        public event TBArrayEventHandler Enter;
        public event TBArrayEventHandler AfterEnter;
        public event TBArrayEventHandler Leave;
        public event TBArrayEventHandler AfterLeave;
        public event TBArrayEventHandler Validated;
 
        public event TBArrayEventHandler ValueChanged;  //“ü—͍€–ڕύX‚µ‚½‚©‚Ç‚¤‚©
        public event TBArrayResultEventHandler IsCorrectEtc; //ƒRƒ“ƒgƒ[ƒ‹ƒvƒƒpƒeƒBˆÈŠO‚̃`ƒFƒbƒN‚ðs‚¤‚Æ‚«‚É‚±‚̃Cƒxƒ“ƒg“à‚ōs‚¤B
        public event TBArrayKeyDownEventHandler NextFocus;
        public event TBArrayKeyDownEventHandler KeyEnd;
        public event TBArrayEventHandler ForceValidating;
        public event TBArrayKeyPressEventHandler MultiKeyPress;
 
        private List<CTextBox> items = new List<CTextBox>();
        private string orgMstName = string.Empty;
        private int acitveIndex = 0;
        private Color orgBkColor = Color.White;
        private bool isChangeLine = false;
        private bool disbleValidating = false;
        private bool disbleTextChanged = false;
        private bool autoNextFocus = false;
        private bool m_ReadOnly = false;
 
        private bool m_ForceChangeLine = false;
 
        /// <summary>
        /// ƒ}ƒ‹ƒ`“ü—͂̏ꍇAuEnterv‚ʼnüs
        /// </summary>
        public bool ForceChangeLine
        {
            get { return m_ForceChangeLine; }
            set { m_ForceChangeLine = value; }
        }
 
        private bool m_UsingOrgBackColor = false;
 
        public bool UsingOrgBackColor
        {
            get { return m_UsingOrgBackColor; }
            set { m_UsingOrgBackColor = value; }
        }
 
        /// <summary>
        /// 
        /// </summary>
        public bool ReadOnly
        {
            get { return m_ReadOnly; }
            set 
            {
                try
                {
                    m_ReadOnly = value;
                    foreach (CTextBox item in items) { item.ReadOnly = m_ReadOnly; }
                }
                catch { }
            }
        }
 
 
        /// <summary>
        /// Œ³‚̃}ƒXƒ^–¼iƒtƒHƒbƒJƒX‚ÌŽžj
        /// </summary>
        public string OrgMstName
        {
            get { return orgMstName; }
            set { orgMstName = value; }
        }
 
        public List<CTextBox> Items
        {
            get { return items; }
        }
 
        public bool DisbleValidating
        {
            get { return disbleValidating; }
            set { disbleValidating = value; }
        }
 
        public bool DisbleTextChanged
        {
            get { return disbleTextChanged; }
            set { disbleTextChanged = value; }
        }
 
        /// <summary>
        /// TAB‡ˆÊ‚ŃtƒH[ƒJƒX‚·‚é‚©‚Ç‚¤‚©
        /// </summary>
        public bool AutoNextFocus
        {
            get { return autoNextFocus; }
            set { autoNextFocus = value; }
        }
 
        private bool m_ShiftEnterEnabled = false;
 
        /// <summary>
        /// ShiftEnter = KeyUp ? 
        /// </summary>
        public bool ShiftEnterEnabled
        {
            get { return m_ShiftEnterEnabled; }
            set { m_ShiftEnterEnabled = value; }
        }
 
 
        public CTextBoxArray()
            : this(false, null)
        { }
 
        public CTextBoxArray(bool autoNextFocus)
            : this(autoNextFocus, null)
        { }
 
        public CTextBoxArray(bool autoNextFocus, Control owner)
        {
            this.autoNextFocus = autoNextFocus;
            if (owner != null) { InitInputSetting(owner); }
        }
 
        public void Add(CTextBox item)
        {
            Add(item, -1);
        }
 
        public void Add(CTextBox item, int index)
        {
            item.Enter += new EventHandler(this.Cell_Enter);
            item.Leave += new EventHandler(this.Cell_Leave);
            item.KeyPress += new KeyPressEventHandler(this.Cell_KeyPress);
            item.KeyDown += new KeyEventHandler(Cell_KeyDown);
            item.PreviewKeyDown += new PreviewKeyDownEventHandler(Cell_PreviewKeyDown);
            item.TextChanged += new EventHandler(this.Cell_TextChanged);
            item.Validating += new CancelEventHandler(this.Cell_Validating);
            item.Validated += new EventHandler(this.Cell_Validated);
            item.DoubleClick += new EventHandler(Cell_DoubleClick);
            if (index >= 0)
            {
                items.Insert(index,item);
            }
            else
            {
                items.Add(item);
            }
            //enableManager.Add(new ControlEnableManager(item));
        }
 
        private void Cell_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            int index = items.IndexOf((sender as CTextBox));
            if (PreviewKeyDown != null)
            {
                PreviewKeyDown(this, index, e);
            }
        }
 
 
        public static void SetCtrlEnabled(Control obj,bool value)
        {
            try
            {
                if (obj is CTextBox)
                {
                    (obj as CTextBox).Enabled = value;
                }
                else
                {
                    obj.Enabled = value;
                }
            }
            catch { }
        }
 
 
        /// <summary>
        /// ‰æ–ʂɓü—͍€–ڈȊO‚ÌCTextBox‚ð“ü—Í•s‰Â‚É‚·‚é
        /// </summary>
        /// <param name="obj">e—eŠí</param>
        /// <param name="tbArray">“ü—ÍŠÇ—€–Ú</param>
        public void SetCTextProtect(Control obj)
        {
            foreach (Control item in obj.Controls)
            {
                if (item is UserControl) { continue; }
 
                if (item is CTextBox)
                {
                    if (!items.Contains(item as CTextBox)) { (item as CTextBox).Enabled = false; }
                }
 
                SetCTextProtect(item);
            }
        }
 
        public void ClearText()
        {
            foreach (CTextBox item in items)
            {
                item.Text = string.Empty;
                if (item.MstNameCtrl != null) 
                {
                    item.MstNameCtrl.Text = string.Empty; 
                }
            }
        }
 
        public void ClearTag()
        {
            foreach (CTextBox item in items)
            {
                item.Tag = null;
            }
        }
 
        public void PerformValidated(object sender, EventArgs e)
        {
            Cell_Validated(sender, e);
        }
 
        public void PerformValidating(object sender, CancelEventArgs e)
        {
            Cell_Validating(sender, e);
        }
 
        public void PerformEnter(object sender, EventArgs e)
        {
            Cell_Enter(sender, e);
        }
 
        public void PerformEnter()
        {
            Cell_Enter(items[acitveIndex], new EventArgs());
        }
 
        public void PerformKeyDown(object sender, KeyEventArgs e)
        { 
            Cell_KeyDown(sender, e);
        }
 
        private void SetItemValue(object sender, int index, string value)
        {
            (sender as CTextBox).TextChanged -= new EventHandler(Cell_TextChanged);
            this.Items[index].Text = value;
            (sender as CTextBox).TextChanged += new EventHandler(Cell_TextChanged);
        }
 
        private string GetItemValue(object sender, int index)
        {
            string value = string.Empty;
            value = this.Items[index].Text;
            return value;
        }
 
        private bool CheckValueChanged(object sender, int index)
        {
            string newInputValue = string.Empty;
            if ((InputCtrl.GetShowStyle(this.Items[index]) & numShowStyle.ThousandSeparator) == numShowStyle.ThousandSeparator)
            {
                newInputValue = CConvert.ToThousandSeparator(CConvert.ToDecimal(this.Items[index].Text));
            }
            else
            {
                newInputValue = GetItemValue(sender, index);
            }
            if (!newInputValue.Equals(this.Items[index].OrgText))
            {
                //’l•ύX‚µ‚½‚©‚Ç‚¤‚©
                if (ValueChanged != null)
                {
                    string saveText = GetItemValue(sender, index);
                    CTextBoxArrayBaseEventArgs vce = new CTextBoxArrayBaseEventArgs() { Cancel = false };
                    ValueChanged(this, index, vce);
                    if (vce.Cancel)
                    {
                        //Œ³‚Ì’l‚É–ß‚·
                        if (vce.ResetOrgValue) { SetItemValue(sender, index, this.Items[index].OrgText); }
                        return false;
                    }
                    SetItemValue(sender, index, saveText);
                }
            }
            return true;
        }
 
 
        private void SetInputValue(object sender, int index)
        {
            if ((InputCtrl.GetShowStyle(this.Items[index]) & numShowStyle.ThousandSeparator) == numShowStyle.ThousandSeparator)
            {
                this.Items[index].OrgText = CConvert.ToThousandSeparator(CConvert.ToDecimal(this.Items[index].Text));
            }
            else
            {
                this.Items[index].OrgText = GetItemValue(sender, index);
            }
            //System.Diagnostics.Debug.WriteLine(string.Format("SetInputValue({0},{1}) orgValue = {2}", (sender as Control).Name, index, orgValue));
        }
 
 
        private void Cell_KeyDown(object sender, KeyEventArgs e)
        {
            int index = items.IndexOf((sender as CTextBox));
            if (KeyDown != null)
            {
                KeyDown(this, index, e);
            }
            else
            {
                switch (e.KeyCode)
                {
                    case Keys.Down:
                    case Keys.Return:
                        if (m_ShiftEnterEnabled && e.KeyCode == Keys.Return && e.Shift)
                        {
                            Cell_KeyDown(sender, new KeyEventArgs(Keys.Up));
                            return;
                        }
 
                        if (e.Control == true && this.Items[index].Multiline) 
                        {
                            isChangeLine = true;
                            return; 
                        }
 
                        isChangeLine = false;
 
                        if (!this.Items[index].EnableUpDownKey && e.KeyCode == Keys.Up) { return; }
 
                        if (!m_ReadOnly)
                        {
                            if (!CheckValueChanged(sender, index)) { return; }  //’l•ύX‚µ‚½‚çAƒCƒxƒ“ƒg”­¶
 
                            if (!InputCtrl.IsCorrectText(this.Items[index]))
                            {
                                (sender as Control).Focus(); 
                                return;
                            }
 
                            //‚»‚ÌŠOƒ`ƒFƒbƒN
                            if (IsCorrectEtc != null)
                            {
                                if (!IsCorrectEtc(this, index, new CTextBoxArrayBaseEventArgs() { CheckAll = false, ShowErrMsg = true }))
                                {
                                    (sender as Control).Focus(); //‚µ‚½‚­‚È‚¢Žž‚́HH
                                    return;
                                }
                            }
 
                            SetInputValue(sender, index);
                        }
                        if (NextFocus != null) { NextFocus(this, index, e); }
                        if (autoNextFocus) { SetAutoNextFocus(index, e); }
                        break;
                    case Keys.Up:
                    case Keys.Escape:
                        if (m_ShiftEnterEnabled && e.KeyCode == Keys.Up && this.Items[index].Multiline && !IsTopLine(this.Items[index])) { return; }
                        if (!this.Items[index].EnableUpDownKey && e.KeyCode == Keys.Down) { return; }
                        if (NextFocus != null) { NextFocus(this, index, e); }
                        if (autoNextFocus) { SetAutoNextFocus(index, e); }
                        break;
                    case Keys.End:
                        if (m_ReadOnly) { e.Handled = true; return; }
                        if ((InputCtrl.GetShowStyle(this.Items[index]) & numShowStyle.ShowList) != numShowStyle.ShowList) { return; }
                        if (KeyEnd != null) 
                        {
                            e.Handled = true;
                            KeyEnd(this, index, e);
                        }
                        break;
                }
            }
        }
 
        private bool IsTopLine(CTextBox obj)
        {
            try
            {
                if (obj.SelectionStart == 0) { return true; }
 
                string line = obj.Text.Substring(0, obj.SelectionStart);
                return !line.Contains(Environment.NewLine);
            }
            catch
            {
                return true;
            }
        }
 
        private void Cell_Validated(object sender, EventArgs e)
        {
            if (m_ReadOnly) { return; }
            int index = items.IndexOf((sender as CTextBox));
            if (Validated != null)
            {
                Validated(this, index, new CTextBoxArrayBaseEventArgs());
            }
        }
 
        private void Cell_DoubleClick(object sender, EventArgs e)
        {
            if (m_ReadOnly) { return; }
            CTextBox curField = (sender as CTextBox);
            int index = items.IndexOf(curField);
            if ((InputCtrl.GetShowStyle(curField) & numShowStyle.ShowList) == numShowStyle.ShowList)
            { 
                //ˆê——•\ަ
                if (KeyEnd != null)
                {
                    KeyEnd(this, index, new KeyEventArgs(Keys.End));
                }
            }
        }
 
        private void Cell_Enter(object sender, EventArgs e)
        {
            CTextBox curField = (sender as CTextBox);
            System.Diagnostics.Debug.WriteLine("Cell_Enter  " + curField.Name);
            orgBkColor = curField.BackColor;
            int index = items.IndexOf(curField);
            acitveIndex = index;
            if (Enter != null)
            {
                Enter(this, index, new CTextBoxArrayBaseEventArgs());
            }
            else
            {
                this.Items[index].OrgText = curField.Text;
                System.Diagnostics.Debug.WriteLine("Cell_Enter  " + curField.Name + " Org = " + this.Items[index].OrgText);
                InputCtrl.TextEnter(curField);
                if (AfterEnter != null) { AfterEnter(this, index, new CTextBoxArrayBaseEventArgs()); }
            }
        }
 
        private void Cell_Leave(object sender, EventArgs e)
        {
            CTextBox curField = (sender as CTextBox);
            System.Diagnostics.Debug.WriteLine("Cell_Leave  " + curField.Name);
            int index = items.IndexOf((sender as CTextBox));
            if (Leave != null)
            {
                Leave(this, index, new CTextBoxArrayBaseEventArgs());
            }
            else
            {
                InputCtrl.TextLeave(curField);
                if (curField.ReadOnly || m_UsingOrgBackColor) { curField.BackColor = orgBkColor; }
            }
            if (AfterLeave != null) { AfterLeave(this, index, new CTextBoxArrayBaseEventArgs()); }
        }
 
        private void Cell_KeyPress(object sender, KeyPressEventArgs e)
        {
            //3 'Ctrl-C Copy  22 'Ctrl-V Paste 24 'Ctrl-X Cut //¬–Ø Redmine#8743
            if (e.KeyChar == 3 || e.KeyChar == 22 || e.KeyChar == 24)
            {
                return;
            }
 
            CTextBox curField = (sender as CTextBox);
            int index = items.IndexOf((sender as CTextBox));
            if (KeyPress != null)
            {
                KeyPress(this, index, e);
            }
            else
            {
                if (curField.Multiline) { if (MultiKeyPress != null) { MultiKeyPress(this, index, e); } }
                if (curField.IsChkData) { InputCtrl.TextBoxKeyPress(curField, e, curField.MaxLength, InputCtrl.GetInputType(curField)); }
                if (!m_ForceChangeLine)
                {
                    if (!isChangeLine && e.KeyChar == 13)
                    {
                        e.Handled = true;
                    }
                }
                isChangeLine = false;
            }
        }
 
        private void Cell_TextChanged(object sender, EventArgs e)
        {
            if (m_ReadOnly) { return; }
            if (disbleTextChanged) { return; }
            int index = items.IndexOf((sender as CTextBox));
            if (TextChanged != null)
            {
                TextChanged(this, index, new CTextBoxArrayBaseEventArgs());
            }
        }
 
        private void Cell_Validating(object sender, CancelEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Cell_Validating  " + (sender as Control).Name);
            if (m_ReadOnly) { return; }
            if (this.disbleValidating) { return; }
            int index = items.IndexOf((sender as CTextBox));
            if (Validating != null)
            {
                Validating(this, index, e);
            }
            else
            {
                bool isCheck = false;
                if (!this.Items[index].OrgText.Equals(this.Items[index].Text)) { isCheck = true; }
 
                if (!isCheck) 
                {
                    if (ForceValidating == null)
                    {
                        return;
                    }
                    else
                    {
                        //’l“¯‚¶‚Å‚àƒ`ƒFƒbƒNˆ—‚ðs‚¤
                        CTextBoxArrayBaseEventArgs forceValidatingEventArgs = new CTextBoxArrayBaseEventArgs() { Cancel = true };
                        ForceValidating(this, index, forceValidatingEventArgs);
                        if (forceValidatingEventArgs.Cancel) { return; }
                    }
                }
 
                bool isPass = false;
 
                //System.Diagnostics.Debug.WriteLine("Org:" + orgValue);
                if (!CheckValueChanged(sender, index)) { return; }  //’l•ύX‚µ‚½‚çAƒCƒxƒ“ƒg”­¶
 
                if (InputCtrl.IsCorrectText(this.Items[index], false))
                {
                    if (IsCorrectEtc == null)
                    {
                        isPass = true;
                    }
                    else
                    {
                        if (IsCorrectEtc(this, index, new CTextBoxArrayBaseEventArgs() { CheckAll = false, ShowErrMsg = false }))
                        {
                            isPass = true;
                        }
                    }
                }
 
                if (isPass)
                {
                    SetInputValue(sender, index);
                }
                else
                {
                    SetItemValue(sender, index, this.Items[index].OrgText);
                }
            }
        }
 
        private bool SetAutoNextFocus(int index, KeyEventArgs e)
        {
            try
            {
                if (BeforeAutoNextFocus != null) 
                { 
                    BeforeAutoNextFocus(this, index, e);
                    if (e.Handled) { return true; }
                }
                
                int hitIdx = -1;
                if (e.KeyCode == Keys.Return)
                {
                    for (int i = index + 1; i < items.Count; i++)
                    {
                        if (GeneralSub.CtrlCanFocus(items[i]))
                        {
                            hitIdx = i;
                            break;
                        }
                    }
 
                    if (hitIdx == -1) { return false; }
                }
                else if (e.KeyCode == Keys.Up)
                {
                    if (index == 0) { return true; }
 
                    for (int i = index - 1; i >= 0; i--)
                    {
                        if (GeneralSub.CtrlCanFocus(items[i]))
                        {
                            hitIdx = i;
                            break;
                        }
                    }
 
                    if (hitIdx == -1) { return false; }
                }
                items[hitIdx].Focus();
                e.Handled = true;
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                if (AfterAutoNextFocus != null) { AfterAutoNextFocus(this, index, e); }
            }
        }
 
        private void InitAllCtrl(Control owner)
        {
            foreach (Control obj in owner.Controls)
            {
                if ((obj is ITextBoxArray) && (obj as ITextBoxArray).TextBoxArrayMember)
                {
                    if (obj is CTextBox) { this.Add(obj as CTextBox); }
                }
 
                //ŽŸ‚ÌŠK‘w
                if (obj.Controls.Count > 0) { InitAllCtrl(obj); }
            }
        }
 
        /// <summary>
        /// “ü—͐ݒ菉Šú‰»‚·‚é
        /// </summary>
        /// <param name="ownerForm"></param>
        public void InitInputSetting(Control owner)
        {
            InitAllCtrl(owner);
            items.Sort(itemsComparer);                        
        }
    }
}