C#windows表示用于密码的datagridview键按事件 [英] C# windows forms datagridview key press event for password

查看:73
本文介绍了C#windows表示用于密码的datagridview键按事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



i在datagridview中输入了密码列的代码,但它无法正常工作。

我的代码:

Hello ,

i did the code for password columns in the datagridview but it is not working.
My code:

private void dgvCompanyUsers_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
       {


           if (dgvCompanyUsers.CurrentCell.OwningColumn.HeaderText == "Password")
           {
               DataGridViewTextBoxEditingControl textBox = e.Control as DataGridViewTextBoxEditingControl;
               if (textBox != null)
               {
                  // textBox.KeyPress -= new KeyPressEventHandler(Control_KeyPress);
                   textBox.KeyPress += new KeyPressEventHandler(Control_KeyPress);
               }
           }

   public void Control_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
       {
           //if (e.KeyChar >= '0' && e.KeyChar <= '9')
           //{
           //    e.Handled = true;
           //}
           TextBox s = ((TextBox)sender);
           if (s.ToString() != "")
           {
              // e.Handled = true;
               e.KeyChar = s.PasswordChar;


           }



如何在datagridview.how的密码列中设置(*)来写用于设置(*)的按键事件中的代码。



已添加代码块[/编辑]


how to set the (*) in the password column in the datagridview.how to write code in the keypress event for setting (*).

Code block added[/Edit]

推荐答案

我不确定WinForms DataGridView中ItemTemplate的可用性。

此外,我不确定KeyPress事件是否是此目的的最佳事件。尽管如此,



要将文本框的内容(在您的示例中名为s)转换为PasswordChar字符串,您可以使用以下代码:



I am not sure about availability of ItemTemplate in WinForms DataGridView.
Also, I am not sure about KeyPress Event being the best Event for this purpose. nevertheless,

To convert content of your text box (named ''s'' in your example) into a string of ''PasswordChar'', you can use the following code:

.
if (s.Text.Length > 0)
{
    string strToShow="";
    foreach(char ch in s.Text.ToCharArray())
    {
        strToShow+="*";
    }
}
s.Text=strToShow;





我希望你已存储了keyStroke的原始值,以便您可以使用原始密码而不是PasswordString。此外,在应用上述代码之前,请确保已处理BackSpace / Delete / Enter / UnwantedCharactersFromPasswordCriteria。



And I hope you had stored the original value of keyStroke somewhere so that you can have original password rather than this ''PasswordString''. Also, before applying the above code, make sure you have handled BackSpace/Delete/Enter/UnwantedCharactersFromPasswordCriteria .


这篇关于C#windows表示用于密码的datagridview键按事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆