added samples
[windows-sources.git] / sdk / samples / CrossTechnologySamples / VistaBridge / vistabridgelibrary / dialogs / dialogcontrolcollection.cs
blob29558fa0836212bd14dd10814557553b9a554ba7
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Collections.ObjectModel;
5 using System.ComponentModel;
6 using System.Text;
7 using Microsoft.SDK.Samples.VistaBridge.Library;
9 namespace Microsoft.SDK.Samples.VistaBridge.Library
11 /// <summary>
12 /// Strongly typed collection for dialog controls.
13 /// </summary>
14 /// <typeparam name="T">DialogControl</typeparam>
15 public sealed class DialogControlCollection<T> : Collection<T> where T : DialogControl
17 private IDialogControlHost hostingDialog;
19 internal DialogControlCollection(IDialogControlHost host)
21 hostingDialog = host;
24 /// <summary>
25 /// Inserts an dialog control at the specified index.
26 /// </summary>
27 /// <param name="index">The location to insert the control.</param>
28 /// <param name="control">The item to insert.</param>
29 /// <permission cref="System.InvalidOperationException">A control with
30 /// the same name already exists in this collection -or-
31 /// the control is being hosted by another dialog -or- the associated dialog is
32 /// showing and cannot be modified.</permission>
33 protected override void InsertItem(int index, T control)
35 // Check for duplicates, lack of host,
36 // and during-show adds.
37 if (Items.Contains(control))
38 throw new InvalidOperationException(
39 "Dialog cannot have more than one control with the same name.");
40 if (control.HostingDialog != null)
41 throw new InvalidOperationException(
42 "Dialog control must be removed from current collections first.");
43 if (!hostingDialog.IsCollectionChangeAllowed())
44 throw new InvalidOperationException(
45 "Modifying controls collection while dialog is showing is not supported.");
47 // Reparent, add control.
48 control.HostingDialog = hostingDialog;
49 base.InsertItem(index, control);
51 // Notify that we've added a control.
52 hostingDialog.ApplyCollectionChanged();
54 /// <summary>
55 /// Removes the control at the specified index.
56 /// </summary>
57 /// <param name="index">The location of the control to remove.</param>
58 /// <permission cref="System.InvalidOperationException">
59 /// The associated dialog is
60 /// showing and cannot be modified.</permission>
61 protected override void RemoveItem(int index)
63 // Notify that we're about to remove a control.
64 // Throw if dialog showing.
65 if (!hostingDialog.IsCollectionChangeAllowed())
66 throw new InvalidOperationException(
67 "Modifying controls collection while dialog is showing is not supported.");
69 DialogControl control = (DialogControl)Items[index];
71 // Unparent and remove.
72 control.HostingDialog = null;
73 base.RemoveItem(index);
75 hostingDialog.ApplyCollectionChanged();
77 /// <summary>
78 /// Defines the indexer that supports accessing controls by name.
79 /// </summary>
80 /// <remarks>
81 /// <para>Control names are case sensitive.</para>
82 /// <para>This indexer is useful when the dialog is created in XAML
83 /// rather than constructed in code.</para></remarks>
84 ///<exception cref="System.ArgumentException">
85 /// The name cannot be null or a zero-length string.</exception>
86 public T this[string name]
88 get
90 if (String.IsNullOrEmpty(name))
91 throw new ArgumentException(
92 "Control name must not be null or zero length.");
94 foreach (T control in base.Items)
96 // NOTE: we don't ToLower() the strings - casing effects
97 // hash codes, so we are case-sensitive.
98 if (control.Name == name)
99 return control;
101 return null;
105 internal DialogControl GetControlbyId(int id)
107 foreach (DialogControl control in Items)
109 // Match?
110 if (control.Id == id)
111 return control;
114 // Control id not found - likely an error, but the calling function
115 // should ultimately decide.
116 return null;