Code:
/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / TrustUi / MS / Internal / documents / Application / PageTextBox.cs / 1 / PageTextBox.cs
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
//
// Description: PageTextBox is a derived TextBox with some extra
// functionality for the Page related behaviours of
// DocumentApplicationUI.
//
// History:
// 09/22/2005 - [....] - Created.
//
//---------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input; // For event args
using System.Windows.TrustUI; // For string resources
///
/// A derived TextBox with some extra functionality for the Page related behaviours of DocumentApplicationUI.
///
namespace MS.Internal.Documents.Application
{
internal sealed class PageTextBox : TextBox
{
//-----------------------------------------------------
//
// Constructors
//
//-----------------------------------------------------
#region Constructors
///
/// Default PageTextBox constructor.
///
internal PageTextBox()
{
// Set any TextBox properties.
SetDefaults();
// Setup any TextBox event handlers.
SetHandlers();
}
#endregion Constructors
//------------------------------------------------------
//
// Public Properties
//
//-----------------------------------------------------
#region Public Properties
public static readonly DependencyProperty PageNumberProperty =
DependencyProperty.Register(
"PageNumber",
typeof(int),
typeof(PageTextBox),
new FrameworkPropertyMetadata(
_pageNumberDefault, //default value
FrameworkPropertyMetadataOptions.None, //MetaData flags
new PropertyChangedCallback(OnPageNumberChanged))); //changed callback
///
/// Sets the page number to be displayed in the PageTextBox.
///
public void SetPageNumber(int pageNumber)
{
if (CultureInfo.CurrentCulture != null)
{
this.Text = pageNumber.ToString(CultureInfo.CurrentCulture);
// If this is currently focused, refocus to reset text selection.
if (IsFocused)
{
SelectAll();
}
}
}
#endregion Public Properties
//------------------------------------------------------
//
// Internal Events
//
//------------------------------------------------------
#region Internal Events
///
/// This event will be fired anytime a user was editing the TextBox but applied
/// the new value (ie pressed enter, or tab).
///
internal event EventHandler PageNumberEdited;
///
/// This will fire a PageNumberEdited event if required.
///
internal void OnPageNumberEdited()
{
// Check if the TextBox is being edited
if (_isEditingText)
{
_isEditingText = false;
// Since the TextBox was being edited, fire a cancelled event so that the value
// may be applied (if desired by the UI).
PageNumberEdited(this, EventArgs.Empty);
}
}
///
/// This event will be fired anytime a user was editing the TextBox but chose not to apply
/// the new value (ie change of focus, or escape pressed).
///
internal event EventHandler PageNumberEditCancelled;
///
/// This will fire a PageNumberEditCancelled event if required.
///
internal void OnPageNumberEditCancelled()
{
// Check if the TextBox is being edited
if (_isEditingText)
{
_isEditingText = false;
// Since the TextBox was being edited, fire a cancelled event so that the value
// may be reset (if desired by the UI).
PageNumberEditCancelled(this, EventArgs.Empty);
}
}
#endregion Internal Events
//-----------------------------------------------------
//
// Protected Methods
//
//------------------------------------------------------
///
/// Creates AutomationPeer ( )
///
protected override AutomationPeer OnCreateAutomationPeer()
{
return new PageTextBoxAutomationPeer(this);
}
///
/// Highlight the entire text when the user gives focus to the PageTextBox by
/// clicking on it. In this case we must not pass the event to the base handler
/// since the base handler will place the cursor at the current position after
/// setting focus. If we are already focused, then placing the cursor is the
/// correct behavior so the base handler will be used.
///
/// A reference to the sender of the event (not used)
/// Arguments to the event (not used)
protected override void OnMouseDown(MouseButtonEventArgs e)
{
if (!IsFocused)
{
Focus();
e.Handled = true;
}
else
{
base.OnMouseDown(e);
}
}
//-----------------------------------------------------
//
// Private Methods
//
//-----------------------------------------------------
#region Private Methods
///
/// Select the text when we receive focus.
///
/// A reference to the sender of the event (not used)
/// Arguments to the event (not used)
private void OnGotFocus(object sender, EventArgs e)
{
SelectAll();
}
///
/// This will check incoming key presses for 'enter', 'escape', and 'tab' and take the
/// appropriate action.
///
/// A reference to the sender of the event.
/// Arguments to the event, used for the key reference.
private void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
// This will check for the use of 'enter', 'escape' and 'tab' and take the appropriate action
// Ensure the arguments are not null.
if (e != null)
{
// Check which Key was pressed.
switch (e.Key)
{
// Erasure keys -- these don't trigger OnPreviewTextInput but should
// set _isEditingText nonetheless
case Key.Delete:
case Key.Back:
_isEditingText = true;
break;
// Submission Keys
case Key.Return: // This also covers: case Key.Enter
case Key.Tab:
case Key.Execute:
// Enter pressed, issue submit, mark input as handled.
OnPageNumberEdited();
break;
// Rejection Keys
case Key.Cancel:
case Key.Escape:
// Escape pressed, issue cancel, mark input as handled.
OnPageNumberEditCancelled();
break;
}
}
}
///
/// This will check the characters that have been entered into the TextBox, and restrict it
/// to only the valid set.
///
/// A reference to the sender of the event.
/// Arguments to the event, used for the input text.
private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
if ((e != null) && (!String.IsNullOrEmpty(e.Text)) && (IsValidInputChar(e.Text[0])))
{
// Set editing mode and allow the ComboBox to handle it.
_isEditingText = true;
}
else
{
// Do not allow any remaining characters for text input.
e.Handled = true;
}
}
///
/// This will validate incoming strings pasted into the textbox, only pasting
/// valid (all digit) strings.
///
/// A reference to the sender of the event.
/// Arguments to the event, used to determine the input.
private void OnPaste(object sender, DataObjectPastingEventArgs e)
{
// Validate the parameters, return if data is null.
if ((e == null) || (e.DataObject == null) || (String.IsNullOrEmpty(e.FormatToApply)))
{
return;
}
// Acquire a reference to the new string.
string incomingString = e.DataObject.GetData(e.FormatToApply) as string;
if (IsValidInputString(incomingString))
{
// Since the new content is valid set the PageTextBox as in edit mode
// and allow the text to be processed normally (ie don't CancelCommand).
_isEditingText = true;
}
else
{
// Cancel the paste if the string is null, empty, or otherwise invalid.
e.Handled = true;
e.CancelCommand();
}
}
///
/// Checks if the given string contains valid characters for this control. Used for pasting
/// and UI Automation.
///
/// The string to test.
private bool IsValidInputString(string incomingString)
{
if (String.IsNullOrEmpty(incomingString))
{
return false;
}
// Check that each character is valid (a digit).
foreach (char c in incomingString.ToCharArray())
{
if (!(IsValidInputChar(c)))
{
return false;
}
}
return true;
}
///
/// Returns true if the character is valid for input to this control.
///
/// The character to test.
private bool IsValidInputChar(char c)
{
// This will limit which characters are allowed to be entered into the TextBox
// Currently this is limited to 0-9
// This method (by using IsDigit) is localization independent, since (as localization
// informed me) this avoids worrying about the mapping of the keys, and instead the
// actual value that will be input.
return (Char.IsDigit(c));
}
///
/// Callback for the PageNumber DependencyProperty. When this event is fired
/// PageTextBox updates the page number it is displaying.
///
/// The PageTextBox to update.
/// The associated arguments.
private static void OnPageNumberChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
PageTextBox PageTextBox = (PageTextBox)d;
PageTextBox.SetPageNumber((int)e.NewValue);
}
///
/// Set the default ComboBox properties
///
private void SetDefaults()
{
ToolTip = SR.Get(SRID.PageTextBoxToolTip);
IsReadOnly = false;
VerticalAlignment = VerticalAlignment.Center;
HorizontalAlignment = HorizontalAlignment.Center;
VerticalContentAlignment = VerticalAlignment.Center;
HorizontalContentAlignment = HorizontalAlignment.Center;
TextAlignment = TextAlignment.Right;
Padding = new Thickness(1);
Margin = new Thickness(0);
// Since PageTextBox only supports input as numbers, we should disable
// IME options so that we don't need multiple Enter presses to parse
// the input.
InputMethod.SetIsInputMethodEnabled(this, false);
}
///
/// Attach any needed TextBox event handlers.
///
private void SetHandlers()
{
GotFocus += OnGotFocus;
PreviewTextInput += OnPreviewTextInput;
PreviewKeyDown += OnPreviewKeyDown;
DataObject.AddPastingHandler(this, new DataObjectPastingEventHandler(OnPaste));
}
#endregion Private Methods
#region Nested Classes
///
/// AutomationPeer associated with PageTextBox
///
private class PageTextBoxAutomationPeer : TextBoxAutomationPeer, IValueProvider
{
///
/// Constructor
///
/// Owner of the AutomationPeer.
public PageTextBoxAutomationPeer(PageTextBox owner)
: base(owner)
{ }
///
///
///
override protected string GetClassNameCore()
{
return "PageTextBox";
}
///
///
///
override public object GetPattern(PatternInterface patternInterface)
{
if (patternInterface == PatternInterface.Value)
{
return this;
}
else
{
return base.GetPattern(patternInterface);
}
}
void IValueProvider.SetValue(string value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
if (!IsEnabled())
{
throw new ElementNotEnabledException();
}
PageTextBox owner = (PageTextBox)Owner;
if (owner.IsReadOnly)
{
throw new ElementNotEnabledException();
}
if (owner.IsValidInputString(value))
{
owner.Text = value;
owner._isEditingText = true;
owner.OnPageNumberEdited();
}
}
}
#endregion
//-----------------------------------------------------
//
// Private Fields
//
//------------------------------------------------------
private bool _isEditingText;
private const int _pageNumberDefault = 0;
}
}
// 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
- VoiceInfo.cs
- DataGridViewColumnCollectionDialog.cs
- BindingBase.cs
- ViewStateAttachedPropertyFeature.cs
- RectangleGeometry.cs
- CodeDefaultValueExpression.cs
- HttpPostedFile.cs
- DesignTimeResourceProviderFactoryAttribute.cs
- SystemException.cs
- GridViewSelectEventArgs.cs
- DataBoundLiteralControl.cs
- FilterQuery.cs
- ReferentialConstraint.cs
- MetadataItemEmitter.cs
- HyperLinkStyle.cs
- SeverityFilter.cs
- altserialization.cs
- SqlDataSourceParameterParser.cs
- TimeoutValidationAttribute.cs
- XmlAggregates.cs
- Completion.cs
- PropertyNames.cs
- SByteConverter.cs
- Timeline.cs
- DateRangeEvent.cs
- SmtpReplyReader.cs
- MouseGestureValueSerializer.cs
- ResetableIterator.cs
- SmiConnection.cs
- ConcurrentBag.cs
- RepeaterItemCollection.cs
- DataServiceContext.cs
- XmlWriter.cs
- DateTime.cs
- DependencyPropertyKey.cs
- MemoryFailPoint.cs
- DiagnosticTrace.cs
- PlainXmlWriter.cs
- EndpointIdentityConverter.cs
- ClientRuntimeConfig.cs
- XamlTreeBuilderBamlRecordWriter.cs
- RepeaterDesigner.cs
- DataGridCaption.cs
- QuaternionKeyFrameCollection.cs
- BinaryFormatterWriter.cs
- IResourceProvider.cs
- WpfWebRequestHelper.cs
- SqlConnectionStringBuilder.cs
- CellQuery.cs
- TimeSpanSecondsOrInfiniteConverter.cs
- UriScheme.cs
- SafeMarshalContext.cs
- PlaceHolder.cs
- assemblycache.cs
- DataServiceQueryOfT.cs
- RectAnimationClockResource.cs
- Vector.cs
- RegexBoyerMoore.cs
- Int32.cs
- DES.cs
- EnumDataContract.cs
- FrameworkElement.cs
- BamlLocalizabilityResolver.cs
- RefreshPropertiesAttribute.cs
- OracleCommandSet.cs
- Variable.cs
- SecurityTokenTypes.cs
- CodeTypeDelegate.cs
- CurrencyWrapper.cs
- SqlMethodAttribute.cs
- MILUtilities.cs
- DataSourceComponent.cs
- PropertyItemInternal.cs
- CrossAppDomainChannel.cs
- DocumentViewerBase.cs
- HtmlGenericControl.cs
- XamlTreeBuilder.cs
- DataAdapter.cs
- WebPartEditorApplyVerb.cs
- PointCollectionConverter.cs
- DBSchemaRow.cs
- ListQueryResults.cs
- XmlExpressionDumper.cs
- AbandonedMutexException.cs
- HttpRequestBase.cs
- Buffer.cs
- TaskDesigner.cs
- mil_sdk_version.cs
- Floater.cs
- ValidateNames.cs
- PriorityRange.cs
- SrgsText.cs
- XmlSchemaAnyAttribute.cs
- ListViewDeleteEventArgs.cs
- XmlSchemaInferenceException.cs
- RemoteDebugger.cs
- FileAuthorizationModule.cs
- SamlConditions.cs
- CommonGetThemePartSize.cs
- AutoGeneratedField.cs