C# 中怎么将propertyGrid控件中的内容加密?

C# 怎么将PropertyGrid控件中输入内容加密?(PropertyGrid控件的内容是读一个自定义类中的属性,属性内容可以改变)

img

找到了在属性上面设置 [PasswordPropertyText(true)]

设置为 private 属性

  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/234961
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:C# PropertyGrid控件的四个自定义操作
  • 除此之外, 这篇博客: c# winform PropertyGrid控件 为指定属性增加自定义控件中的 2.创建编辑器 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  •     /// <summary>
        /// 
        /// </summary>
        public class Editor : UITypeEditor
        {
            /// <summary>
            /// 更改属性值编辑样式(增加省略号按钮)
            /// </summary>
            /// <param name="context"></param>
            /// <returns></returns>
            public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
            {
                return UITypeEditorEditStyle.Modal;
            }
    
            /// <summary>
            /// 增加自定义用户控件
            /// </summary>
            /// <param name="context"></param>
            /// <param name="provider"></param>
            /// <param name="value"></param>
            /// <returns></returns>
            public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
            {
                var formES = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
                if (formES != null)
                {
                    var popedControl = new PropertyGridMoreControl(value);
                    //弹出窗口
                    //formES.ShowDialog(窗口/对话框);
                    //下拉框
                    formES.DropDownControl(popedControl);
                }
    
                return base.EditValue(context, provider, value);
            }
        }
    
  • 以下回答由chatgpt基于相关博客总结生成:

    要在C#中使用propertyGrid控件时将内容加密,可以采取以下步骤:

    1. 创建一个自定义的TypeConverter类,实现属性值的加密和解密。下面是一个示例代码:
    public class EncryptedStringConverter : TypeConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return destinationType == typeof(string);
        }
    
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            // 将属性值转换为加密的字符串
            string encryptedValue = Encrypt((string)value);
            return encryptedValue;
        }
    
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(string);
        }
    
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            // 将加密的字符串解密为原来的属性值
            string encryptedValue = (string)value;
            string decryptedValue = Decrypt(encryptedValue);
            return decryptedValue;
        }
    
        // 编写加密方法和解密方法
        private string Encrypt(string value)
        {
            // 加密逻辑
        }
    
        private string Decrypt(string encryptedValue)
        {
            // 解密逻辑
        }
    }
    
    1. 在需要加密的属性上方使用[TypeConverter]属性,指定使用自定义的TypeConverter类。例如,在Student类中的Name属性上方添加[TypeConverter(typeof(EncryptedStringConverter))]。
    public class Student
    {
        private string name = "Tom";
    
        [TypeConverter(typeof(EncryptedStringConverter))]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    
        // 其他属性...
    }
    

    这样,当在propertyGrid控件中编辑该属性时,属性值会自动加密和解密。

    希望以上解决方案对你有帮助!如果还有其他问题,请随时提问。