This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyNavBarControl.cs
133 lines (118 loc) · 5.05 KB
/
MyNavBarControl.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraNavBar;
using DevExpress.XtraBars.Docking;
namespace NavBarExample {
public partial class MyNavBarControl : DevExpress.XtraEditors.XtraUserControl {
const int _margin = 3;
ImageList images;
int imageIndexer = 0;
public MyNavBarControl() {
InitializeComponent();
this.ControlAdded += new ControlEventHandler(MyNavBarControl_ControlAdded);
this.ControlRemoved += new ControlEventHandler(MyNavBarControl_ControlRemoved);
this.Resize += new EventHandler(MyNavBarControl_Resize);
images = new ImageList();
images.Images.Add(Properties.Resources._1);
images.Images.Add(Properties.Resources._2);
images.Images.Add(Properties.Resources._3);
images.Images.Add(Properties.Resources._4);
images.Images.Add(Properties.Resources._5);
images.Images.Add(Properties.Resources._6);
images.Images.Add(Properties.Resources._7);
images.Images.Add(Properties.Resources._8);
images.Images.Add(Properties.Resources._9);
images.Images.Add(Properties.Resources._10);
}
void MyNavBarControl_Resize(object sender, EventArgs e) {
foreach(NavBarControl item in Controls) {
item.Height = this.Height - (_margin * 2);
}
}
void MyNavBarControl_ControlRemoved(object sender, ControlEventArgs e) {
if(Controls.Count == 0) return;
CalculateBestFit();
}
void MyNavBarControl_ControlAdded(object sender, ControlEventArgs e) {
CalculateBestFit();
}
public void CalculateBestFit() {
List<Point> locations = new List<Point>();
int shift = _margin;
for(int i = 0; i < Controls.Count; i++) {
Point newLocation = new Point();
newLocation.X = (i * _margin) + shift;
shift += Controls[i].Width;
newLocation.Y = _margin;
locations.Add(newLocation);
}
for(int i = 0; i < Controls.Count; i++) {
Controls[i].Location = locations[i];
}
this.Size = new Size((Controls.Count * _margin) + shift, Controls[0].Height + _margin * 2);
OnResize(new EventArgs());
}
public void AddBar(NavBarControl navBar) {
navBar.SmallImages = images;
this.Controls.Add(navBar);
}
public void DeleteBar(int index) {
if(Controls.Count < index) return;
Controls.RemoveAt(index);
}
public void AddGroup(int barIndex, string groupName) {
if(!IsCorrectIndex(barIndex)) return;
NavBarGroup group = new NavBarGroup(groupName);
group.Name = groupName;
(Controls[barIndex] as NavBarControl).Groups.Add(group);
}
public void DeleteGroup(int barIndex, string groupName) {
if(!IsCorrectIndex(barIndex)) return;
NavBarControl bar = GetNavBarByIndex(barIndex);
NavBarGroup group = GetNavBarGroupByName(bar, groupName);
if(group != null) (Controls[barIndex] as NavBarControl).Groups.Remove(group);
}
public void ArrGroupItem(int barIndex, string groupName, string itemName) {
if(!IsCorrectIndex(barIndex)) return;
NavBarControl bar = GetNavBarByIndex(barIndex);
NavBarGroup group = GetNavBarGroupByName(bar, groupName);
if(group == null) return;
if(!group.Expanded) group.Expanded = true;
NavBarItem item = new NavBarItem(itemName);
item.SmallImageIndex = (imageIndexer++) % (images.Images.Count - 1);
group.ItemLinks.Add(item);
}
public void DeleteGroupItem(int barIndex, string groupName, string itemName) {
if(!IsCorrectIndex(barIndex)) return;
NavBarControl bar = GetNavBarByIndex(barIndex);
NavBarGroup group = GetNavBarGroupByName(bar, groupName);
if(group == null) return;
NavBarItemLink item = GetItemLinkByName(group, itemName);
if(item == null) return;
group.ItemLinks.Remove(item);
}
NavBarItemLink GetItemLinkByName(NavBarGroup group, string itemName) {
foreach(NavBarItemLink item in group.ItemLinks) {
if(item.Caption == itemName) {
return item;
}
}
return null;
}
bool IsCorrectIndex(int index) {
return index > -1 && index < Controls.Count;
}
NavBarGroup GetNavBarGroupByName(NavBarControl bar, string groupName) {
return bar.Groups[groupName];
}
NavBarControl GetNavBarByIndex(int index) {
return Controls[index] as NavBarControl;
}
}
}