Silverlight Convert PlatformKeyCode to Character Code - silverlight

Question:
Is there a good way in Silverlight to intercept undesirable characters from being entered into a textbox?
Background:
I have a textbox which allows a user to enter a filename. I would like to exclude invalid file characters from being entered into the textbox. A few of these characters are:
'?'
'\'
'<'
'>'
Although the Silverlight TextBox class does not support a KeyPress event, it does have a KeyDown and a KeyUp event that can be used to retrieve character information when a key is entered into the textbox. It exposes these as a member of the Key enumeration or it can return an int for the PlatformKeyCode.
Of course the range of keys is larger/different from the range of characters - "F keys" are an example of this. However the presence of something like a KeyPress event in Windows Forms is indicative of the usefulness of being able to extract specific character information.
To do a proof of concept that things could work I hardcoded the PlatformKeyCode values for the undesired characters for my platform into the event handler and everything worked... but of course this is just my platform. I need to make sure this implementation is platform agnostic. Here is the code to demonstrate how I would like it to work:
private void theText_KeyDown(object sender, KeyEventArgs e)
{
int[] illegals = { 191, 188, 190, 220, 186, 222, 191, 56, 186};
if (illegals.Any(i => i == e.PlatformKeyCode)) e.Handled = true;
}

Both responses (in comments) from Henrik and Johannes contain the best answer for the scenario. Rather than thinking like a Windows Forms programmer and capturing the specific key event, the canonical approach in Silverlight is to use the TextChanged event to remove undesirable characters from a TextBox. A similar question on allowing only numeric input to a TextBox is what prompted the solution that was used.
The following code samples the approach I took after reading the comments and it works quite well to remove characters inappropriate for entry:
private void fileNameTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
string illegalChars = #"?<>:""\/*|";
fileNameTextBox.Text = String.Join("", fileNameTextBox.Text.Split(illegalChars.ToCharArray()));
fileNameTextBox.SelectionStart = fileNameTextBox.Text.Length;
}

using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
public class FilterTextBoxBehavior : Behavior<TextBox>
{
public readonly static DependencyProperty AllowAlphaCharactersProperty = DependencyProperty.Register("AllowAlphaCharacters", typeof(bool), typeof(FilterTextBoxBehavior), new PropertyMetadata(true));
public bool AllowAlphaCharacters
{
get { return (bool)GetValue(AllowAlphaCharactersProperty); }
set { SetValue(AllowAlphaCharactersProperty, value); }
}
public readonly static DependencyProperty AllowNumericCharactersProperty = DependencyProperty.Register("AllowNumericCharacters", typeof(bool), typeof(FilterTextBoxBehavior), new PropertyMetadata(true));
public bool AllowNumericCharacters
{
get { return (bool)GetValue(AllowNumericCharactersProperty); }
set { SetValue(AllowNumericCharactersProperty, value); }
}
public readonly static DependencyProperty AllowSpecialCharactersProperty = DependencyProperty.Register("AllowSpecialCharacters", typeof(bool), typeof(FilterTextBoxBehavior), new PropertyMetadata(true));
public bool AllowSpecialCharacters
{
get { return (bool)GetValue(AllowSpecialCharactersProperty); }
set { SetValue(AllowSpecialCharactersProperty, value); }
}
public readonly static DependencyProperty DoNotFilterProperty = DependencyProperty.Register("DoNotFilter", typeof(string), typeof(FilterTextBoxBehavior), new PropertyMetadata(default(string)));
public string DoNotFilter
{
get { return (string)GetValue(DoNotFilterProperty); }
set { SetValue(DoNotFilterProperty, value); }
}
protected override void OnAttached()
{
base.OnAttached();
if (AssociatedObject == null) { return; }
FilterAssociatedObject();
AssociatedObject.TextChanged += OnTextChanged;
}
protected override void OnDetaching()
{
base.OnDetaching();
if (AssociatedObject == null) { return; }
FilterAssociatedObject();
AssociatedObject.TextChanged -= OnTextChanged;
}
private void OnTextChanged(object sender, TextChangedEventArgs e) { FilterAssociatedObject(); }
private void FilterAssociatedObject()
{
int cursorLocation = AssociatedObject.SelectionStart;
for (int i = AssociatedObject.Text.Length - 1; i >= 0; i--)
{
char c = AssociatedObject.Text[i];
if (ValidChar(c)) { continue; }
AssociatedObject.Text = AssociatedObject.Text.Remove(i, 1);
cursorLocation--;
}
AssociatedObject.SelectionStart = Math.Min(AssociatedObject.Text.Length, Math.Max(0, cursorLocation));
}
private bool ValidChar(char c)
{
if (!string.IsNullOrEmpty(DoNotFilter) && DoNotFilter.Contains(c)) { return true; }
if (!AllowAlphaCharacters && char.IsLetter(c)) { return false; }
if (!AllowNumericCharacters && char.IsNumber(c)) { return false; }
if (!AllowSpecialCharacters && Regex.IsMatch(c.ToString(), #"[\W|_]")) { return false; }
return true;
}
}

Related

WPF Custom TextBox with Decimal Formatting

I am new to WPF.
I have a requirement that I need to develop a custom textbox control which should support the functionality like:
Should accept only decimal values.
Should round off to 3 decimal places when assigned a value through code or by the user.
Should show the full value(without formatting) on focus.
Eg:
If 2.21457 is assigned to textbox(by code or by user), it should display 2.215. When user clicks in it to edit it, it must show the full value 2.21457.
After the user edits the value to 5.42235 and tabs out, it should again round off to 5.422.
Tried it without success. So need some help on it.
Thanks in advance for the help.
Thanks
I have written a custom control which will have dependency property called ActualText. Bind your value into that ActualText property and manipulated the Text property of the textbox during the gotfocus and lostfocus event. Also validated for decimal number in the PreviewTextInput event. refer the below code.
class TextBoxEx:TextBox
{
public string ActualText
{
get { return (string)GetValue(ActualTextProperty); }
set { SetValue(ActualTextProperty, value); }
}
// Using a DependencyProperty as the backing store for ActualText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ActualTextProperty =
DependencyProperty.Register("ActualText", typeof(string), typeof(TextBoxEx), new PropertyMetadata(string.Empty, OnActualTextChanged));
private static void OnActualTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TextBox tx = d as TextBox;
tx.Text = (string)e.NewValue;
string str = tx.Text;
double dbl = Convert.ToDouble(str);
str = string.Format("{0:0.###}", dbl);
tx.Text = str;
}
public TextBoxEx()
{
this.GotFocus += TextBoxEx_GotFocus;
this.LostFocus += TextBoxEx_LostFocus;
this.PreviewTextInput += TextBoxEx_PreviewTextInput;
}
void TextBoxEx_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
decimal d;
if(!decimal.TryParse(e.Text,out d))
{
e.Handled = true;
}
}
void TextBoxEx_LostFocus(object sender, System.Windows.RoutedEventArgs e)
{
ConvertText();
}
void TextBoxEx_GotFocus(object sender, System.Windows.RoutedEventArgs e)
{
this.Text = ActualText;
}
private void ConvertText()
{
string str = this.Text;
ActualText = str;
double dbl = Convert.ToDouble(str);
str = string.Format("{0:0.###}", dbl);
this.Text = str;
}
}

Extended WPF Toolkit DoubleUpDown

I've added an Extended WPF Toolkit DoubleUpDown control.
The behaviour is if you type 5.35 it is fine.
Then say you type 4.3errortext7 and tab it reverts back to 5.35 (as the 4.3errortext7 is not a valid number).
However I'd like it just to go to 4.37 in that case (and ignore the invalid characters.
Is there an elegant way to get my required behaviour?
The best I was able to come up with is to use the PreviewTextInput Event and check for valid input, unfortunatly it will allow still allow a space between numbers, but will prevent all other text from being input.
private void doubleUpDown1_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (!(char.IsNumber(e.Text[0]) || e.Text== "." ))
{
e.Handled = true;
}
}
Maybe a bit late but I had the same problem yesterday and I also did not want to register a handler every time I use the control. I mixed the solution by Mark Hall with an Attached Behavior (inspired by this post: http://www.codeproject.com/Articles/28959/Introduction-to-Attached-Behaviors-in-WPF):
public static class DoubleUpDownBehavior
{
public static readonly DependencyProperty RestrictInputProperty =
DependencyProperty.RegisterAttached("RestrictInput", typeof(bool),
typeof(DoubleUpDownBehavior),
new UIPropertyMetadata(false, OnRestrictInputChanged));
public static bool GetRestrictInput(DoubleUpDown ctrl)
{
return (bool)ctrl.GetValue(RestrictInputProperty);
}
public static void SetRestrictInput(DoubleUpDown ctrl, bool value)
{
ctrl.SetValue(RestrictInputProperty, value);
}
private static void OnRestrictInputChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
{
DoubleUpDown item = depObj as DoubleUpDown;
if (item == null)
return;
if (e.NewValue is bool == false)
return;
if ((bool)e.NewValue)
item.PreviewTextInput += OnPreviewTextInput;
else
item.PreviewTextInput -= OnPreviewTextInput;
}
private static void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (!(char.IsNumber(e.Text[0]) ||
e.Text == CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator))
{
e.Handled = true;
}
}
}
Then you can simply set the default style for DoubleUpDown like this:
<Style TargetType="xctk:DoubleUpDown">
<Setter Property="behaviors:DoubleUpDownBehavior.RestrictInput" Value="True" />
</Style>
In my case, it was much better to use regex.
private void UpDownBox_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
var upDownBox = (sender as DoubleUpDown);
TextBox textBoxInTemplate = (TextBox)upDownBox.Template.FindName("PART_TextBox", upDownBox);
Regex regex = new Regex("^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$");
e.Handled = !regex.IsMatch(upDownBox.Text.Insert((textBoxInTemplate).SelectionStart, e.Text));
}

Change DataPager text

This might sound like a strange request and i'm not sure if it's actually possible, but I have a Silverlight DataPager control where it says "Page 1 of X" and I want to change the "Page" text to say something different.
Can this be done?
In DataPager style there is a part by name CurrentPagePrefixTextBlock by default its value is "Page".
You can refer http://msdn.microsoft.com/en-us/library/dd894495(v=vs.95).aspx for more info.
One of the solution is to extend DataPager
Here is the code to do that
public class CustomDataPager:DataPager
{
public static readonly DependencyProperty NewTextProperty = DependencyProperty.Register(
"NewText",
typeof(string),
typeof(CustomDataPager),
new PropertyMetadata(OnNewTextPropertyChanged));
private static void OnNewTextPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var newValue = (string)e.NewValue;
if ((sender as CustomDataPager).CustomCurrentPagePrefixTextBlock != null)
{
(sender as CustomDataPager).CustomCurrentPagePrefixTextBlock.Text = newValue;
}
}
public string NewText
{
get { return (string)GetValue(NewTextProperty); }
set { SetValue(NewTextProperty, value); }
}
private TextBlock _customCurrentPagePrefixTextBlock;
internal TextBlock CustomCurrentPagePrefixTextBlock
{
get
{
return _customCurrentPagePrefixTextBlock;
}
private set
{
_customCurrentPagePrefixTextBlock = value;
}
}
public CustomDataPager()
{
this.DefaultStyleKey = typeof(DataPager);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
CustomCurrentPagePrefixTextBlock = GetTemplateChild("CurrentPagePrefixTextBlock") as TextBlock;
if (NewText != null)
{
CustomCurrentPagePrefixTextBlock.Text = NewText;
}
}
}
Now by setting NewText property in this CustomDataPager we can get whatever text we want instead of "Page"
xmlns:local="clr-namespace:Assembly which contains CustomDataPager"
<local:CustomDataPager x:Name="dataPager1"
PageSize="5"
AutoEllipsis="True"
NumericButtonCount="3"
DisplayMode="PreviousNext"
IsTotalItemCountFixed="True" NewText="My Text" />
Now it displays "My Text" Instead of "Page".
But other parts also need to be customised inorder make this work correctly!!
Hope this answers your question

How to animate "typing" text in SketchFlow?

In Microsoft's Expression Blend 3 SketchFlow application.
How would you go about animating the typing of text, ideally in staged character by character fashion. As if the user is typing it.
An associated flashing cursor would make it perfect, but that's far into the realm of "nice to have".
The keyframe animation system, does not allow you to manipulate the
Common Property > Text
field so therefore it doesn't persist as a recorded change in that keyframe of animation.
I'm looking for either editor steps (using some kind of other control) or even XAML code...
<VisualState>
<StoryBoard>
<DoubleAnimationUsingKeyFrame ... >
After blogging about this with a solution involving a wipe animation of a rectangle over a text block, a response blog post with a more advanced solution of using a custom behavior attached to a text block was created.
Creating a 'TypeOnAction' behavior and adding to a TextBlock, will give the desired effect of character by character display, with a customizable appearance rate. Get the full code sample here.
public class TypeOnAction : TriggerAction<TextBlock>
{
DispatcherTimer timer;
int len = 1;
public TypeOnAction()
{
timer = new DispatcherTimer();
}
protected override void Invoke(object o)
{
if (AssociatedObject == null)
return;
AssociatedObject.Text = "";
timer.Interval = TimeSpan.FromSeconds(IntervalInSeconds);
timer.Tick += new EventHandler(timer_Tick);
len = 1;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
if (len > 0 && len <= TypeOnText.Length)
{
AssociatedObject.Text = TypeOnText.Substring(0, len);
len++;
timer.Start();
}
else
timer.Stop();
}
public string TypeOnText
{
get { return (string)GetValue(TypeOnTextProperty); }
set { SetValue(TypeOnTextProperty, value); }
}
public static readonly DependencyProperty TypeOnTextProperty =
DependencyProperty.Register("TypeOnText", typeof(string), typeof(TypeOnAction), new PropertyMetadata(""));
public double IntervalInSeconds
{
get { return (double)GetValue(IntervalInSecondsProperty); }
set { SetValue(IntervalInSecondsProperty, value); }
}
public static readonly DependencyProperty IntervalInSecondsProperty =
DependencyProperty.Register("IntervalInSeconds", typeof(double), typeof(TypeOnAction), new PropertyMetadata(0.35));
}

Adding button into a Listview in WinForms

Is there a way to add a button control to a cell in inside a ListView in a WinForms app?
Here is a code of a class ListViewExtender that you can reuse. It's not a derived class of ListView, basically you just declare that a specific column is displayed as buttons instead of text. The button's text is the subItem's text.
It allows big sized list views without problems, does not use p/invoke, and also works with horizontal scrollbars (some code proposed as answers here don't or are quite slow with a great number of items). Note it requires the extended ListView to have FullRowSelect set to true and view type set to Details.
This is a sample code that uses it:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); // you need to add a listView named listView1 with the designer
listView1.FullRowSelect = true;
ListViewExtender extender = new ListViewExtender(listView1);
// extend 2nd column
ListViewButtonColumn buttonAction = new ListViewButtonColumn(1);
buttonAction.Click += OnButtonActionClick;
buttonAction.FixedWidth = true;
extender.AddColumn(buttonAction);
for (int i = 0; i < 10000; i++)
{
ListViewItem item = listView1.Items.Add("item" + i);
item.SubItems.Add("button " + i);
}
}
private void OnButtonActionClick(object sender, ListViewColumnMouseEventArgs e)
{
MessageBox.Show(this, #"you clicked " + e.SubItem.Text);
}
}
}
Here is the ListViewExtender code and associated classes:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
namespace WindowsFormsApplication1
{
public class ListViewExtender : IDisposable
{
private readonly Dictionary<int, ListViewColumn> _columns = new Dictionary<int, ListViewColumn>();
public ListViewExtender(ListView listView)
{
if (listView == null)
throw new ArgumentNullException("listView");
if (listView.View != View.Details)
throw new ArgumentException(null, "listView");
ListView = listView;
ListView.OwnerDraw = true;
ListView.DrawItem += OnDrawItem;
ListView.DrawSubItem += OnDrawSubItem;
ListView.DrawColumnHeader += OnDrawColumnHeader;
ListView.MouseMove += OnMouseMove;
ListView.MouseClick += OnMouseClick;
Font = new Font(ListView.Font.FontFamily, ListView.Font.Size - 2);
}
public virtual Font Font { get; private set; }
public ListView ListView { get; private set; }
protected virtual void OnMouseClick(object sender, MouseEventArgs e)
{
ListViewItem item;
ListViewItem.ListViewSubItem sub;
ListViewColumn column = GetColumnAt(e.X, e.Y, out item, out sub);
if (column != null)
{
column.MouseClick(e, item, sub);
}
}
public ListViewColumn GetColumnAt(int x, int y, out ListViewItem item, out ListViewItem.ListViewSubItem subItem)
{
subItem = null;
item = ListView.GetItemAt(x, y);
if (item == null)
return null;
subItem = item.GetSubItemAt(x, y);
if (subItem == null)
return null;
for (int i = 0; i < item.SubItems.Count; i++)
{
if (item.SubItems[i] == subItem)
return GetColumn(i);
}
return null;
}
protected virtual void OnMouseMove(object sender, MouseEventArgs e)
{
ListViewItem item;
ListViewItem.ListViewSubItem sub;
ListViewColumn column = GetColumnAt(e.X, e.Y, out item, out sub);
if (column != null)
{
column.Invalidate(item, sub);
return;
}
if (item != null)
{
ListView.Invalidate(item.Bounds);
}
}
protected virtual void OnDrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
e.DrawDefault = true;
}
protected virtual void OnDrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
ListViewColumn column = GetColumn(e.ColumnIndex);
if (column == null)
{
e.DrawDefault = true;
return;
}
column.Draw(e);
}
protected virtual void OnDrawItem(object sender, DrawListViewItemEventArgs e)
{
// do nothing
}
public void AddColumn(ListViewColumn column)
{
if (column == null)
throw new ArgumentNullException("column");
column.Extender = this;
_columns[column.ColumnIndex] = column;
}
public ListViewColumn GetColumn(int index)
{
ListViewColumn column;
return _columns.TryGetValue(index, out column) ? column : null;
}
public IEnumerable<ListViewColumn> Columns
{
get
{
return _columns.Values;
}
}
public virtual void Dispose()
{
if (Font != null)
{
Font.Dispose();
Font = null;
}
}
}
public abstract class ListViewColumn
{
public event EventHandler<ListViewColumnMouseEventArgs> Click;
protected ListViewColumn(int columnIndex)
{
if (columnIndex < 0)
throw new ArgumentException(null, "columnIndex");
ColumnIndex = columnIndex;
}
public virtual ListViewExtender Extender { get; protected internal set; }
public int ColumnIndex { get; private set; }
public virtual Font Font
{
get
{
return Extender == null ? null : Extender.Font;
}
}
public ListView ListView
{
get
{
return Extender == null ? null : Extender.ListView;
}
}
public abstract void Draw(DrawListViewSubItemEventArgs e);
public virtual void MouseClick(MouseEventArgs e, ListViewItem item, ListViewItem.ListViewSubItem subItem)
{
if (Click != null)
{
Click(this, new ListViewColumnMouseEventArgs(e, item, subItem));
}
}
public virtual void Invalidate(ListViewItem item, ListViewItem.ListViewSubItem subItem)
{
if (Extender != null)
{
Extender.ListView.Invalidate(subItem.Bounds);
}
}
}
public class ListViewColumnMouseEventArgs : MouseEventArgs
{
public ListViewColumnMouseEventArgs(MouseEventArgs e, ListViewItem item, ListViewItem.ListViewSubItem subItem)
: base(e.Button, e.Clicks, e.X, e.Y, e.Delta)
{
Item = item;
SubItem = subItem;
}
public ListViewItem Item { get; private set; }
public ListViewItem.ListViewSubItem SubItem { get; private set; }
}
public class ListViewButtonColumn : ListViewColumn
{
private Rectangle _hot = Rectangle.Empty;
public ListViewButtonColumn(int columnIndex)
: base(columnIndex)
{
}
public bool FixedWidth { get; set; }
public bool DrawIfEmpty { get; set; }
public override ListViewExtender Extender
{
get
{
return base.Extender;
}
protected internal set
{
base.Extender = value;
if (FixedWidth)
{
base.Extender.ListView.ColumnWidthChanging += OnColumnWidthChanging;
}
}
}
protected virtual void OnColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
{
if (e.ColumnIndex == ColumnIndex)
{
e.Cancel = true;
e.NewWidth = ListView.Columns[e.ColumnIndex].Width;
}
}
public override void Draw(DrawListViewSubItemEventArgs e)
{
if (_hot != Rectangle.Empty)
{
if (_hot != e.Bounds)
{
ListView.Invalidate(_hot);
_hot = Rectangle.Empty;
}
}
if ((!DrawIfEmpty) && (string.IsNullOrEmpty(e.SubItem.Text)))
return;
Point mouse = e.Item.ListView.PointToClient(Control.MousePosition);
if ((ListView.GetItemAt(mouse.X, mouse.Y) == e.Item) && (e.Item.GetSubItemAt(mouse.X, mouse.Y) == e.SubItem))
{
ButtonRenderer.DrawButton(e.Graphics, e.Bounds, e.SubItem.Text, Font, true, PushButtonState.Hot);
_hot = e.Bounds;
}
else
{
ButtonRenderer.DrawButton(e.Graphics, e.Bounds, e.SubItem.Text, Font, false, PushButtonState.Default);
}
}
}
}
The ListView itself (or ListViewItem) does not function as a container of any kind so no way to add controls directly, however it is doable. I have used this extended ListView with a lot of success: Embedding Controls in a ListView.
This is the BEST custom listview control for WinForms.
ObjectListView
To make the extender of Simon Mourier working is missing the following line:
extender.AddColumn(buttonAction);
This is, it should look like:
ListViewExtender extender = new ListViewExtender(listSummary);
ListViewButtonColumn buttonAction = new ListViewButtonColumn(2);
buttonAction.Click += OnButtonActionClick;
buttonAction.FixedWidth = true;
extender.AddColumn(buttonAction);
Maybe this could be of interest?
http://www.codeproject.com/KB/list/extendedlistviews.aspx
No, a standard Windows Forms ListView doesn't support embedded controls. You could try to build your own custom control, or you could use something like http://www.codeproject.com/KB/list/EXListView.aspx.
No and yes, ListView itself does not support such functionality, but you can create a button on top of it, so that it appears to the user as integral part of the listview. (I suppose this is what the ExtendedListView mentioned above does too).
Maybe it worths mentioning, the list view control might be designed in WPF as an usercontrol/custom control with buttons in its ListViewItems, and then use this control in the WinForms application, in an ElementHost control.
I accidentally come across a discussion before, hope this help: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/ee232cc4-68c5-4ed3-9ea7-d4d999956504/
You could use a GlacialList. It allow you to put ANY control inside a list cell and it's simple to use. You will just need to join a GlacialList.dll document to the reference part of your Solution. If you click the link it will show you how it works and how to use it and download it.
If you have a System.IO.FileNotFoundException on the InitializeComponent() just download source code from the above link, compile and use this .dll (inside bin/Debug subfolder) to your project .
Here is an example of what it looks like:
This looks like the simplest answer I have come across... just added an ItemCommand to the ListView.
See this link: handle-the-button-click-event-from-an-asp-net-listview-control

Resources