How to get password as SecureString from DevExpress WinForms TextEdit - winforms

WPF has PasswordBox which receives password input into a SecureString, but I have a WinForms application. I can use SecurePasswordTextBox, but I want something that blends with DevExpress controls and DevExpress appearance customization. On the DevExpress TextEdit control you can set UseSystemPasswordChar = true, but it doesn't use SecureString (and won't: cf. 10/22/2010, cf. also.
How can I easily get SecureString support into a DevExpress WinForms TextEdit control?
I came up with something, which I post below as my own answer. Does anybody else have a solution?
Edit: I'm accepting my own answer, which works, because I need the DevExpress appearance.

Here is my own answer:
Instead of subclassing TextEdit, which seems too complicated, I just catch the KeyPress events and hide each pressed character away until the EditValueChanging event. I keep unique tokens in a fake string, so that I can figure out what to change in my SecureString on each EditValueChanging event.
Here is the proof-of-concept -- a simple form with a TextEdit control and two event handlers:
public partial class PasswordForm : DevExpress.XtraEditors.XtraForm
{
private Random random = new Random();
private HashSet<char> pool = new HashSet<char>();
private char secret;
private char token;
private List<char> fake = new List<char>();
public PasswordForm()
{
InitializeComponent();
this.Password = new SecureString();
for (int i = 0; i < 128; i++)
{
this.pool.Add((char)(' ' + i));
}
}
public SecureString Password { get; private set; }
private void textEditPassword_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
{
string value = e.NewValue as string;
// If any characters have been deleted...
foreach (char c in this.fake.ToArray())
{
if (value.IndexOf(c) == -1)
{
this.Password.RemoveAt(this.fake.IndexOf(c));
this.fake.Remove(c);
this.pool.Add(c);
}
}
// If a character is being added...
if (this.token != '\0')
{
int i = value.IndexOf(this.token);
this.Password.InsertAt(i, this.secret);
this.secret = '\0';
fake.Insert(i, this.token);
}
}
private void textEditPassword_KeyPress(object sender, KeyPressEventArgs e)
{
if (Char.IsControl(e.KeyChar))
{
this.token = '\0';
}
else
{
this.token = this.pool.ElementAt(random.Next(this.pool.Count)); // throws ArgumentOutOfRangeException when pool is empty
this.pool.Remove(this.token);
this.secret = e.KeyChar;
e.KeyChar = this.token;
}
}
}

The simplest way is to use the WPF PasswordBox hosted inside an ElementHost.
You can drag the ElementHost control from the ToolBox, or do the whole thing in code:
public Form1()
{
InitializeComponent();
System.Windows.Forms.Integration.ElementHost host = new System.Windows.Forms.Integration.ElementHost();
System.Windows.Controls.PasswordBox pb=new System.Windows.Controls.PasswordBox();
host.Child = pb;
this.Controls.Add(host);
}
Of course this does not use the DevExpress control, but I can't see any reason for using a Rich Text Editor as a password box when there is a simple alternative.

Related

WinForms Button: draw as the default button without setting the form's AcceptButton property (custom drawing, IsDefault property, BS_DEFPUSHBUTTON)

Imagine the following construction in WinForms .NET. A WinForms form contains a custom control with several buttons, which are instances of the traditional Button class. One of these buttons is the default button for the form. The custom control executes the action associated with the default button when ENTER is pressed. This is done in the redefined ProcessCmdKey method:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Return)
{
buttonOK_Click(null, EventArgs.Empty);
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
The default button must have an additional visual cue telling the user that this is the default button (an extra border inside the button). If we did this in a normal form, we would set its AcceptButton property. However, this approach is not applicable here. Even if we find the parent form using the Control.FindForm method or with an expression like (this.Parent as Form), we cannot set the AcceptButton property of the host form and then clear it the right way without resource leak or similar problems (a lot of technical details to place here and to bloat the question).
The first possible way to solve this task is to redefine or enhance the drawing of the button. Is there a relatively easy way to draw a button as the default button with the corresponding visual cue without implementing full custom painting? In my understanding, we might write a special class for our default button based on the following core:
internal class DefaultButton : Button
{
protected override void OnPaint(PaintEventArgs pevent)
{
Rectangle rc = new Rectangle(0, 0, this.Width, this.Height);
ButtonRenderer.DrawButton(pevent.Graphics, rc, System.Windows.Forms.VisualStyles.PushButtonState.Default);
}
}
However, it should take into account the focused state, whether another button on a form is focused (in this case the default button is not drawn with the visual cue), and the like. I could not find a good example of this to use as a basis for my development.
Another possible way to solve my problem could be setting the protected IsDefault property or/and specifying the BS_DEFPUSHBUTTON flag in the overridden CreateParams method in a class inherited from the Button class, for example:
internal class DefaultButton : Button
{
public DefaultButton() : base()
{
IsDefault = true;
}
protected override CreateParams CreateParams
{
get
{
const int BS_DEFPUSHBUTTON = 1;
CreateParams cp = base.CreateParams;
cp.Style |= BS_DEFPUSHBUTTON;
return cp;
}
}
}
But I could not make this code work. Buttons based on this class are always drawn as normal push buttons without the default button visual cue.
I'm not sure about the original requirement; for example I don't have any idea why a UserControl itself should set the AcceptButton of a Form, or what is the expected behavior if there are multiple instances of such controls on the form. It doesn't seem to be responsibility of the UserControl to set the AcceptButton of the Form and there might be better solutions, like relying on events and setting the AcceptButton.
Anyways, the following code example shows you how to set the AcceptButton of a Form; maybe it helps you to find a solutions. The highlights of the code:
The code uses dispose to set the AcceptButton to null.
The code implements ISupportInitialize to set the accept button after initialization of the control is done. If you create the control instance at run-time with code, don't forget to call EndInit, like this: ((System.ComponentModel.ISupportInitialize)(userControl11)).EndInit(); after adding it to the Form, but if you use designer, the designer will take care of that.
The code calls NotifyDefault(true) just for visual effect in design time when it's hosted on a form.
Here's the example:
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public class UserControl1 : UserControl, ISupportInitialize
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(15, 57);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(96, 57);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(15, 17);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 2;
//
// UserControl1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(236, 106);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
public System.Windows.Forms.Button button1;
public System.Windows.Forms.Button button2;
public UserControl1()
{
InitializeComponent();
//Just for visual effect in design time when it's hosted on a form
button2.NotifyDefault(true);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("1");
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("2");
}
public void BeginInit()
{
}
public void EndInit()
{
var f = this.FindForm();
if (f != null)
f.AcceptButton = button2;
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
if (disposing)
{
var f = this.FindForm();
if (f != null)
f.AcceptButton = null;
}
base.Dispose(disposing);
}
}
}

Is using an "Attached Property" a good practice for input capturing?

I am looking to find a generic way to support keyboard wedge scanning for my WPF TextBox controls.
(I am really a novice when it comes to more advanced WPF features, so I would like to ask if I am going in the right direction before I put a lot of time into research.)
What I am wanting to do is to add an Attached Property (or something) to my TextBoxes that will cause it to read all input into the box and then call a custom "ScanCompleted" command with the scanned input.
If an Attached Property is not a good fit for this, then is there a way to get this command on a TextBox without descending my own custom "ScanableTextBox"?
(Note: The criteria for a scan (instead of typed data) is that it will start with the Pause key (#19) and end with a Return key (#13).)
I think this could probably be accomplished with attached properties (behaviors), but would be much simpler and more straightforward to simply subclass TextBox and override the OnTextChanged, OnKeyDown, OnKeyUp and similar methods to add custom functionality.
Why don't you want to create your own control in this way?
update: Attached Behaviour
If you really don't want a derived control, here is an attached behaviour that accomplishes this (explanation below):
public class ScanReading
{
private static readonly IDictionary<TextBox, ScanInfo> TrackedTextBoxes = new Dictionary<TextBox, ScanInfo>();
public static readonly DependencyProperty ScanCompletedCommandProperty =
DependencyProperty.RegisterAttached("ScanCompletedCommand", typeof (ICommand), typeof (ScanReading),
new PropertyMetadata(default(ICommand), OnScanCompletedCommandChanged));
public static void SetScanCompletedCommand(TextBox textBox, ICommand value)
{
textBox.SetValue(ScanCompletedCommandProperty, value);
}
public static ICommand GetScanCompletedCommand(TextBox textBox)
{
return (ICommand) textBox.GetValue(ScanCompletedCommandProperty);
}
private static void OnScanCompletedCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var textBox = d as TextBox;
if (textBox == null)
return;
var command = (ICommand) e.NewValue;
if (command == null)
{
textBox.Unloaded -= OnTextBoxUnloaded;
textBox.KeyUp -= OnTextBoxKeyUp;
TrackedTextBoxes.Remove(textBox);
}
else
{
textBox.Unloaded += OnTextBoxUnloaded;
TrackedTextBoxes.Add(textBox, new ScanInfo(command));
textBox.KeyUp += OnTextBoxKeyUp;
}
}
static void OnTextBoxKeyUp(object sender, KeyEventArgs e)
{
var textBox = (TextBox) sender;
var scanInfo = TrackedTextBoxes[textBox];
if (scanInfo.IsTracking)
{
if (e.Key == Key.Return)
{
scanInfo.ScanCompletedCommand.Execute(textBox.Text);
scanInfo.IsTracking = false;
}
}
else if (string.IsNullOrEmpty(textBox.Text) && e.Key == Key.Pause)
{
TrackedTextBoxes[textBox].IsTracking = true;
}
}
static void OnTextBoxUnloaded(object sender, RoutedEventArgs e)
{
var textBox = (TextBox) sender;
textBox.KeyUp -= OnTextBoxKeyUp;
textBox.Unloaded -= OnTextBoxUnloaded;
TrackedTextBoxes.Remove(textBox);
}
}
public class ScanInfo
{
public ScanInfo(ICommand scanCompletedCommand)
{
ScanCompletedCommand = scanCompletedCommand;
}
public bool IsTracking { get; set; }
public ICommand ScanCompletedCommand { get; private set; }
}
Consume this by declaring a TextBox like so (where local is the namespace of your attached property, and ScanCompleted is an ICommand on your view-model):
<TextBox local:ScanReading.ScanCompletedCommand="{Binding ScanCompleted}" />
Now when this property is set, we add the TextBox to a static collection along with its associated ICommand.
Each time a key is pressed, we check whether it is the Pause key. If it is, and if the TextBox is empty, we set a flag to true to start looking for the Enter key.
Now each time a key is pressed, we check whether it is the Enter key. If it is, we execute the command, passing in the TextBox.Text value, and reset the flag to false for that TextBox.
We've also added a handler for the TextBox.Unloaded event to clean up our event subscriptions and remove the TextBox from the static list.

Keypad decimal separator on a Wpf TextBox, how to?

I have a Wpf application with some textbox for decimal input.
I would that when I press "dot" key (.) on numeric keypad of pc keyboard it send the correct decimal separator.
For example, on Italian language the decimal separator is "comma" (,)...Is possible set the "dot" key to send the "comma" character when pressed?
Quick and dirty:
private void NumericTextBox_KeyDown(object sender, KeyEventArgs e) {
if (e.Key == Key.Decimal) {
var txb = sender as TextBox;
int caretPos=txb.CaretIndex;
txb.Text = txb.Text.Insert(txb.CaretIndex, System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator);
txb.CaretIndex = caretPos + 1;
e.Handled = true;
}
}
Although you may set the default converter locale in WPF as suggested by Mamta Dalal it is not enough to convert the "decimal" key press to the correct string. This code will display the correct currency symbol and date/time format on data-bound controls
//Will set up correct string formats for data-bound controls,
// but will not replace numpad decimal key press
private void Application_Startup(object sender, StartupEventArgs e)
{
//Among other settings, this code may be used
CultureInfo ci = CultureInfo.CurrentUICulture;
try
{
//Override the default culture with something from app settings
ci = new CultureInfo([insert your preferred settings retrieval method here]);
}
catch { }
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
//Here is the important part for databinding default converters
FrameworkElement.LanguageProperty.OverrideMetadata(
typeof(FrameworkElement),
new FrameworkPropertyMetadata(
XmlLanguage.GetLanguage(ci.IetfLanguageTag)));
//Other initialization things
}
I found that handling the previewKeyDown event window-wide is a little cleaner than textbox-specific (it would be better if it could be applied application-wide).
public partial class MainWindow : Window
{
public MainWindow()
{
//Among other code
if (CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator != ".")
{
//Handler attach - will not be done if not needed
PreviewKeyDown += new KeyEventHandler(MainWindow_PreviewKeyDown);
}
}
void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Decimal)
{
e.Handled = true;
if (CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator.Length > 0)
{
Keyboard.FocusedElement.RaiseEvent(
new TextCompositionEventArgs(
InputManager.Current.PrimaryKeyboardDevice,
new TextComposition(InputManager.Current,
Keyboard.FocusedElement,
CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator)
) { RoutedEvent = TextCompositionManager.TextInputEvent});
}
}
}
}
If anybody could come up with a way to set it application-wide...

how to show Messagebox in MVVM [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How have you successfully implemented MessageBox.Show() functionality in MVVM?
I want to show message box in my MVVM WPF application.
so from where to call MessageBox.Show().
In Windows Forms, or WPF without MVVM, you could just say MessageBox.Show() and that was it! Wouldn't it be nice if you could do the same in MVVM?
Here is one way to do this - and it is as close as I can get to MessageBox.Show().
Here is the MVVM friendly MessageBox_Show()!
public class MyViewModel: ViewModelBase
{
protected void AskTheQuestion()
{
MessageBox_Show(ProcessTheAnswer, "Are you sure you want to do this?", "Alert", System.Windows.MessageBoxButton.YesNo);
}
public void ProcessTheAnswer(MessageBoxResult result)
{
if (result == MessageBoxResult.Yes)
{
// Do something
}
}
}
Tada!
Here is how it works:
All that MessageBox_Show actually does is fire an event, so it is perfectly MVVM friendly. The ViewModel knows nothing about any view that may or may not be consuming it, and it doesn't perform the showing of a Windows MessageBox on it's own, so it can also be safely unit tested.
To use it in a View, which will cause it to actually show a MessageBox, you just subscribe to the event and call e.Show() in the event handler, like this:
public partial class MyView : UserControl
{
public MyView()
{
InitializeComponent();
this.DataContext = new MyViewModel();
(this.DataContext as MyViewModel).MessageBoxRequest += new EventHandler<MvvmMessageBoxEventArgs>(MyView_MessageBoxRequest);
}
void MyView_MessageBoxRequest(object sender, MvvmMessageBoxEventArgs e)
{
e.Show();
}
}
And that is all you need to do to show MVVM friendly Windows MessageBoxes.
The code below only needs to be implemented once in your project, or you can put it in a reusable shared library.
Add this to your ViewModel base class so it can be used from any ViewModel:
public class ViewModelBase : INotifyPropertyChanged
{
//...
public event EventHandler<MvvmMessageBoxEventArgs> MessageBoxRequest;
protected void MessageBox_Show(Action<MessageBoxResult> resultAction, string messageBoxText, string caption = "", MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.None, MessageBoxOptions options = MessageBoxOptions.None)
{
if (this.MessageBoxRequest != null)
{
this.MessageBoxRequest(this, new MvvmMessageBoxEventArgs(resultAction, messageBoxText, caption, button, icon, defaultResult, options));
}
}
}
And then add the EventArgs class for the event handler:
public class MvvmMessageBoxEventArgs : EventArgs
{
public MvvmMessageBoxEventArgs(Action<MessageBoxResult> resultAction, string messageBoxText, string caption = "", MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.None, MessageBoxOptions options = MessageBoxOptions.None)
{
this.resultAction = resultAction;
this.messageBoxText = messageBoxText;
this.caption = caption;
this.button = button;
this.icon = icon;
this.defaultResult = defaultResult;
this.options = options;
}
Action<MessageBoxResult> resultAction;
string messageBoxText;
string caption;
MessageBoxButton button;
MessageBoxImage icon;
MessageBoxResult defaultResult;
MessageBoxOptions options;
public void Show(Window owner)
{
MessageBoxResult messageBoxResult = MessageBox.Show(owner, messageBoxText, caption, button, icon, defaultResult, options);
if (resultAction != null)resultAction(messageBoxResult);
}
public void Show()
{
MessageBoxResult messageBoxResult = MessageBox.Show(messageBoxText, caption, button, icon, defaultResult, options);
if (resultAction != null) resultAction(messageBoxResult);
}
}
Unit testing is easy:
target.AskTheQuestion();
target.ProcessTheAnswer(System.Windows.MessageBoxResult.Yes);
Happy Coding!
I've found that MVVM invokes a streak of OCD in programmers (I know from experience). That's a good thing. But for certain things the effort just isn't worth it, especially if it introduces an entire order of complexity just to ask the user "Are you sure you wish to xxxx?"
My opinion is that MessageBox.Show() may be called from the code-behind, but never the ViewModel. Until dialog boxes integrate better with XAML, just take the hit and don't feel bad about it. It's really a gray area of the current state of WPF UI design.

Scrolling text using a toolstriplabel on a c# winform

I am currently working on a little app that scrolls a message across the top of the form - nothing complex however i have ran into a issue where i can not get it to work with a toolstriplabel on my c# winform. I currently have it working by the following method using a normal label but toolstriplabels dont appear to have the .Left option i require to make it scroll. This is the code i am currently using in a timer.
private void timer1_Tick(object
sender, System.EventArgs e)
{
this.label1.Left = this.label1.Left - 1;
if (this.label1.Left + this.label1.Width < 0)
{
this.label1.Left = this.label1.Width;
}
}
Does anyone know how i can make this work with a toolstrip label as i would really like this scrolling text on a toolstrip so the user can drag it to where there require?
Thanks
How about something like this:
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
string _labelText = "Hello out there!";
int _scrollOffset = 0;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick( object sender, EventArgs e )
{
string textToDisplay = _labelText.Substring( _scrollOffset++ );
this.toolStripLabel1.Text = textToDisplay;
if ( _scrollOffset > _labelText.Length )
{
_scrollOffset = 0;
}
}
}
}

Resources