WPF Textbox use default text - wpf

I have a TextBox, which I want my users to enter a time value in the format XX:XX:XX. I already have validation in place to make sure they enter it in this format. However, now I'd like to have the colons there automatically. I'd like them to be in the textbox, and as the user types numbers, they just skip over the colons. Is it possible to have some kind of format decorator for the TextBox?
EDIT: I am using WPF 4.

you can use a masked textbox from the wpf toolkit
http://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox&referringTitle=Home

If you want to stick to vanilla WPF you could create a Custom Control and add 3 textboxes.
Put colons in between them and handle the keydown events to pass focus from one textbox to the other and at the same time accepting numbers only.
Again: using the toolkit might be less work.

Using three TextBoxes as Erno suggested is probably a better solution but you could also use the TextChanged event to add colons to the text (which might confuse the user), here'd be the code that would insert them after the second and fifth character:
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox tb = sender as TextBox;
if (e.Changes.Count == 1)
{
if (e.Changes.ElementAt(0).AddedLength == 1 && (tb.Text.Length == 2 || tb.Text.Length == 5))
{
tb.Text += ":";
tb.SelectionStart = tb.Text.Length;
}
}
}

Related

WPF - RichTextBox, make elements focusable

I have a RichTextBox that contains some text and another uiElements like ComboBox, etc.
I need to focus the Combobox when the cursor riches it. Now it just jumps over it. I can focus the combobox using the Tab button, but I need to focus it when I browse the richtextbox using the cursor.
Thanks for any advice
if what you need is a condition, that if satisfied, you call yourComboBox.Focus() then handle SelectionChange event and use CaretPosition.GetOffsetToPosition() method. Something like this:
void RichTextBox_SelectionChanged(object sender, RoutedEventArgs e)
{
int y = yourRTB.CaretPosition.GetOffsetToPosition(yourUI.ElementStart);
if (y == 1 || y == -3)
{
yourComboBox.Focus();
}
}
Of course you can change the condition to satisfy your needs.

How to validate textboxes in C#.net winforms

I have a form where I need to validate the textboxes like Firstname, Middlename, Lastname, emailId, date, mobilenumber. The validation should happen when a user starts typing in the textbox. errorprovider should show message under textbox if a user enters numbers in place of text and text in place of numbers. I got to know about implicit validation and explicit validation but I feel better to use implicit validation only because its on time error provider when user looses focus on text box or if he shifts to another textbox. I've posed this kind of question with a explicit validation code but no one responded me. So Im making it simple to get help. Do not think I havent done enough research before posting this question.
If you have a very specific validation to do, Marc's answer is correct. However, if you only ensure the "enter number instead of letters" or "enter letters instead of numbers" thing, a MaskedTextBox would do the job better than you (user wouldn't be able to answer incorrect data, and you can still warn them by handling the MaskInputRejected event)
http://msdn.microsoft.com/en-us/library/kkx4h3az(v=vs.100).aspx
You should take a look to the TextChanged Event in of your textbox.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.textchanged.aspx
This event is raised if the Text property is changed by either a
programmatic modification or user interaction.
I would do something like this
private void TextBoxExample_TextChanged(object sender, EventArgs e)
{
TextBox box = sender as TextBox;
if (box.Text.Contains("Example"))
{
LabelError.Text = "Error";
}
else
{
LabelError.Text = string.Empty;
}
}
Hope it helps :)
You can also use keyPressEvent to Avoid the entering the numerical values in the textboxes
it will not allow the numerical chars in the text box
private void textboxName_KeyPress(object sender, KeyPressEventArgs e)
{
//not allowing the non character values
if (!char.IsLetter(e.KeyChar) && !char.IsControl(e.KeyChar) && !(e.KeyChar == (char)Keys.Back) && !(e.KeyChar == (char)Keys.Left) && !(e.KeyChar == (char)Keys.Right) && !(e.KeyChar == (char)Keys.Space) && !char.IsPunctuation(e.KeyChar))
{
e.Handled = true;
}
}

How to prevent key entry with NumericUpDown control?

I have a numericupdown control on a winform and I noticed while testing that not only you have the option of changing the value by pressing up and down key but also simply entering the values from your keyboard.
I don't want that. I only want the user to be able to change the numericupdown's value only by clicking the up and down buttons within the box.
So far I simply can't find a solution.
Does anyone know how to do this?
To disable user from editing, set Readonly property to true.
updown.ReadOnly = true;
For more tailoring, you may refer this answer.
Sounds bad for user experience depending on the range of values you are allowing.
To do this you need to create a control with inherits from NumericUpDown and override the OnKeyPress/OnKeyDown methods.
Using updown.ReadOnly = True; does not work for me. It seems to be a reoccuring bug.
But catching any changes and then undo it does. For this bind the function updown_ValueChanged() to the updown.ValueChanged attribute.
decimal spin = 1;
private void updown_ValueChanged(object sender, EventArgs e)
{
if (updown.ReadOnly)
{
if (updown.Value != spin)
{
updown.Value = spin;
}
}
else spin = updown.Value;
}

TextBox Password Char

the textbox in Windows Forms used to have a PasswordChar property. In WPF there is an extra control for that: PasswordBox. This wouldn't be a problem but my application runs on an touchscreen only device. Unfortunately the password box does not support the on screen keyboard. I was wondering if there is a way of adding the password char feature to the standard textbox.
This answer may provide you with what you need.
I made my way around this particular problem by creating two Properties for the Password content and binding both of them to the same Model value. One of them (the visible UI Element) binds to Password. The Get on this property of course then returns an array of characters for display. The functions that must use the password text can use the PlainPassword Property.
Adding "UpdateSourceTrigger=PropertyChanged" to the Binding for the textbox causes the characters to appear in the text box as they are typed.
public string Password
{
set
{
Model.Password = value;
OnPropertyChanged("Password");
}
get
{
return new String('●', Model.Password.Length);
}
}
public string PlainPassword
{
set
{
Model.Password = value;
OnPropertyChanged("Password");
}
get { return Model.Password; }
}
I believe the only way you can achieve this is to create your own control based on textbox. Then just bind the actual text property to a property that returns your password character rather than the actual password. Then you can pull the password as a dependency property (though I've heard this is rather insecure, which is why it is not a dependency property in the password box), or just a regular property and access it by passing the whole textbox object.
A simple way to obfuscate the password in a TextBox is to use the Webdings font.
txtInput.FontFamily = new FontFamily("Webdings");
This is not completely safe, but sufficient in most cases. Note that Webdings works better than Wingdings, because Wingdings does not cover the lower case letters and returns everything in upper case.
helló!
im new here but maybe i can help u. i find this -> can be work whit WPF and passwordbox
private void delete_Click(object sender, RoutedEventArgs e)
{
if (pass_passbox.IsFocused == true)
{
pass_passbox.Password= "";
}
}
ofc u do this pass_passbox.Text if its textbox but when change WPF passwordbox u need to write can pass_passbox.Password and u can do changes from screen keyboard .
not fully tested but u can reset this way
and u can do select like this:
string Query = "Select * from [employeeinfo] where username='" + this.txt_user_name.Text + "' and password='" + this.pass_passbox.Password + "' ";
u can see this.pass_passbox.Password is the same at textbox this.pass_passbox.Text
private void txtBoxPassword_TextChanged(object sender, EventArgs e)
{
//password view protection//
txtBoxPassword.UseSystemPasswordChar = true;
}
That's a way to enable the DEFAULT character used for hiding the password from the system,if you wish to set your own password char just substitute the actual line inside the event function with the following:
txtBoxPassword.PasswordChar='*'; //or any other character

Prevent Silverlight DataGrid from Processing Enter Key

I have a datagrid in a Silverlight application. Clicking on the first row, and then pressing the Enter key causes the selection to move to the next row. I do not want this behavior - the Enter key is used for totally different purposes on this screen.
I realize that this is part of the editing framework, but I need a way to turn it off. I tried setting IsReadOnly to True (even though the control isn't technically read-only) and that didn't have any effect.
I attached to the datagrid KeyDown event but it's not called when the Enter key is pressed. It works fine for other keys.
I'm stumped. Thanks for your help.
Yeah, that's annoying.
As far as I know, the only option is to create your own control that inherits from DataGrid, and do what is needed before passing the event on to the base.
public class NewDataGrid : DataGrid
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
//do what is needed here
e.Handled = true;
}
base.OnKeyDown(e);
}
}

Resources