Hi Leute.
Ich arbeite an einem Ribbon Control. Ich Arbeite Gerade an dem ApplicationMenu. Ich habe ein ContainerControl welches mehrere LineRibbonControls beinhalten kann. (Items :LineRibbonControlCollection Eigenschaft).
Nun zu meinem Problem: Beim Designer meines ContainerControls lasse ich über ein Verb ein Component anlegen und dem Container hinzufügen. Jedoch wird im InitializeComponent das Component nicht der Liste hinzugefügt. Wisst ihr wo das Problem liegt? Hier der Code:
Code csharp:
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
    public class LineRibbonMainMenuControlContainerDesigner : ControlDesigner
    {
        private LineRibbonMainMenuControlContainer _oOwner;
 
        /// <summary>
        /// Ruft die Auflistung der Komponenten ab, die der durch den Designer verwalteten Komponente zugeordnet ist.
        /// </summary>
        /// <value></value>
        /// <returns>Die Komponenten, die der durch den Designer verwalteten Komponente zugeordnet sind.</returns>
        public override System.Collections.ICollection AssociatedComponents
        {
            get
            {
                return (ICollection)_oOwner.Items;
            }
        }
 
        /// <summary>
        /// Ruft die Auswahlregeln ab, die die Bewegungsfunktionen einer Komponente angeben. 
        /// </summary>
        public override SelectionRules SelectionRules
        {
            get
            {
                return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.AllSizeable;
            }
        }
 
        public override DesignerVerbCollection Verbs
        {
            get
            {
                return new DesignerVerbCollection(new DesignerVerb[] { new DesignerVerb("Add Button", AddButtonHandler), });
            }
        }
 
        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            _oOwner = component as LineRibbonMainMenuControlContainer;
            if (_oOwner == null)
            {
                throw new Exception("The LineRibbonMainMenuControlContainerDesigner can only Design a LineRibbonMainMenuControlContainer. Type was: " + Control.GetType().FullName);
            }
            ISelectionService s = (ISelectionService)GetService(
        typeof(ISelectionService));
            IComponentChangeService c = (IComponentChangeService)
                GetService(typeof(IComponentChangeService));
            s.SelectionChanged += OnSelectionChanged;
            c.ComponentRemoving += OnComponentRemoving;
        }
 
        private void OnSelectionChanged(object sender, System.EventArgs e)
        {
        }
        private void OnComponentRemoving(object sender, ComponentEventArgs e)
        {
            IComponentChangeService c = (IComponentChangeService)
    GetService(typeof(IComponentChangeService));
            LineRibbonControl button;
            IDesignerHost h = (IDesignerHost)GetService(typeof(IDesignerHost));
            int i;
            // If the user is removing a button
            if (e.Component is LineRibbonControl)
            {
                button = (LineRibbonControl)e.Component;
                if (_oOwner.Items.Contains(button))
                {
                    c.OnComponentChanging(_oOwner, null);
                    _oOwner.Items.Remove(button);
                    c.OnComponentChanged(_oOwner, null, null, null);
                    return;
                }
            }
            // If the user is removing the control itself
            if (e.Component == _oOwner)
            {
                for (i = _oOwner.Items.Count - 1; i >= 0; i--)
                {
                    button = _oOwner.Items[i];
                    c.OnComponentChanging(_oOwner, null);
                    _oOwner.Items.Remove(button);
                    h.DestroyComponent(button);
                    c.OnComponentChanged(_oOwner, null, null, null);
                }
            }
        }
        protected override void Dispose(bool disposing)
        {
            ISelectionService s = (ISelectionService)GetService(
            typeof(ISelectionService));
            IComponentChangeService c = (IComponentChangeService)
                GetService(typeof(IComponentChangeService));
            // Unhook events
            s.SelectionChanged -= OnSelectionChanged;
            c.ComponentRemoving -= OnComponentRemoving;
            base.Dispose(disposing);
        }
 
        private void AddButtonHandler(object sender, EventArgs e)
        {
            LineRibbonButton oButton;
            IDesignerHost h = (IDesignerHost)GetService(typeof(IDesignerHost));
            DesignerTransaction dt;
            IComponentChangeService c = (IComponentChangeService)
            GetService(typeof(IComponentChangeService));
            // Add a new button to the collection
            dt = h.CreateTransaction("Add Button");
            oButton = (LineRibbonButton)h.CreateComponent(typeof(LineRibbonButton));
            c.OnComponentChanging(_oOwner, null);
            _oOwner.Items.Add(oButton);
            c.OnComponentChanged(_oOwner, null, null, null);
            dt.Commit();
        }
 
        protected override bool GetHitTest(System.Drawing.Point point)
        {
            point = _oOwner.PointToClient(point);
            foreach (LineRibbonControl oControl in _oOwner.Items)
            {
                if (oControl.Bounds.Contains(point))
                    return true;
            }
            return false;
        }
    }