private string NameFromText(string text, System.Type itemType, INameCreationService nameCreationService, bool adjustCapitalization) { string name = null; if (text == "-") { name = "toolStripSeparator"; } else { string str2 = itemType.Name; StringBuilder builder = new StringBuilder(text.Length + str2.Length); bool flag = false; for (int j = 0; j < text.Length; j++) { char c = text[j]; if (char.IsLetterOrDigit(c)) { if (!flag) { c = char.ToLower(c, CultureInfo.CurrentCulture); flag = true; } builder.Append(c); } } builder.Append(str2); name = builder.ToString(); if (adjustCapitalization) { string str3 = ToolStripDesigner.NameFromText(null, typeof(ToolStripMenuItem), this._designer.Component.Site); if (!string.IsNullOrEmpty(str3) && char.IsUpper(str3[0])) { name = char.ToUpper(name[0], CultureInfo.InvariantCulture) + name.Substring(1); } } } if (this._host.Container.Components[name] == null) { if (!nameCreationService.IsValidName(name)) { return(nameCreationService.CreateName(this._host.Container, itemType)); } return(name); } string str4 = name; for (int i = 1; !nameCreationService.IsValidName(str4); i++) { str4 = name + i.ToString(CultureInfo.InvariantCulture); } return(str4); }
/// <summary> /// Computes a name from a text label by removing all spaces and non-alphanumeric characters. /// </summary> private string NameFromText(string text, Type itemType, INameCreationService nameCreationService, bool adjustCapitalization) { string baseName; // for separators, name them ToolStripSeparator... if (text == "-") { baseName = "toolStripSeparator"; } else { string nameSuffix = itemType.Name; // remove all the non letter and number characters. Append length of "MenuItem" Text.StringBuilder name = new Text.StringBuilder(text.Length + nameSuffix.Length); bool firstCharSeen = false; for (int i = 0; i < text.Length; i++) { char c = text[i]; if (char.IsLetterOrDigit(c)) { if (!firstCharSeen) { c = char.ToLower(c, CultureInfo.CurrentCulture); firstCharSeen = true; } name.Append(c); } } name.Append(nameSuffix); baseName = name.ToString(); if (adjustCapitalization) { string nameOfRandomItem = ToolStripDesigner.NameFromText(null, typeof(ToolStripMenuItem), _designer.Component.Site); if (!string.IsNullOrEmpty(nameOfRandomItem) && char.IsUpper(nameOfRandomItem[0])) { baseName = char.ToUpper(baseName[0], CultureInfo.InvariantCulture) + baseName.Substring(1); } } } // see if this name matches another one in the container.. object existingComponent = _host.Container.Components[baseName]; if (existingComponent is null) { if (!nameCreationService.IsValidName(baseName)) { // we don't have a name collision but this still isn't a valid name...something is wrong and we can't make a valid identifier out of this so bail. return(nameCreationService.CreateName(_host.Container, itemType)); } else { return(baseName); } } else { // start appending numbers. string newName = baseName; for (int indexer = 1; !nameCreationService.IsValidName(newName); indexer++) { newName = baseName + indexer.ToString(CultureInfo.InvariantCulture); } return(newName); } }
private void RefreshColumnCollection() { DataGridView component = (DataGridView)base.Component; ISupportInitializeNotification dataSource = component.DataSource as ISupportInitializeNotification; if ((dataSource == null) || dataSource.IsInitialized) { IComponentChangeService service = null; PropertyDescriptor member = null; IDesignerHost provider = base.Component.Site.GetService(typeof(IDesignerHost)) as IDesignerHost; if (!this.ProcessSimilarSchema(component)) { PropertyDescriptorCollection itemProperties = null; if (this.cm != null) { try { itemProperties = this.cm.GetItemProperties(); } catch (ArgumentException exception) { throw new InvalidOperationException(System.Design.SR.GetString("DataGridViewDataSourceNoLongerValid"), exception); } } IContainer container = (component.Site != null) ? component.Site.Container : null; service = base.Component.Site.GetService(typeof(IComponentChangeService)) as IComponentChangeService; member = TypeDescriptor.GetProperties(base.Component)["Columns"]; service.OnComponentChanging(base.Component, member); DataGridViewColumn[] columnArray = new DataGridViewColumn[component.Columns.Count]; int index = 0; for (int i = 0; i < component.Columns.Count; i++) { DataGridViewColumn column = component.Columns[i]; if (!string.IsNullOrEmpty(column.DataPropertyName)) { PropertyDescriptor descriptor2 = TypeDescriptor.GetProperties(column)["UserAddedColumn"]; if ((descriptor2 == null) || !((bool)descriptor2.GetValue(column))) { columnArray[index] = column; index++; } } } for (int j = 0; j < index; j++) { component.Columns.Remove(columnArray[j]); } service.OnComponentChanged(base.Component, member, null, null); if (container != null) { for (int m = 0; m < index; m++) { container.Remove(columnArray[m]); } } DataGridViewColumn[] columnArray2 = null; int num5 = 0; if (component.DataSource != null) { columnArray2 = new DataGridViewColumn[itemProperties.Count]; num5 = 0; for (int n = 0; n < itemProperties.Count; n++) { System.Type typeofDataGridViewImageColumn; DataGridViewColumn column2 = null; TypeConverter converter = TypeDescriptor.GetConverter(typeof(Image)); System.Type propertyType = itemProperties[n].PropertyType; if (typeof(IList).IsAssignableFrom(propertyType)) { if (!converter.CanConvertFrom(propertyType)) { continue; } typeofDataGridViewImageColumn = DataGridViewDesigner.typeofDataGridViewImageColumn; } else if ((propertyType == typeof(bool)) || (propertyType == typeof(CheckState))) { typeofDataGridViewImageColumn = typeofDataGridViewCheckBoxColumn; } else if (typeof(Image).IsAssignableFrom(propertyType) || converter.CanConvertFrom(propertyType)) { typeofDataGridViewImageColumn = DataGridViewDesigner.typeofDataGridViewImageColumn; } else { typeofDataGridViewImageColumn = typeofDataGridViewTextBoxColumn; } string name = ToolStripDesigner.NameFromText(itemProperties[n].Name, typeofDataGridViewImageColumn, base.Component.Site); column2 = TypeDescriptor.CreateInstance(provider, typeofDataGridViewImageColumn, null, null) as DataGridViewColumn; column2.DataPropertyName = itemProperties[n].Name; column2.HeaderText = !string.IsNullOrEmpty(itemProperties[n].DisplayName) ? itemProperties[n].DisplayName : itemProperties[n].Name; column2.Name = itemProperties[n].Name; column2.ValueType = itemProperties[n].PropertyType; column2.ReadOnly = itemProperties[n].IsReadOnly; provider.Container.Add(column2, name); columnArray2[num5] = column2; num5++; } } service.OnComponentChanging(base.Component, member); for (int k = 0; k < num5; k++) { columnArray2[k].DisplayIndex = -1; component.Columns.Add(columnArray2[k]); } service.OnComponentChanged(base.Component, member, null, null); } } }