TextBox Password Char - wpf

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

Related

How to prevent silverlight validation popup from appearing half off the screen

I have a silverlight 4 application which has some text boxes that are as wide as the page.
When there is a validation error, a popup is displayed when the user clicks in the control.
The problem is - it only shows the popup for these long text boxes to the left of the text box. It wont go above or below and so as a consequence, most of the popup is displayed out of the page so its chopped off.
I know that I can re-template the text box and try to adjust the popup myself, but before doing that I just wanted to check to see if someone knew of a simple property or something that I can use to prevent that from happening?
Cheers
Rod.
Good question. I guess I would try to solve this via a somewhat "intelligent" AttachedProperty.
Pseudo code ahead:
<TextBox ... my:PopupUtils.KeepPopupWithinScreen="True"/>
And the (pseudo c#) code:
public static class PopupUtils
{
// remember: pseudo code, just to get the idea
static AttachedProperty KeepPopupWithinScreen = type: bool, default: false,
onChanged: HandleKeepPopupWithinScreenChanged;
private static void HandleKeepPopupWithinScreenChanged(
DependencyObject obj, bool value)
{
obj.Loaded += HandleTargetElementLoaded;
}
private staic void HandleTargetElementLoaded(object sender, ...)
{
var popup = VisualTreeHelper.GetDecendantOfType<Popup>(sender);
if ( popup != null )
{
var offsetController = new OffsetController();
offsetController.SetBinding(ObservedOffsetProperty,
new Binding("HorizontalOffset"){Source=popup});
offsetController.ControlledTarget = popup;
//now to prevent garbageCollection...
SetAttachedOffsetController(popup,offsetController);
}
}
public static AttachedProperty AttachedOffsetController = type:OffsetController;
}
I do this sometimes, so this pattern is actually working quite nicely. Maybe it feels a bit "unnatural" at first.
Just letting you know of the solution I used to this problem.
It was another StackOverflow question which I have lost the reference to so I do apologise for not referencing it properly, but the problem is caused by the Text Box and other control styles having a fixed position of to the side of the control when displaying the validation message.
I simply had to create a copy of the style and have the popup appear at the top of the controls instead of beside it.
Problem Solved.
Cheers
Rod.

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;
}

How to give an initial value to textBox?

I want my C# program to have initial values for its textboxes. For example, in one of the textboxes, it should say "Please enter your name".
When you click (or tabStop) on the textbox, the initial value should disappear and the user will be able to enter their input to the textbox.
I can do all this with click_event, but using this method the initial text would not have less opacity. How am I able to achieve this?
This is how I finally did it:
Boolean first_time_click = true;
private void Form1_Load(object sender, EventArgs e)
{
textBox1.ForeColor = System.Drawing.Color.Gray;
textBox1.Text = "Enter the Text";
}
private void For_First_Click()
{
if (first_time_click)
{
textBox1.Clear();
textBox1.ForeColor = textBox1.ForeColor = SystemColors.WindowText;
}
first_time_click = false;
}
private void textBox1_Click(object sender, EventArgs e)
{
For_First_Click();
}
I assume you are talking about winform (tabstop) you have to handle it within the event key-press. you can use the below code:
TextBox1.Select(0, TextBox1.Text.Length);
this will select the text and window will remove it for you as soon as the user start to typing
you can use the same code to have this behavior also for TabStop
All you need to do is set the Textbox's .Text property and use GotFocus event to clear the box when the person clicks (or tabs) into it to start typing.
Always remember that there are more ways than the mouse to navigate a form, so use the GotFocus event to determine when the user enters a control, and use the Validated event to determine when they've changed data and exited the control.
For this type of effect you need java script.Because java script provide you functionality of mouse hover and mouse out these are the functions which provide you the same functionality which u seeing in this page of search bar. If you need code reply me i can give you.

WPF Textbox use default text

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;
}
}
}

Resources