I have written bunch of switch case statement, but I know this can be simplified, any guidance will be respected. I am fairly new to XAML.
switch (e.Key)
{
case Key.Escape:
this.DialogResult = false;
break;
case Key.Return:
this.DialogResult = true;
break;
case Key.Back:
if (ResultValue != null && ResultValue.Length > 0)
ResultValue = ResultValue.Remove(ResultValue.Length - 1);
if (isUserAccess)
{
if (UserAccessPasswordValue != null && UserAccessPasswordValue.Length > 0)
UserAccessPasswordValue = UserAccessPasswordValue.Remove(UserAccessPasswordValue.Length - 1);
}
break;
case Key.Space:
if (!CheckOutputLength(ResultValue)) return;
ResultValue += " ";
break;
case Key.NumPad0:
case Key.D0:
case Key.NumPad1:
case Key.D1:
case Key.NumPad2:
case Key.D2:
case Key.NumPad3:
case Key.D3:
case Key.NumPad4:
case Key.D4:
case Key.NumPad5:
case Key.D5:
case Key.NumPad6:
case Key.D6:
case Key.NumPad7:
case Key.D7:
case Key.NumPad8:
case Key.D8:
case Key.NumPad9:
case Key.D9:
case Key.A:
case Key.B:
case Key.C:
case Key.D:
case Key.E:
case Key.F:
case Key.G:
case Key.H:
case Key.I:
case Key.J:
case Key.K:
case Key.L:
case Key.M:
case Key.N:
case Key.O:
case Key.P:
case Key.Q:
case Key.R:
case Key.S:
case Key.T:
case Key.U:
case Key.V:
case Key.W:
case Key.X:
case Key.Y:
case Key.Z:
CheckandAddValue(e.Key.ToString());
break;
}
My attempt has too many errors. This is a custom key board and detects the user inputs. The above code works and I am able to get the required outcome but i know this can be made simpler.
My simplified attempt
if ((e.Key >= Key.A && e.Key <= Key.Z) || (e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9))
Handle case which you want else default case..
switch (e.Key)
{
case Key.Escape:
this.DialogResult = false;
break;
case Key.Return:
this.DialogResult = true;
break;
case Key.Back:
if (ResultValue != null && ResultValue.Length > 0)
ResultValue = ResultValue.Remove(ResultValue.Length - 1);
if (isUserAccess)
{
if (UserAccessPasswordValue != null && UserAccessPasswordValue.Length > 0)
UserAccessPasswordValue = UserAccessPasswordValue.Remove(UserAccessPasswordValue.Length - 1);
}
break;
case Key.Space:
if (!CheckOutputLength(ResultValue)) return;
ResultValue += " ";
break;
case default :
CheckandAddValue(e.Key.ToString());
break;
}
You can use C#7 pattern-matching in Visual Studion 2017 and 2015 :
switch (e.Key)
{
case Key.Escape:
this.DialogResult = false;
break;
case Key.Return:
this.DialogResult = true;
break;
case Key.Back:
if (!string.IsNullOrEmpty(ResultValue))
ResultValue = ResultValue.Remove(ResultValue.Length - 1);
if (isUserAccess)
{
if (!string.IsNullOrEmpty(UserAccessPasswordValue))
UserAccessPasswordValue = UserAccessPasswordValue.Remove(UserAccessPasswordValue.Length - 1);
}
break;
case Key.Space:
if (!CheckOutputLength(ResultValue)) return;
ResultValue += " ";
break;
case var k when k >= Key.D0 && k <= Key.NumPad9
&& !(k >= Key.LWin && k <= Key.Sleep):
CheckandAddValue(e.Key.ToString());
break;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.