How to change disabled background color of TextBox in WPF

Unfortunately for the TextBox control, it appears like it’s not as simple as just adding a trigger and changing the Background color when the trigger condition is true. You have to override the entire ControlTemplate to achieve this. Below is one example on how you might do this: <Window x:Class=”StackOverflow.MainWindow” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” Title=”MainWindow” Height=”350″ Width=”525″> … Read more

How do I implement a TextBox that displays “Type here”?

Something that has worked for me: this.waterMarkActive = true; this.textBox.ForeColor = Color.Gray; this.textBox.Text = “Type here”; this.textBox.GotFocus += (source, e) => { if (this.waterMarkActive) { this.waterMarkActive = false; this.textBox.Text = “”; this.textBox.ForeColor = Color.Black; } }; this.textBox.LostFocus += (source, e) => { if (!this.waterMarkActive && string.IsNullOrEmpty(this.textBox.Text)) { this.waterMarkActive = true; this.textBox.Text = “Type here”; this.textBox.ForeColor … Read more

Get current cursor position in a textbox

It looks OK apart from the space in your ID attribute, which is not valid, and the fact that you’re replacing the value of your input before checking the selection. function textbox() { var ctl = document.getElementById(‘Javascript_example’); var startPos = ctl.selectionStart; var endPos = ctl.selectionEnd; alert(startPos + “, ” + endPos); } <input id=”Javascript_example” name=”one” … Read more

How to define TextBox input restrictions?

I’ve done this in the past with an attached behavior, which can be used like this: <TextBox b:Masking.Mask=”^\p{Lu}*$”/> The attached behavior code looks like this: /// <summary> /// Provides masking behavior for any <see cref=”TextBox”/>. /// </summary> public static class Masking { private static readonly DependencyPropertyKey _maskExpressionPropertyKey = DependencyProperty.RegisterAttachedReadOnly(“MaskExpression”, typeof(Regex), typeof(Masking), new FrameworkPropertyMetadata()); /// <summary> … Read more

Best way to restrict a text field to numbers only?

This is something I made another time for just numbers, it will allow all the formatters as well. jQuery $(‘input’).keypress(function(e) { var a = []; var k = e.which; for (i = 48; i < 58; i++) a.push(i); if (!(a.indexOf(k)>=0)) e.preventDefault(); });​ Try it http://jsfiddle.net/zpg8k/ As a note, you’ll want to filter on submit/server side … Read more