csharp:DropDownComboxTreeView

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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Resources;
using System.ComponentModel;
using System.Windows.Forms;
 
namespace CustomControls
{
 
 
    /// <summary>
    /// 涂聚文 2014-06-30
    /// DropDownTreeView class offers TreeView that is desined to act under a drop down control
    /// You can use this control when you need to select a value from a dynamic TreeView.  
    /// The sub controls of this control are set to protected. You can override the control
    /// to assign values to the subcontrol of change their behaviour.  
    /// http://www.codeproject.com/Articles/3994/Custom-ComboBoxes-with-Advanced-Drop-down-Features
    /// </summary>
    public class DropDownComboxTreeView : UserControl
    {
        /// <summary>
        /// protected Button control, this is the control's toggle button
        /// </summary>
        protected ButtonEx btnSelect;
        /// <summary>
        /// protected TextBox control, this is control's value TextBox.
        /// The TextBox's ReadyOnly false is by default set ti true
        /// </summary>
        protected ComboBox useComboxBox;
        /// <summary>
        /// protected TreeView control, this is the control's TreeView.
        /// </summary>
        protected TreeView treeView;
 
        /// <summary>
        /// protected value that is set to true of false within the
        ///  SetTextValueToSelectedNode method.
        /// </summary>
        protected bool TextValueSet=false;
 
        /// <summary>
        /// the resize label on the Control's TreeView control
        /// </summary>
        private LabelEx treeLabel;
 
        /// <summary>
        /// This is the margin in pixels for resizing the TreeView
        /// </summary>
        private int Margin = 10;
 
        /// <summary>
        /// TreeView's minimum width
        /// </summary>
        private int tvMinWidth = 0;
 
        /// <summary>
        /// TreeView's minimum height
        /// </summary>
        private int tvMinHeight = 150;
 
        //private ComboBox.ObjectCollection itm;
 
        /// <summary>
        /// General point to access MouseMove event for resizing the TreeView
        /// </summary>
        private MouseEventArgs parentMouse;
 
       // ComboboxItem item = null;
 
        //private string _Text;
 
        //        public string ComboxText
        //        {
        //            get { return _Text; }
        //            set { _Text = value; }
        //        }
        //private string _Value;
 
        //        public string ComboxValue
        //        {
        //            get { return _Value; }
        //            set { _Value = value; }
        //        }
       
        /// <summary>
        /// Get TreeNodeCollection collection, Herewith you can add and remove items to the treeview
        /// </summary>
        [
            Description("Gets the TreeView Nodes collection"),
            Category("TreeView")
        ]
        public TreeNodeCollection Nodes
        {
            get
            {
                return treeView.Nodes;
            }
        }
 
        /// <summary>
        /// Get,set property that provides the TreeView's Selected Node
        /// </summary>
        [
            Description("Gets or sets the TreeView's Selected Node"),
            Category("TreeView")
        ]
        public TreeNode SelectedNode
        {
            get
            {
                return treeView.SelectedNode;
            }
            set
            {
                treeView.SelectedNode = value;
            }
        }
 
        /// <summary>
        /// Get, set property of boolean type, this property is mapped to the ReadOnly property of the control's TextBox
        /// </summary>
        [
            Description("Gets or sets the ComboBox's Enabled state"),
            Category("ComboBox")
        ]
        public bool ComboBoxReadOnly
        {
            set
            {
                useComboxBox.Enabled = value;
            }
            get
            {
                return useComboxBox.Enabled;
            }
        }
 
        //[
        //    Description("Gets or sets the ComboBox's text state"),
        //    Category("ComboBox")
        //]
        //public ComboboxItem Items
        //{
        //    get
        //    {
        //        return object; //txtValue.Items
        //    }
        //    set
        //    {
        //        txtValue.SelectedItem = value;
        //    }
        //}
        /// <summary>
        ///
        /// </summary>
        //public List<UserCombox>
        //{
        //   List<UserCombox> collection=new List<UserCombox>;
                             
            
        //}
        //public UserCombox Items
        //{
        //    get { }
        //    set { }
        //}
 
        //[
        //    Description("Gets or sets the ComboBox's text state"),
        //    Category("ComboBox")
        //]
        public ComboBox.ObjectCollection Items
        {
           //ComboBox.ObjectCollection  items= new ComboBox.ObjectCollection(txtValue);
 
            set
            {
                for (int i = 0; i < value.Count; i++)
                {
                    useComboxBox.Items.Add(value[i]);
                }
            }
 
            get
            {
                //if (Items.Count > 0)
                //{
                //    txtValue.Items.Add(Items);
                //}
                return this.useComboxBox.Items;
            }
        }
        [
            Description("Gets the ComboxBox DataSource"),
            Category("ComboBox")
        ]
        public Object DataSource
        {        
            set
            {
                useComboxBox.DataSource = value;  //要先的关系
            }
            get
            {
                return useComboxBox.DataSource;
            }
   
        }
        [
            Description("Gets the ComboxBox DataSource DisplayMember"),
            Category("ComboBox")
        ]
        public string DisplayMember
        {
            set
            {
                useComboxBox.DisplayMember = value;
            }
            get
            {
                return useComboxBox.DisplayMember;
            }
        }
        [
            Description("Gets the ComboxBox DataSource ValueMember"),
            Category("ComboBox")
        ]
        public string ValueMember
        {
            set
            {
                useComboxBox.ValueMember = value;
            }
            get
            {
                return useComboxBox.ValueMember;
            }
        }
 
  //      public static void SetLookupBinding(ListControl toBind,
  //object dataSource, string displayMember,
  //string valueMember)
  //      {
  //          toBind.DisplayMember = displayMember;
  //          toBind.ValueMember = valueMember;
 
  //          // Only after the following line will the listbox
  //          // receive events due to binding.
  //          toBind.DataSource = dataSource;
  //      }
 
        /// <summary>
        /// Get, set property thai is mapped to the Control's treeView control's ImageList property.
        /// </summary>
        public ImageList Imagelist
        {
            get
            {
                return treeView.ImageList;
            }
            set
            {
                treeView.ImageList = value;
            }
        }
        /// <summary>
        /// Control's constuctor
        /// </summary>
        public DropDownComboxTreeView()
        {
            //item=new ComboboxItem();
            //item.Text = "";// CustomControls.CustComboboxItem.ComboxText;
            //item.Value = "";// CustomControls.CustComboboxItem.ComboxValue;
            // General
            TextValueSet = false;
 
            // Initializing Controls
            InitControls();
 
            // Setting The locations
            SetSizeAndLocation();
 
            // Adding Controls to UserControl
            this.Controls.AddRange(new Control[] { btnSelect, useComboxBox, treeView });
 
            // attaching events
            this.Resize += new EventHandler(ControlResize);
            this.LocationChanged += new EventHandler(ControlResize);
 
        }
 
        /// <summary>
        /// Control's resize handler, this handler is attached to the controls Resize and LocationChanged events
        /// </summary>
        /// <param name="sender">sender obcject</param>
        /// <param name="e">default EventArgs</param>
        private void ControlResize(object sender, EventArgs e)
        {
            // Setting The locations
            SetSizeAndLocation();
        }
 
 
        /// <summary>
        /// Controls, sub control initializer, private void that handles the control initialization.
        /// </summary>
        public void InitControls()
        {
            try
            {
                //value box settings;
                useComboxBox = new ComboBox();
                this.ComboBoxReadOnly = true;
                useComboxBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.Simple;
                //初始化的值
                //if (TextValueSet == false)
                //{
                //    ComboboxItem item = new ComboboxItem();
                //    ////txtValue.Text = this.SelectedNode.Text; //设定的值
                //    item.Text = CustComboboxItem.ComboxText;
                //    item.Value = CustComboboxItem.ComboxValue;
                //if (!object.Equals(item, null))
                //{
                //    txtValue.Items.Add(item);
                //    txtValue.SelectedIndex = 0;
                //}
 
                //}
                //txtValue.Text = "gee";
                //txtValue.Text = "";
                //txtValue.Items.Add(SelecItem);
                if (TextValueSet == false)
                {
                    useComboxBox.DataSource = DataSource;
                    useComboxBox.DisplayMember = DisplayMember;
                    useComboxBox.ValueMember = ValueMember;
                }
                else
                {
                    useComboxBox.DataSource = null;
                }
 
                useComboxBox.Size = new System.Drawing.Size(121, 18);
                //select button settings
                btnSelect = new ButtonEx();
                //btnSelect.Font = new Font("System",7);
                //btnSelect.Text = "+";
                //btnSelect.TextAlign = ContentAlignment.MiddleCenter;
                //btnSelect.ClientSize = btnSelect.Size;
                btnSelect.Click += new EventHandler(ToggleTreeView);
                //btnSelect.Size = new Size(120,txtValue.Height);
 
 
                treeView = new TreeView();
                treeView.BorderStyle = BorderStyle.FixedSingle;
                treeView.Visible = false;
                treeView.AfterSelect += new TreeViewEventHandler(TreeViewNodeSelect);
                treeView.DoubleClick += new EventHandler(TreeViewNodeDoubleClickSelect); //双击为选择
                treeView.MouseMove += new MouseEventHandler(TreeViewMouseMoveEventHandler);
                treeView.Size = new Size(tvMinWidth, tvMinHeight);
                treeLabel = new LabelEx();
                treeLabel.Size = new Size(16, 16);
                treeLabel.BackColor = Color.Transparent;
                treeView.Controls.Add(treeLabel);
                SetHotSpot();
 
                this.SetStyle(ControlStyles.DoubleBuffer, true);
                this.SetStyle(ControlStyles.ResizeRedraw, true);
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }
 
 
        /// <summary>
        /// private void for setting the HotSpot helper label.
        /// </summary>
        private void SetHotSpot()
        {
            treeLabel.Top = treeView.Height - treeLabel.Height;
            treeLabel.Left = treeView.Width - treeLabel.Width;
        }
 
        /// <summary>
        /// TreeView node select handler, this is a protected event method
        /// that is attached to the TreeView's AfterSelect event. It sets the
        /// TextBox's Text property. By default It checks the selected treenode,
        /// If the treenode dosn't have any child node then, this node's value
        /// is assigned to the TextBox's Text Property.
        /// You can implement your own AfterSelect handler by overriding this method.
        /// </summary>
        /// <param name="sender">sender object</param>
        /// <param name="e">default TreeViewEventArgs</param>
        protected void TreeViewNodeSelect(object sender, TreeViewEventArgs e)
        {
            // chiching for none root node
            if (this.SelectedNode.Nodes.Count == 0) //选择确定
            {
                SetTextValueToSelectedNode();
                TextValueSet = true;
            }
        }
 
 
        /// <summary>
        /// This event method is the control's TreeView's DoubleClick event handler.
        /// It is meant to close the TreeView when an item is DoubleClicked.
        /// This function calls the TreeViewNodeSelect event handler method, then if
        /// the TextValueSet is set to true it toggles (closes) the TreeView
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TreeViewNodeDoubleClickSelect(object sender, EventArgs e)
        {
            this.TreeViewNodeSelect(sender, null);
            if (TextValueSet)
            {
                this.ToggleTreeView(sender, null);
            }
        }
        /// <summary>
        /// private void setting the control's TextBox's Text property.
        /// This method also sets the TextValueSet to true if a value
        /// is assigned to the TextBox's Text property
        /// </summary>
        private void SetTextValueToSelectedNode()
        {
            try
            {
                //useComboxBox.Items.Clear();
                useComboxBox.DataSource = null;
                ComboboxItem item = new ComboboxItem();
                //txtValue.Text = this.SelectedNode.Text; //设定的值
                item.Text = this.SelectedNode.Text;
                item.Value = this.SelectedNode.Name;
                useComboxBox.Items.Clear();
                useComboxBox.Items.Add(item);
                useComboxBox.SelectedIndex = 0;
                TextValueSet = true;
            }
            catch(Exception ex)
            {
                ex.Message.ToString();
                TextValueSet = false;
            }
        }
 
        /// <summary>
        /// private event method, this method is attached to the control's Button's Clcick event.
        /// It handles the show/hide of the control's treeview
        /// </summary>
        /// <param name="sender">sender object</param>
        /// <param name="e">default EventArgs</param>
        private void ToggleTreeView(object sender, EventArgs e)
        {
            if (!treeView.Visible)
            {
 
                SetTreeViewSizeAndLocation();
                try
                {
                    this.btnSelect.Text = "-";
                    this.Parent.Controls.Add(treeView);
                    this.treeView.BringToFront();
                    treeView.Visible = true;
                    treeView.Select();
                    Parent.MouseMove += new MouseEventHandler(ParentMouseMoveHandler);
                    if (treeView.Width < this.Width || treeView.Height < this.Height)
                    {
                        treeView.Width = tvMinWidth;
                        treeView.Height = 150;
                        SetHotSpot();
                    }
 
                }
                catch
                {
                }
            }
            else
            {
                try
                {
                    btnSelect.Text = "+";
                    treeView.Visible = false;
                    this.Parent.Controls.Remove(treeView);
                    Parent.MouseMove -= new MouseEventHandler(ParentMouseMoveHandler);
                }
                catch
                {
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TreeViewMouseMoveEventHandler(object sender, MouseEventArgs e)
        {
            this.parentMouse = e;
            SetCursor(Cursors.Default);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ParentMouseMoveHandler(object sender, MouseEventArgs e)
        {
            int tx, ty;
            this.parentMouse = e;
 
            tx = treeView.Left + treeView.Width;
            ty = treeView.Top + treeView.Height;
 
            SetHotSpot();
 
            if (e.Button == MouseButtons.Left)
            {
                Margin = 50;
                treeLabel.BringToFront();
            }
            else
            {
                Margin = 4;
            }
 
            if ((is_in_range(e.Y, ty - Margin, ty + Margin)) && is_in_range(e.X, tx - Margin, tx + Margin))
            {
                SetCursor(Cursors.SizeNWSE);
                if (e.Button == MouseButtons.Left)
                {
                    treeView.Height = e.Y - treeView.Top;
                    treeView.Width = e.X - treeView.Left;
                }
            }
            else
            {
                SetCursor(Cursors.Default);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="crs"></param>
        private void SetCursor(Cursor crs)
        {
            treeView.Cursor = crs;
            Parent.Cursor = crs;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="rvalue"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        private bool is_in_range(int rvalue, int start, int end)
        {
            if ((rvalue >= start) && (rvalue <= end))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
 
        /// <summary>
        ///
        /// </summary>
        public void SetTreeViewMinimumSize()
        {
            if ((treeView.Width < this.Width) || (treeView.Height < 150))
            {
                treeView.Width = this.Width;
                treeView.Height = 150;
            }
        }
 
        /// <summary>
        /// private method sets the control's TreeView's size and location.
        /// It is called when the control is resized to moved from it's place
        /// </summary>
        private void SetTreeViewSizeAndLocation()
        {
            if (treeView != null)
            {
                treeView.Location = new Point(this.Left, this.Top + this.Height + 1);
            }
        }
 
        /// <summary>
        /// private method, sets the controls size and location.
        /// It sets the control's and sub control's sizes and locations
        /// </summary>
        private void SetSizeAndLocation()
        {
            tvMinWidth = this.Width;
            if (useComboxBox != null && btnSelect != null)
            {
                btnSelect.Size = new Size(useComboxBox.Height, 18);
                useComboxBox.Width = this.Width - btnSelect.Width - 4;
                useComboxBox.Location = new Point(0, 0);
                btnSelect.Left = useComboxBox.Width + 4;
                SetTreeViewSizeAndLocation();
                this.Height = useComboxBox.Height;
            }
        }
    }
 
 
    /// <summary>
    /// Extended Label control with user paint.
    /// </summary>
    public class LabelEx : Label
    {
        /// <summary>
        ///
        /// </summary>
        public LabelEx()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        }
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            System.Windows.Forms.ControlPaint.DrawSizeGrip(e.Graphics, Color.Black, 0, 0, 16, 16);
        }
    }
 
 
    /// <summary>
    ///
    /// </summary>
    public class ComboboxItem
    {
 
 
        string _Text;
        public string Text {
            get { return _Text; }
            set { _Text = value; }
        }
        object _Value;
        public object Value {
            get { return _Value; }
            set { _Value = value; }
         }
 
        public override string ToString()
        {
            return Text;
        }
    }
    /// <summary>
    ///
    /// </summary>
    public  class CustComboboxItem
    {
        public static string ComboxText;
        public static string ComboxValue;
    }
    //private string _Text;
 
    //        public string ComboxText
    //        {
    //            get { return _Text; }
    //            set { _Text = value; }
    //        }
    //private string _Value;
 
    //        public string ComboxValue
    //        {
    //            get { return _Value; }
    //            set { _Value = value; }
    //        }
    /// <summary>
    /// Extended button control with userpaint
    /// </summary>
    public class ButtonEx : Button
    {
        ButtonState state;
 
        /// <summary>
        ///
        /// </summary>
        public ButtonEx()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        }
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            state = ButtonState.Pushed;
            base.OnMouseDown(e);
        }
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseUp(MouseEventArgs e)
        {
            state = ButtonState.Normal;
            base.OnMouseUp(e);
        }
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            System.Windows.Forms.ControlPaint.DrawComboButton(e.Graphics, 0, 0, this.Width, this.Height, state);
        }
    }
}

 

posted @   ®Geovin Du Dream Park™  阅读(580)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示