Hide password text - c#

I have a textbox using UseSystemPasswordChar, so it will not display the password that the user enters. The issue is that the password is still able to be read by something like Spy++. I'm looking for a way to hide this like they do in the password fields in the Services.msc > Log On tab.

Here is what I've got so far.
You can improve this by having some unique events to indicate whether a pressed key has been accepted, if InputFilter or RealText has been changed, etc...
Another great thing to improve would be the default usage of InputFilter, because working with char and Keys doesn't really work for many special keys. For example - at the moment, if you press Alt+F4 when the PasswordBox is in focus, it will type in 's'... So there's a bag of bugs to fix.
And lastly, there's probably a more elegant way to handle capital vs non-capital letters input than what I did there.
So here it is:
public class PasswordBox : TextBox
{
private string _realText;
public string RealText
{
get { return this._realText; }
set
{
var i = this.SelectionStart;
this._realText = value ?? "";
this.Text = "";
this.Text = new string('*', this._realText.Length);
this.SelectionStart = i > this.Text.Length ? this.Text.Length : i;
}
}
private Func<KeyEventArgs, bool> _inputFilter;
public Func<KeyEventArgs, bool> InputFilter
{
get { return this._inputFilter; }
set { this._inputFilter = value ?? (e => true); }
}
public PasswordBox()
{
this.RealText = "";
this.InputFilter = e => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".Any(c => c == e.KeyValue);
}
protected override void OnKeyDown(KeyEventArgs e)
{
e.SuppressKeyPress = true;
switch (e.KeyCode)
{
case Keys.Back:
if (this.SelectionStart > 0 || this.SelectionLength > 0)
{
this.RealText = this.SelectionLength == 0
? this.RealText.Remove(--this.SelectionStart, 1)
: this.RealText.Remove(this.SelectionStart, this.SelectionLength);
}
break;
case Keys.Delete:
if (this.SelectionStart == this.TextLength)
{
return;
}
this.RealText = this.RealText.Remove(this.SelectionStart, this.SelectionLength == 0 ? 1 : this.SelectionLength);
break;
case Keys.X:
case Keys.C:
case Keys.V:
if (e.Control)
{
return;
}
goto default;
case Keys.Right:
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Shift:
case Keys.Home:
case Keys.End:
e.SuppressKeyPress = false;
base.OnKeyDown(e);
break;
default:
if (e.Control)
{
e.SuppressKeyPress = false;
base.OnKeyDown(e);
break;
}
if (this.InputFilter(e))
{
var c = (char)e.KeyValue;
if (e.Shift == IsKeyLocked(Keys.CapsLock))
{
c = char.ToLower(c);
}
this.RealText = this.RealText.Remove(this.SelectionStart, this.SelectionLength)
.Insert(this.SelectionStart, c.ToString());
this.SelectionStart++;
}
break;
}
}
}

So try something like this
private string realpass = "";
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (Char) Keys.Back)
realpass += realpass.Substring(0, realpass.Length - 2);
realpass += e.KeyChar.ToString();
textBox1.Text = "";
for (int i = 0; i < realpass.Length; i++)
textBox1.Text += "*";
}

You should not use your own dialog if you intend to collect Windows/domain user credentials. You should use what Windows provides via PInvoke or simply use a wrapper like this,
http://weblogs.asp.net/hernandl/archive/2005/11/21/usercredentialsdialog.aspx
and this,
http://credentials.codeplex.com/

Related

getting caret position from DataGridView

I am looking to validate an input to a DataGridView what has DataGridViewTextBoxCells. The textbox cells can be either signed or unsigned and int or double based on the column in focus. The issue that I am having is determining the caret position when a key is pressed.
For example if the cell allows signed doubles(caret <=> '^'):
a valid keypress event : ^12.3456 with a key press of '-' would give -12.3456
an invalid keypress event: 12.34-56 with a key press of '-' would give 12.34-56
I have not been able to find anything that will allow me to access the caret position at the time of a keypress.
private void SomeGridView_KeyPress(object sender, KeyPressEventArgs e)
{
DataGridView DGV = SomeGridView;
string curStr;
bool isFirst = DGV.CurrentCell.EditedFormattedValue == null;
curStr = isFirst ? "" : DGV.CurrentCell.EditedFormattedValue.ToString();
Type type = DGV.CurrentCell.GetType();
if (DGV.CurrentCell.GetType() == typeof(DataGridViewTextBoxCell))
{
DataGridViewTextBoxCell DGVTB = (DataGridViewTextBoxCell)DGV.CurrentCell;
//Not sure how to get caret here
}
switch ((GridDataEnum)DGV.CurrentCell.ColumnIndex)
{
case GridDataEnum.setpoint:
case GridDataEnum.SlopePoint:
e.Handled = !HelperUtils.isValidNumber(curStr, e.KeyChar, HelperUtils.TargetNumberTypeEnum.signedDouble);
break;
case GridDataEnum.lowerX:
case GridDataEnum.upperX:
case GridDataEnum.TransX:
case GridDataEnum.constY:
e.Handled = !HelperUtils.isValidNumber(curStr, e.KeyChar, HelperUtils.TargetNumberTypeEnum.unsignedDouble);
break;
}
}
Thanks
In case anyone else is looking for it I found the answer thanks to LarsTech.
private void SomeGridView_KeyPress(object sender, KeyPressEventArgs e)
{
DataGridView DGV = SomeGridView;
string curStr;
bool isFirst = DGV.CurrentCell.EditedFormattedValue == null;
curStr = isFirst ? "" : DGV.CurrentCell.EditedFormattedValue.ToString();
Type type = DGV.CurrentCell.GetType();
if (DGV.CurrentCell.GetType() == typeof(DataGridViewTextBoxCell))
{
DataGridViewTextBoxCell DGVTB = (DataGridViewTextBoxCell)DGV.CurrentCell;
if (DGV.CurrentCell.EditType == typeof(DataGridViewTextBoxEditingControl))
if(DGV.EditingControl != null)
charIndex = ((TextBox)DGV.EditingControl).SelectionStart;
}
switch ((GridDataEnum)DGV.CurrentCell.ColumnIndex)
{
case GridDataEnum.setpoint:
case GridDataEnum.SlopePoint:
e.Handled = !HelperUtils.isValidNumber(curStr, e.KeyChar, HelperUtils.TargetNumberTypeEnum.signedDouble, charIndex);
break;
case GridDataEnum.lowerX:
case GridDataEnum.upperX:
case GridDataEnum.TransX:
case GridDataEnum.constY:
e.Handled = !HelperUtils.isValidNumber(curStr, e.KeyChar, HelperUtils.TargetNumberTypeEnum.unsignedDouble, charIndex);
break;
}
}

How do you pause between cases in switch statements? c#. 3second delay before executing the next case

This is a sample of my code. I want to have a 3 second delay before executing the next case (display picturebox). But When I tried using timer/stopwatch, the 3 second delay will only be on the 1st case and case2 will execute the same time as case1 (with no delay).
private void button1_Click(object sender, EventArgs e)
{
string input = (input1.Text).ToString();
char[] letters = input.ToCharArray();
int stringlength = letters.Length;
int length = stringlength - 1;
int state = 1;
int position = 0;
string validation = " ";
switch (state)
{
case 1:
//insert timer here
if (letters[position] == 'a')
{
pictureBox2.Visible = true;
validation = "Invalid";
label1.Text = validation;
break;
}
else if (letters[position] == 'b')
{
if (position == length)
{
validation = "Invalid";
label1.Text = validation;
break;
}
pictureBox2.Visible = true;
position = position + 1;
goto case 2;
}
break;
case 2:
//Insert Timer here
if (letters[position] == 'a')
{
pictureBox3.Visible = true;
if (position == length)
{
validation = "Invalid because it does not end at final state";
label1.Text = validation;
break;
}
position = position + 1;
goto case 3;
}
else if (letters[position] == 'b')
{
if (position == length)
{
validation = "Invalid";
label1.Text = validation;
break;
}
position = position + 1;
goto case 4;
}
break;
}
}
By the way, I can't use Task.Delay or async/await because I use the .Net 4.0 framework.
You can perform this by using:
System.Threading.Thread.Sleep(3000);
So Your code should look like:
private void button1_Click(object sender, EventArgs e)
{
int state = 1;
//some code
switch (state)
{
case 1:
System.Threading.Thread.Sleep(3000);
//some case code
break;
case 2:
System.Threading.Thread.Sleep(3000);
//some case code
break;
default:
System.Threading.Thread.Sleep(3000);
//some default code
break;
}
//rest of the code
}
Just to let You know, there should be other (and better) way to going through this, rather than using switch and this type of GOTO case this and this. Making the code unreadable in future.
Thread.Sleep(3000) you can use

Radiobuttons, how to replace all those if statements

Several places in my program, the RadioButton matching the selected item has to be checked, and I have a lot of if statements like so:
DataRowView TempRow = (DataRowView)ScheduleDataGrid.SelectedItem;
if (Convert.ToString(TempRow["Bio"]) == "Bio1")
{
BioRB1.IsChecked = true;
}
if (Convert.ToString(TempRow["Bio"]) == "Bio2")
{
BioRB2.IsChecked = true;
}
if (Convert.ToString(TempRow["Bio"]) == "Bio3")
and so on... I want to replace all this with something short and smart.
I tried using the number of the bio to relate to the button like so:
string bioselected = Convert.ToString(TempRow["Bio"]);
int i = Convert.ToInt16(bioselected.Substring(bioselected.Length - 1, 1));
BioRB[i].IsChecked = true;
but doing a BioRB[i] doesn't work, it ignores the [i] and says BioRB does not exist. Any other suggestions?
BioRB[i] is not doing anything like what you think it's doing. All variable references (controls included) have to be well-defined at compile time - you can't refer to a control's name by building a string that matches the name.**
Try creating a list of your radio buttons. Then you can index into the list:
List<RadioButton> radioButtons = new List<RadioButton>()
{
BioRB1,
BioRB2
};
string bioselected = Convert.ToString(TempRow["Bio"]);
int i = Convert.ToInt16(bioselected.Substring(bioselected.Length - 1, 1));
radioButtons[i].IsChecked = true;
** Technically you can do this via reflection, but it's far more complex than what you've tried.
Maybe this will look better:
string caseSwitch = Convert.ToString(TempRow["Bio"]);
switch (caseSwitch)
{
case "Bio1":
BioRB1.IsChecked = true;
break;
case "Bio2":
BioRB2.IsChecked = true;
break;
case "Bio3":
BioRB3.IsChecked = true;
break;
default:
Console.WriteLine("Default case...is optional");
break;
}
Also, try doing what Alybaba726 said and use CellContentClick or something like this:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if(e.ColumnIndex == dgv.Columns["Bio"].Index)
{
string bioSelected = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
switch (bioSelected)
{
case "Bio1":
BioRB1.IsChecked = true;
break;
case "Bio2":
BioRB2.IsChecked = true;
break;
case "Bio3":
BioRB3.IsChecked = true;
break;
default:
Console.WriteLine("Default case...this is optional");
break;
}
}
}

Custom TextBox Control that Switches Keyboard Language Automatically c# WPF

i need to make something like that
http://www.codeproject.com/Articles/301832/Custom-Text-box-Control-that-Switch-keyboard-langu
but for WPF and C#
i'v tried to do it with a simple if statement but i was have to put another textbox like that
private void textBox2_TextChanged(object sender, TextChangedEventArgs e)
{
textBox1.Focus();
if (textBox1.Text == "Q" || textBox1.Text == "q")
{
textBox2.Text = textBox2.Text+ "ض";
textBox1.Text = "";
}
else if (textBox1.Text == "W" || textBox1.Text == "w")
{
textBox2.Text = textBox2.Text + "ص";
textBox1.Text = "";
}
// and so ..
}
it works but i want to do something like the link above
You can do that in WPF by creating a new custom Control that inherits TextBox. In that Create a new TextLanguage Property and Override the OnKeyDown method
namespace WpfApplication
{
public enum TextLanguage
{
English,
Arabic
}
public class CustomTextBox : TextBox
{
public TextLanguage TextLanguage { get; set; }
protected override void OnKeyDown(KeyEventArgs e)
{
if (TextLanguage != WpfApplication.TextLanguage.English)
{
e.Handled = true;
if (Keyboard.Modifiers == ModifierKeys.Shift)
{
// Shift key is down
switch (e.Key)
{
case Key.Q:
AddChars("ص");
break;
// Handle Other Cases too
default:
e.Handled = false;
break;
}
}
else if (Keyboard.Modifiers == ModifierKeys.None)
{
switch (e.Key)
{
case Key.Q:
AddChars("ض");
break;
// Handle Other Cases too
default:
e.Handled = false;
break;
}
}
}
base.OnKeyDown(e);
}
void AddChars(string str)
{
if (SelectedText.Length == 0)
AppendText(str);
else
SelectedText = str;
this.SelectionLength = 0;
this.CaretIndex = Text.Length;
}
}
}

Validate textbox on key down event to accept only numbers in silverlight 4

I need a validation to check whether key pressed down is numeric or not. I tried with different code but they cant help me out.In the textbox if the user press Shift+numbers it displays special characters like !,#,#...... I need to validate the Shift + key down event.
//Code
private void txtNumericTextbox_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.Key < Key.D0 || e.Key > Key.D9)
{
if (e.Key < Key.NumPad0 || e.Key > Key.NumPad9)
{
if (e.Key != Key.Back)
{
txtNumericTextbox_.BorderBrush = new SolidColorBrush(Colors.Red);
lblErrorMessage.Visibility = Visibility.Visible;
lblErrorMessage.Text = "Please Enter Numbers Only";
}
else
{
txtNumericTextbox_.BorderBrush = new SolidColorBrush(Colors.DarkGray);
lblErrorMessage.Visibility = Visibility.Collapsed;
lblErrorMessage.Text = "";
}
}
}
}
}
How can I achieve that?
You can use the ModifierKeys property on control to determine if the shift key is being held.
//Code
Use this to accept only numeric values.
Event you can choose textBox1_KeyDown nonnumberenter = false;
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
if (e.KeyCode != Keys.Back)
{
nonnumberenter = true;
string abc = "Please enter numbers only.";
DialogResult result1 = MessageBox.Show(abc.ToString(), "Validate numbers", MessageBoxButtons.OK);
}
}
}
if (Control.ModifierKeys == Keys.Shift)
{
nonnumberenter = true;
string abc = "Please enter numbers only.";
DialogResult result1 = MessageBox.Show(abc.ToString(), "Validate numbers", MessageBoxButtons.OK);
}
Use this to accept only characters. Event you can choose textBox1_KeyPress
if (Char.IsNumber(e.KeyChar) || Char.IsSymbol(e.KeyChar) || Char.IsWhiteSpace(e.KeyChar) || Char.IsPunctuation(e.KeyChar))
{
MessageBox.Show("Only Char are allowed");
e.Handled = true;
}
Hope this helps.
You can use text box key press event
private void txt_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) )
{
e.Handled = true;
}
}
I was looking for the same thing: Accept numbers only. However, I did not find any e.KeyCode on my _KeyDown event, so I adapted Kumar's code to suit my needs, and am sharing with you, should it be more fitting to you:
Use e.Handled = true to cancel the input of that character.
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.NumPad0:
case Key.NumPad1:
case Key.NumPad2:
case Key.NumPad3:
case Key.NumPad4:
case Key.NumPad5:
case Key.NumPad6:
case Key.NumPad7:
case Key.NumPad8:
case Key.NumPad9:
case Key.D0:
case Key.D1:
case Key.D2:
case Key.D3:
case Key.D4:
case Key.D5:
case Key.D6:
case Key.D7:
case Key.D8:
case Key.D9:
break;
default:
e.Handled = true;
break;
}
}

Categories

Resources