Click here to Skip to main content
15,909,242 members

venkatabc - Professional Profile



Summary

    Blog RSS
-2
Debator
10
Editor
1
Enquirer
117
Organiser
709
Participant
0
Author
0
Authority
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Reputation

Weekly Data. Recent events may not appear immediately. For information on Reputation please see the FAQ.

Privileges

Members need to achieve at least one of the given member levels in the given reputation categories in order to perform a given action. For example, to store personal files in your account area you will need to achieve Platinum level in either the Author or Authority category. The "If Owner" column means that owners of an item automatically have the privilege. The member types column lists member types who gain the privilege regardless of their reputation level.

ActionAuthorAuthorityDebatorEditorEnquirerOrganiserParticipantIf OwnerMember Types
Have no restrictions on voting frequencysilversilversilversilver
Bypass spam checks when posting contentsilversilversilversilversilversilvergoldSubEditor, Mentor, Protector, Editor
Store personal files in your account areaplatinumplatinumSubEditor, Editor
Have live hyperlinks in your profilebronzebronzebronzebronzebronzebronzesilverSubEditor, Protector, Editor
Have the ability to include a biography in your profilebronzebronzebronzebronzebronzebronzesilverSubEditor, Protector, Editor
Edit a Question in Q&AsilversilversilversilverYesSubEditor, Protector, Editor
Edit an Answer in Q&AsilversilversilversilverYesSubEditor, Protector, Editor
Delete a Question in Q&AYesSubEditor, Protector, Editor
Delete an Answer in Q&AYesSubEditor, Protector, Editor
Report an ArticlesilversilversilversilverSubEditor, Mentor, Protector, Editor
Approve/Disapprove a pending ArticlegoldgoldgoldgoldSubEditor, Mentor, Protector, Editor
Edit other members' articlesSubEditor, Protector, Editor
Create an article without requiring moderationplatinumSubEditor, Mentor, Protector, Editor
Approve/Disapprove a pending QuestionProtector
Approve/Disapprove a pending AnswerProtector
Report a forum messagesilversilverbronzeProtector, Editor
Approve/Disapprove a pending Forum MessageProtector
Have the ability to send direct emails to members in the forumsProtector
Create a new tagsilversilversilversilver
Modify a tagsilversilversilversilver

Actions with a green tick can be performed by this member.


 
GeneralSliver Light TextBox Filter Pin
venkatabc14-Jan-10 22:40
venkatabc14-Jan-10 22:40 
Add this two .cs files in your project


sample for textbox filter only numbers

// txtnumber your textbox name
TextBoxFilterService.SetFilter(txtnumber, TextBoxFilterType.Integer);



1.
public static class TextBoxFilterService {

private static string OldValueStr { get; set; }
/// <summary>
/// Filter Attached Dependency Property
/// </summary>
public static readonly DependencyProperty FilterProperty =
DependencyProperty.RegisterAttached("Filter", typeof(TextBoxFilterType), typeof(TextBoxFilterService),
new PropertyMetadata(OnFilterChanged));

/// <summary>
/// Gets the Filter property.
/// </summary>
public static TextBoxFilterType GetFilter(DependencyObject d) {
return (TextBoxFilterType)d.GetValue(FilterProperty);
}



/// <summary>
/// Sets the Filter property.
/// </summary>
public static void SetFilter(DependencyObject d, TextBoxFilterType value) {
d.SetValue(FilterProperty, value);
}


/// <summary>
/// Handles changes to the Filter property.
/// </summary>
/// <param name="d">DependencyObject</param>
/// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void OnFilterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
TextBox textBox = d as TextBox;
if (TextBoxFilterType.None != (TextBoxFilterType)e.OldValue) {
textBox.TextChanged -= new TextChangedEventHandler(textBox_TextChanged);
textBox.KeyDown -= new KeyEventHandler(textBox_KeyDown);

}
if (TextBoxFilterType.None != (TextBoxFilterType)e.NewValue) {
textBox.TextChanged += new TextChangedEventHandler(textBox_TextChanged);
textBox.KeyDown += new KeyEventHandler(textBox_KeyDown);
}
}

static void textBox_TextChanged(object sender, TextChangedEventArgs e)
{

}

/// <summary>
/// Handles the KeyDown event of the textBox control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs"/> instance containing the event data.</param>
private static void textBox_KeyDown(object sender, KeyEventArgs e) {
// bypass other keys!
if (IsValidOtherKey(e.Key)) {
return;
}
//
TextBoxFilterType filterType = GetFilter((DependencyObject)sender);
TextBox textBox = sender as TextBox;
if (null == textBox) {
textBox = e.OriginalSource as TextBox;
}
//
switch (filterType) {
case TextBoxFilterType.PositiveInteger:
e.Handled = !IsValidIntegerKey(textBox, e.Key, e.PlatformKeyCode, false);
break;
case TextBoxFilterType.Integer:
e.Handled = !IsValidIntegerKey(textBox, e.Key, e.PlatformKeyCode, true);
break;
case TextBoxFilterType.PositiveDecimal:
e.Handled = !IsValidDecmialKey(textBox, e.Key, e.PlatformKeyCode, false);
break;
case TextBoxFilterType.Decimal:
e.Handled = !IsValidDecmialKey(textBox, e.Key, e.PlatformKeyCode, true);
break;
case TextBoxFilterType.Alpha:
e.Handled = !IsValidAlphaKey(e.Key);
break;
}
}

/// <summary>
/// Determines whether the specified key is valid other key.
/// </summary>
/// <param name="key">The key.</param>
/// <returns>
/// <c>true</c> if [is valid other key] [the specified key]; otherwise, <c>false</c>.
/// </returns>
private static bool IsValidOtherKey(Key key) {
// allow control keys
if ((Keyboard.Modifiers & ModifierKeys.Control) != 0) {
return true;
}
// allow
// Back, Tab, Enter, Shift, Ctrl, Alt, CapsLock, Escape, PageUp, PageDown
// End, Home, Left, Up, Right, Down, Insert, Delete
// except for space!
// allow all Fx keys
if (
(key < Key.D0 && key != Key.Space)
|| (key > Key.Z && key < Key.NumPad0)) {
return true;
}
// we need to examine all others!
return false;
}

/// <summary>
/// Determines whether the specified key is valid integer key for the specified text box.
/// </summary>
/// <param name="textBox">The text box.</param>
/// <param name="key">The key.</param>
/// <param name="platformKeyCode">The platform key code.</param>
/// <param name="negativeAllowed">if set to <c>true</c> [negative allowed].</param>
/// <returns>
/// <c>true</c> if the specified key is valid integer key for the specified text box; otherwise, <c>false</c>.
/// </returns>
private static bool IsValidIntegerKey(TextBox textBox, Key key, int platformKeyCode, bool negativeAllowed) {
if ((Keyboard.Modifiers & ModifierKeys.Shift) != 0) {
return false;
}
if (Key.D0 <= key && key <= Key.D9) {
return true;
}
if (Key.NumPad0 <= key && key <= Key.NumPad9) {
return true;
}
if (negativeAllowed && (key == Key.Subtract || (key == Key.Unknown && platformKeyCode == 189))) {
return 0 == textBox.Text.Length;
}
//
return false;
}

/// <summary>
/// Determines whether the specified key is valid decmial key for the specified text box.
/// </summary>
/// <param name="textBox">The text box.</param>
/// <param name="key">The key.</param>
/// <param name="platformKeyCode">The platform key code.</param>
/// <param name="negativeAllowed">if set to <c>true</c> [negative allowed].</param>
/// <returns>
/// <c>true</c> if the specified key is valid decmial key for the specified text box; otherwise, <c>false</c>.
/// </returns>
private static bool IsValidDecmialKey(TextBox textBox, Key key, int platformKeyCode, bool negativeAllowed) {
if (IsValidIntegerKey(textBox, key, platformKeyCode, negativeAllowed)) {
return true;
}
if (key == Key.Decimal || (key == Key.Unknown && platformKeyCode == 190)) {
return !textBox.Text.Contains(".");
}
return false;
//
}

/// <summary>
/// Determines whether the specified key is valid alpha key.
/// </summary>
/// <param name="key">The key.</param>
/// <returns>
/// <c>true</c> if the specified key is valid alpha key for the specified text box; otherwise, <c>false</c>.
/// </returns>
private static bool IsValidAlphaKey(Key key) {
if (Key.A <= key && key <= Key.Z) {
return true;
}
//
return false;
//
}
}


2.
public enum TextBoxFilterType
{
None,
PositiveInteger,
Integer,
PositiveDecimal,
Decimal,
Alpha,
}

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.