Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / Designer / WebForms / System / Web / UI / Design / Util / AutoSizeComboBox.cs / 1 / AutoSizeComboBox.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Web.UI.Design.Util { using System; using System.Design; using System.Drawing; using System.Windows.Forms; using System.Windows.Forms.Design; ////// Helper class to automatically resize the dropdown part of a ComboBox /// to fit the widest item. /// Note that if you modify the Items collection of the control, you will /// have to call InvalidateDropDownWidth so that it will be auto-resized /// the next time the combobox is dropped down. /// internal sealed class AutoSizeComboBox : ComboBox { private const int MaxDropDownWidth = 600; private bool _dropDownWidthValid; private void AutoSizeComboBoxDropDown() { int maxWidth = 0; using (Graphics g = Graphics.FromImage(new Bitmap(1, 1))) { foreach (object o in Items) { if (o != null) { Size size = g.MeasureString(o.ToString(), Font, 0, new StringFormat(StringFormatFlags.NoWrap | StringFormatFlags.FitBlackBox)).ToSize(); maxWidth = Math.Max(maxWidth, size.Width); if (maxWidth >= MaxDropDownWidth) { maxWidth = MaxDropDownWidth; break; } } } } int newWidth = maxWidth + SystemInformation.VerticalScrollBarWidth + 2 * SystemInformation.BorderSize.Width; // This is a cheap hack to workaround the fact that the WinForms ComboBox // doesn't send a CB_SETDROPPEDWIDTH message to the control unless the value // has changed. We have to always send the message since the effective value // of the drop down width may have changed due to the width of the combobox // itself changing, and we want the new value. DropDownWidth = newWidth + 1; DropDownWidth = newWidth; } public void InvalidateDropDownWidth() { _dropDownWidthValid = false; } protected override void OnDropDown(EventArgs e) { if (!_dropDownWidthValid) { AutoSizeComboBoxDropDown(); _dropDownWidthValid = true; } base.OnDropDown(e); } protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); _dropDownWidthValid = false; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- WindowsScroll.cs
- WinEventQueueItem.cs
- BrushConverter.cs
- ColorKeyFrameCollection.cs
- IntranetCredentialPolicy.cs
- StatusBarItemAutomationPeer.cs
- ConfigurationElementCollection.cs
- WpfSharedXamlSchemaContext.cs
- CachedResourceDictionaryExtension.cs
- PropertyInfoSet.cs
- Validator.cs
- DesignTimeParseData.cs
- UniqueIdentifierService.cs
- CodeVariableReferenceExpression.cs
- TypeConverterHelper.cs
- Vector3DCollectionValueSerializer.cs
- FutureFactory.cs
- List.cs
- XPathExpr.cs
- Itemizer.cs
- SharedConnectionInfo.cs
- StylusLogic.cs
- FileDialogCustomPlacesCollection.cs
- RelationshipSet.cs
- DesignerVerb.cs
- NativeMethods.cs
- SafeBitVector32.cs
- RightsManagementEncryptionTransform.cs
- CharEntityEncoderFallback.cs
- SendKeys.cs
- GatewayDefinition.cs
- ParagraphResult.cs
- CompositeDesignerAccessibleObject.cs
- GroupBox.cs
- TransportChannelFactory.cs
- XmlSchemaAnnotated.cs
- Delay.cs
- TextAutomationPeer.cs
- SystemKeyConverter.cs
- DataGrid.cs
- BrowserCapabilitiesCompiler.cs
- XmlSchemaAttributeGroup.cs
- SolidColorBrush.cs
- DocumentXPathNavigator.cs
- HttpMethodAttribute.cs
- SmtpDigestAuthenticationModule.cs
- CornerRadiusConverter.cs
- EventLogPermissionEntry.cs
- CatalogZoneBase.cs
- ProfileSettings.cs
- RegexParser.cs
- NoneExcludedImageIndexConverter.cs
- ZipIOBlockManager.cs
- CompiledXpathExpr.cs
- DelimitedListTraceListener.cs
- InkPresenter.cs
- BamlRecordReader.cs
- CompilerGlobalScopeAttribute.cs
- LicenseManager.cs
- RoleManagerModule.cs
- StaticSiteMapProvider.cs
- NodeFunctions.cs
- EffectiveValueEntry.cs
- TabRenderer.cs
- NullableIntSumAggregationOperator.cs
- ImplicitInputBrush.cs
- AccessDataSourceWizardForm.cs
- AbandonedMutexException.cs
- WindowsTab.cs
- ResourcePermissionBase.cs
- HtmlInputReset.cs
- DispatchWrapper.cs
- XhtmlBasicValidationSummaryAdapter.cs
- TextTreePropertyUndoUnit.cs
- OrCondition.cs
- DbDeleteCommandTree.cs
- UnsafeMethods.cs
- ParseElement.cs
- processwaithandle.cs
- SchemaImporter.cs
- RayMeshGeometry3DHitTestResult.cs
- WebPartPersonalization.cs
- TargetControlTypeCache.cs
- TagNameToTypeMapper.cs
- LogRestartAreaEnumerator.cs
- ToolBarButtonDesigner.cs
- ServiceMetadataExtension.cs
- IntSecurity.cs
- URIFormatException.cs
- DefaultTextStoreTextComposition.cs
- ToolStripSplitButton.cs
- PostBackTrigger.cs
- JapaneseCalendar.cs
- DataGridViewRowCancelEventArgs.cs
- BoolExpression.cs
- designeractionlistschangedeventargs.cs
- CapiNative.cs
- BaseInfoTable.cs
- FlowDocumentPage.cs
- ObjectIDGenerator.cs