Eigenes Usercontrol / eigene Properties

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 11 Antworten in diesem Thema. Der letzte Beitrag () ist von BigBen2003.

    Eigenes Usercontrol / eigene Properties

    Hallo,

    in VB.Net wurde erfolgreich ein DataGridView UserControl erstellt.

    Im Userform-Designer von Visual Studio 2015 werden die Benutzer-Properties korrekt angezeigt und können auch korrekt mit Inhalten im Designer.

    Beim Ausführen der Anwendung werden allerdings immer die vorgenommenen Einstellungen im Designer durch die Standard-Einstellungen überschrieben.

    Hat jemand einen Tip, wie die Fehlerursache eingegrenzt werden kann?

    Ich habe eine Vermutung, dass es am Initialisieren des Controls liegt, bei dem die Standard-Einstellungen gesetzt werden.
    Die Standardvorgaben werden nur an einer Stelle in der Cell-Klasse vorgegeben.


    Hier der Code vom UserControl:

    Klasse DataGridViewEncryptPasswordTextBoxCell:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Option Explicit On
    2. Option Strict On
    3. Imports System.ComponentModel
    4. Public Class DataGridViewEncryptPasswordTextBoxCell
    5. Inherits System.Windows.Forms.DataGridViewTextBoxCell
    6. Private myTripleDES As New TripleDES
    7. Private _encryptPassword As Boolean
    8. Private _initVector As Byte()
    9. Private _passwordChar As Char
    10. Private _useSystemPasswordChar As Boolean
    11. Private editingControlPasswordChar As Char
    12. Private editingControlUseSystemPasswordChar As Boolean
    13. ' Button im Textbox rechts einblenden, wenn der Wert bearbeitet wird
    14. 'Private WithEvents _Button As ToggleButtonShowHidePassword
    15. Private WithEvents _Button As Button
    16. Private WithEvents _Textbox As TextBox
    17. Sub New()
    18. MyBase.New
    19. Me._encryptPassword = True
    20. Me._initVector = Nothing
    21. Me._passwordChar = CChar("")
    22. Me._useSystemPasswordChar = True
    23. End Sub
    24. <Description("Soll das eingegebene Passwort mit dem TripleDES-Verfahren verschlüsselt abgespeichert werden?")>
    25. <DefaultValue(True)>
    26. Public Property encryptPassword() As Boolean
    27. Get
    28. Return Me._encryptPassword
    29. End Get
    30. Set(value As Boolean)
    31. Me._encryptPassword = value
    32. End Set
    33. End Property
    34. <Description("InitVector muss 8 Bytes beinhalten, die durch Kommatas voneinander getrennt sind. Beispiel: 65, 110, 68, 26, 69, 178, 200, 219")>
    35. <DefaultValue("")>
    36. Public Property initVector As String
    37. Get
    38. If Me._initVector IsNot Nothing Then
    39. Return String.Join(",", Me._initVector)
    40. Else
    41. Return Nothing
    42. End If
    43. End Get
    44. Set(value As String)
    45. Dim IsValid As Boolean
    46. Dim myItems = myTripleDES.ConvertToBytes(value, 8, IsValid)
    47. If IsValid Then
    48. Me._initVector = myItems
    49. End If
    50. End Set
    51. End Property
    52. <Description("Passwort-Maskierungszeichen - Es kann nur ein Zeichen eingegeben werden!")>
    53. <DefaultValue("")>
    54. Public Property PasswordChar() As Char
    55. Get
    56. Return Me._passwordChar
    57. End Get
    58. Set(ByVal value As Char)
    59. Me._passwordChar = value
    60. End Set
    61. End Property
    62. <Description("Passwort maskiert anzeigen? True = Passwort wird maskiert angezeigt; False = Passwort wird in Klartext angezeigt")>
    63. <DefaultValue(True)>
    64. Public Property UseSystemPasswordChar() As Boolean
    65. Get
    66. Return Me._useSystemPasswordChar
    67. End Get
    68. Set(ByVal value As Boolean)
    69. Me._useSystemPasswordChar = value
    70. End Set
    71. End Property
    72. Public Overrides Function Clone() As Object
    73. Dim copy As DataGridViewEncryptPasswordTextBoxCell = DirectCast(MyBase.Clone(), DataGridViewEncryptPasswordTextBoxCell)
    74. copy.PasswordChar = Me._passwordChar
    75. copy.UseSystemPasswordChar = Me._useSystemPasswordChar
    76. copy.encryptPassword = Me._encryptPassword
    77. If Me._initVector IsNot Nothing Then
    78. copy.initVector = String.Join(",", Me._initVector)
    79. Else
    80. copy.initVector = ""
    81. End If
    82. Return copy
    83. End Function
    84. Protected Overrides Function GetFormattedValue(ByVal value As Object,
    85. ByVal rowIndex As Integer,
    86. ByRef cellStyle As System.Windows.Forms.DataGridViewCellStyle,
    87. ByVal valueTypeConverter As System.ComponentModel.TypeConverter,
    88. ByVal formattedValueTypeConverter As System.ComponentModel.TypeConverter,
    89. ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object
    90. Dim formattedValue As Object
    91. If Me._encryptPassword AndAlso value IsNot Nothing Then
    92. value = myTripleDES.DecryptText(CType(value, String), Me._initVector)
    93. End If
    94. If Me._useSystemPasswordChar AndAlso value IsNot Nothing Then
    95. 'Display the system password character in place of each actual character.
    96. 'TODO: Determine the actual system password character instead of hard-coding this value.
    97. formattedValue = New String(Convert.ToChar(&H25CF), CStr(value).Length)
    98. ElseIf Me._passwordChar <> Char.MinValue AndAlso value IsNot Nothing Then
    99. 'Display the user-defined password character in place of each actual character.
    100. formattedValue = New String(Me._passwordChar, CStr(value).Length)
    101. Else
    102. 'Display the value as is.
    103. formattedValue = MyBase.GetFormattedValue(value,
    104. rowIndex,
    105. cellStyle,
    106. valueTypeConverter,
    107. formattedValueTypeConverter,
    108. context)
    109. End If
    110. Return formattedValue
    111. End Function
    112. Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer,
    113. ByVal initialFormattedValue As Object,
    114. ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle)
    115. MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
    116. Dim _TextBox As TextBox = DirectCast(Me.DataGridView.EditingControl, TextBox)
    117. With _TextBox
    118. 'Remember the current password properties of the editing control.
    119. Me.editingControlPasswordChar = .PasswordChar
    120. Me.editingControlUseSystemPasswordChar = .UseSystemPasswordChar
    121. 'Set the new password properties of the editing control.
    122. .PasswordChar = Me._passwordChar
    123. .UseSystemPasswordChar = Me._useSystemPasswordChar
    124. ' Button rechts einblenden
    125. '_Button = New ToggleButtonShowHidePassword ' Button
    126. _Button = New Button
    127. _Button.Text = "..." ' ToDo: Umschalter für Sichtbar und unsichtbare Passwörter erstellen
    128. _Button.Dock = DockStyle.Right
    129. _Button.Cursor = Cursors.Hand
    130. _Button.Width = 20
    131. _Button.Visible = False
    132. .Controls.Add(_Button)
    133. AddHandler _TextBox.Enter, AddressOf Textbox_OnEnter
    134. 'AddHandler _TextBox.Enter, Function(sender, e) Textbox_OnEnter(myCell)
    135. AddHandler _TextBox.Leave, AddressOf Textbox_OnLeave
    136. End With
    137. End Sub
    138. ''' <summary>
    139. ''' Einblenden des Toggle-Buttons Show/Hide Password
    140. ''' </summary>
    141. ''' <param name="sender"></param>
    142. ''' <param name="e"></param>
    143. Protected Sub Textbox_OnEnter(sender As Object, e As EventArgs)
    144. 'Dim myTxtBx As TextBox = DirectCast(sender, TextBox)
    145. 'Dim myGridView As DataGridView = CType(myTxtBx.Parent.Parent, DataGridView)
    146. 'Dim myCell = myGridView.CurrentCell
    147. _Button.Visible = True
    148. End Sub
    149. ''' <summary>
    150. ''' Ausblenden des Toggle-Buttons Show/Hide Password
    151. ''' </summary>
    152. ''' <param name="sender"></param>
    153. ''' <param name="e"></param>
    154. Protected Sub Textbox_OnLeave(sender As Object, e As EventArgs)
    155. _Button.Visible = False
    156. End Sub
    157. ''' <summary>
    158. ''' Bei Bedarf wird der eingegebene Text vor einer Übernahme verschlüsselt
    159. ''' </summary>
    160. ''' <param name="rowIndex">Index im DataGridView</param>
    161. ''' <param name="Value">zu übernehmender Wert</param>
    162. ''' <returns>True oder False</returns>
    163. Protected Overrides Function SetValue(rowIndex As Integer, Value As Object) As Boolean
    164. If Me._encryptPassword AndAlso Value IsNot Nothing Then
    165. Value = myTripleDES.EncryptText(CType(Value, String), Me._initVector)
    166. End If
    167. Return MyBase.SetValue(rowIndex, Value)
    168. End Function
    169. Public Overrides Sub DetachEditingControl()
    170. MyBase.DetachEditingControl()
    171. With DirectCast(Me.DataGridView.EditingControl, TextBox)
    172. 'Reset the old password properties of the editing control.
    173. .PasswordChar = Me.editingControlPasswordChar
    174. .UseSystemPasswordChar = Me.editingControlUseSystemPasswordChar
    175. End With
    176. End Sub
    177. Private Sub _Button_Click(sender As Object, e As EventArgs) Handles _Button.Click
    178. 'Me.UseSystemPasswordChar = Not Me.UseSystemPasswordChar
    179. End Sub
    180. End Class



    Klasse DataGridViewEncryptPasswordTextBoxColumn:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Option Explicit On
    2. Option Strict On
    3. Imports System.ComponentModel
    4. Public Class DataGridViewEncryptPasswordTextBoxColumn
    5. Inherits System.Windows.Forms.DataGridViewColumn
    6. Private myTripleDES As New TripleDES
    7. Private _encryptPassword As Boolean
    8. Private _initVector As String = ""
    9. Private _passwordChar As Char
    10. Private _useSystemPasswordChar As Boolean
    11. <Category("Passwort")>
    12. Public Property PasswordChar() As Char
    13. Get
    14. Return Me._passwordChar
    15. End Get
    16. Set(ByVal value As Char)
    17. If Me._passwordChar <> value Then
    18. Me._passwordChar = value
    19. Dim cell As DataGridViewEncryptPasswordTextBoxCell = TryCast(Me.CellTemplate, DataGridViewEncryptPasswordTextBoxCell)
    20. If cell IsNot Nothing Then
    21. 'Update the template cell.
    22. cell.PasswordChar = value
    23. End If
    24. If Me.DataGridView IsNot Nothing Then
    25. 'Update each existing cell in the column.
    26. For Each row As DataGridViewRow In Me.DataGridView.Rows
    27. cell = TryCast(row.Cells(Me.Index), DataGridViewEncryptPasswordTextBoxCell)
    28. If cell IsNot Nothing Then
    29. cell.PasswordChar = value
    30. End If
    31. Next
    32. 'Force a repaint so the grid reflects the current property value.
    33. Me.DataGridView.Refresh()
    34. End If
    35. End If
    36. End Set
    37. End Property
    38. <Category("Passwort Verschlüsselung")>
    39. Public Property initVector As String
    40. Get
    41. If Me._initVector IsNot Nothing Then
    42. Return String.Join(",", Me._initVector)
    43. Else
    44. Return Nothing
    45. End If
    46. End Get
    47. Set(value As String)
    48. If value Is Nothing Then
    49. value = ""
    50. End If
    51. Dim IsValid As Boolean
    52. Dim myByte = myTripleDES.ConvertToBytes(value:=value, validCountItems:=8, IsValid:=IsValid)
    53. If IsValid Then
    54. If Not Me._initVector.Equals(value) Then
    55. Me._initVector = value
    56. Dim cell As DataGridViewEncryptPasswordTextBoxCell = TryCast(Me.CellTemplate, DataGridViewEncryptPasswordTextBoxCell)
    57. If cell IsNot Nothing Then
    58. 'Update the template cell.
    59. cell.initVector = value
    60. End If
    61. If Me.DataGridView IsNot Nothing Then
    62. 'Update each existing cell in the column.
    63. For Each row As DataGridViewRow In Me.DataGridView.Rows
    64. cell = TryCast(row.Cells(Me.Index), DataGridViewEncryptPasswordTextBoxCell)
    65. If cell IsNot Nothing Then
    66. cell.initVector = value
    67. End If
    68. Next
    69. 'Force a repaint so the grid reflects the current property value.
    70. Me.DataGridView.Refresh()
    71. End If
    72. End If
    73. End If
    74. End Set
    75. End Property
    76. <Category("Passwort Verschlüsselung")>
    77. Public Property EncryptPassword() As Boolean
    78. Get
    79. Return Me._encryptPassword
    80. End Get
    81. Set(value As Boolean)
    82. If Me._encryptPassword <> value Then
    83. Me._encryptPassword = value
    84. Dim cell As DataGridViewEncryptPasswordTextBoxCell = TryCast(Me.CellTemplate, DataGridViewEncryptPasswordTextBoxCell)
    85. If cell IsNot Nothing Then
    86. 'Update the template cell.
    87. cell.encryptPassword = value
    88. End If
    89. If Me.DataGridView IsNot Nothing Then
    90. 'Update each existing cell in the column.
    91. For Each row As DataGridViewRow In Me.DataGridView.Rows
    92. cell = TryCast(row.Cells(Me.Index), DataGridViewEncryptPasswordTextBoxCell)
    93. If cell IsNot Nothing Then
    94. cell.encryptPassword = value
    95. End If
    96. Next
    97. 'Force a repaint so the grid reflects the current property value.
    98. Me.DataGridView.Refresh()
    99. End If
    100. End If
    101. End Set
    102. End Property
    103. <Category("Passwort")>
    104. Public Property UseSystemPasswordChar() As Boolean
    105. Get
    106. Return Me._useSystemPasswordChar
    107. End Get
    108. Set(ByVal value As Boolean)
    109. If Me._useSystemPasswordChar <> value Then
    110. Me._useSystemPasswordChar = value
    111. Dim cell As DataGridViewEncryptPasswordTextBoxCell = TryCast(Me.CellTemplate, DataGridViewEncryptPasswordTextBoxCell)
    112. If cell IsNot Nothing Then
    113. 'Update the template cell.
    114. cell.UseSystemPasswordChar = value
    115. End If
    116. If Me.DataGridView IsNot Nothing Then
    117. 'Update each existing cell in the column.
    118. For Each row As DataGridViewRow In Me.DataGridView.Rows
    119. cell = TryCast(row.Cells(Me.Index), DataGridViewEncryptPasswordTextBoxCell)
    120. If cell IsNot Nothing Then
    121. cell.UseSystemPasswordChar = value
    122. End If
    123. Next
    124. 'Force a repaint so the grid reflects the current property value.
    125. Me.DataGridView.Refresh()
    126. End If
    127. End If
    128. End Set
    129. End Property
    130. Public Sub New()
    131. MyBase.New(New DataGridViewEncryptPasswordTextBoxCell)
    132. End Sub
    133. Public Overrides Function Clone() As Object
    134. If MyBase.Clone() IsNot Nothing Then
    135. Dim copy As DataGridViewEncryptPasswordTextBoxColumn = DirectCast(MyBase.Clone(), DataGridViewEncryptPasswordTextBoxColumn)
    136. copy.PasswordChar = Me._passwordChar
    137. copy.UseSystemPasswordChar = Me._useSystemPasswordChar
    138. copy.EncryptPassword = Me._encryptPassword
    139. Return copy
    140. Else
    141. Dim myCp As New DataGridViewEncryptPasswordTextBoxColumn
    142. Return myCp
    143. End If
    144. End Function
    145. End Class



    Im Code wird zusätzlich auf eine Klasse TripleDES zugegriffen:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Option Explicit On
    2. Option Strict On
    3. Imports System.IO
    4. Imports System.Text
    5. Imports System.Security.Cryptography
    6. Public Class TripleDES : Implements IFormatProvider, ICustomFormatter
    7. Private key() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
    8. 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}
    9. Private iv() As Byte = Array.ConvertAll(My.Resources.initVector.Split(CType(",", Char)), Function(str) Byte.Parse(CType(str, String))) '{65, 110, 68, 26, 69, 178, 200, 219}
    10. Public Function Format(fmt As String, arg As Object, formatProvider As IFormatProvider) As String Implements ICustomFormatter.Format
    11. If TypeOf arg Is String Then
    12. Return TripleDES.DecryptText(CType(arg, String))
    13. Else
    14. Return Nothing
    15. End If
    16. End Function
    17. Public Function GetFormat(formatType As Type) As Object Implements IFormatProvider.GetFormat
    18. If formatType Is GetType(ICustomFormatter) Then
    19. Return Me
    20. Else
    21. Return Nothing
    22. End If
    23. End Function
    24. ''' <summary>
    25. ''' Chiffre-Key
    26. ''' </summary>
    27. ''' <returns></returns>
    28. Public Property ChiffKey As Byte()
    29. Get
    30. Return key
    31. End Get
    32. Set(value As Byte())
    33. If Not (value.Count = 24 And value.Min = 1 And value.Max = 24) Then
    34. Throw New ArgumentException("Das Byte-Array darf ausschließlich eine Größe von 24 Einträge (Min=1, Max=24) umfassen!", "value", Nothing)
    35. End If
    36. key = value
    37. End Set
    38. End Property
    39. ''' <summary>
    40. ''' Initialisierungs-Vector (8 Byte-Array)
    41. ''' Mehr Angaben werden nicht berücksichtigt
    42. ''' </summary>
    43. ''' <returns></returns>
    44. Public Property InitVector As Byte()
    45. Get
    46. Return iv
    47. End Get
    48. Set(value As Byte())
    49. If Not (value.Count = 8 And value.Min = 1 And value.Max = 8) Then
    50. Throw New ArgumentException("Das Byte-Array darf ausschließlich eine Größe von 8 Einträgen (Min=1, Max=8) umfassen", "value", Nothing)
    51. Else
    52. iv = value
    53. End If
    54. End Set
    55. End Property
    56. ''' <summary>
    57. ''' Prüft eine Eingabe, ob diese in ein 8-Byte Array konvertiert werden kann
    58. ''' </summary>
    59. ''' <param name="value">zu prüfender Inhalt</param>
    60. ''' <param name="IsValid">Gibt True oder False zurück</param>
    61. ''' <returns>Byte-Array</returns>
    62. ''' <remarks>
    63. ''' Es werden nur 8-Byte oder leere Angaben erlaubt
    64. ''' </remarks>
    65. Public Function ConvertToBytes(ByVal value As String, ByVal validCountItems As Integer, ByRef IsValid As Boolean) As Byte()
    66. If value.IndexOf(",") > 0 Then
    67. Dim myItems = value.Split(CType(",", Char))
    68. If myItems.Count = validCountItems Then
    69. IsValid = True
    70. For iPos = 0 To myItems.Count - 1
    71. Dim myItem = myItems(iPos)
    72. Dim byteValue As Byte
    73. If Not Byte.TryParse(myItem, byteValue) Then
    74. IsValid = False
    75. Exit For
    76. End If
    77. Next
    78. If IsValid Then
    79. Return Array.ConvertAll(myItems, Function(str) Byte.Parse(CType(str, String)))
    80. End If
    81. Else
    82. IsValid = False
    83. End If
    84. Else
    85. ' Leere Eingabe ist aktzeptabel
    86. If value = "" Then
    87. IsValid = True
    88. End If
    89. End If
    90. Return Nothing
    91. End Function
    92. ''' <summary>
    93. ''' Verschüsselung von Inhalten
    94. ''' </summary>
    95. ''' <param name="plainText">Zu verschlüsselnder Text</param>
    96. ''' <returns></returns>
    97. Public Function Encrypt(ByVal plainText As String, Optional myIV As Byte() = Nothing) As Byte()
    98. Try
    99. ' Declare a UTF8Encoding object so we may use the GetByte
    100. ' method to transform the plainText into a Byte array.
    101. Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
    102. Dim inputInBytes() As Byte = utf8encoder.GetBytes(plainText)
    103. ' Create a new TripleDES service provider
    104. Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
    105. ' The ICryptTransform interface uses the TripleDES
    106. ' crypt provider along with encryption key and init vector
    107. ' information
    108. Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateEncryptor(Me.key, CType(IIf(myIV Is Nothing, Me.iv, myIV), Byte()))
    109. ' All cryptographic functions need a stream to output the
    110. ' encrypted information. Here we declare a memory stream
    111. ' for this purpose.
    112. Dim encryptedStream As MemoryStream = New MemoryStream()
    113. Dim cryptStream As CryptoStream = New CryptoStream(encryptedStream,
    114. cryptoTransform, CryptoStreamMode.Write)
    115. ' Write the encrypted information to the stream. Flush the information
    116. ' when done to ensure everything is out of the buffer.
    117. cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
    118. cryptStream.FlushFinalBlock()
    119. encryptedStream.Position = 0
    120. ' Read the stream back into a Byte array and return it to the calling
    121. ' method.
    122. Dim result(CInt(encryptedStream.Length - 1)) As Byte
    123. encryptedStream.Read(result, 0, CInt(encryptedStream.Length))
    124. cryptStream.Close()
    125. Return result
    126. Catch ex As Exception
    127. Return Nothing
    128. End Try
    129. End Function
    130. ''' <summary>
    131. ''' Zu entschlüsselnder Text
    132. ''' </summary>
    133. ''' <param name="inputInBytes">Verschlüsselter Inhalt als Byte-Array</param>
    134. ''' <returns></returns>
    135. Public Function Decrypt(ByVal inputInBytes() As Byte, Optional myIV As Byte() = Nothing) As String
    136. Try
    137. If inputInBytes IsNot Nothing Then
    138. ' UTFEncoding is used to transform the decrypted Byte Array
    139. ' information back into a string.
    140. Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
    141. Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
    142. ' As before we must provide the encryption/decryption key along with
    143. ' the init vector.
    144. Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateDecryptor(Me.key, CType(IIf(myIV Is Nothing, Me.iv, myIV), Byte()))
    145. ' Provide a memory stream to decrypt information into
    146. Dim decryptedStream As MemoryStream = New MemoryStream()
    147. Dim cryptStream As CryptoStream = New CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write)
    148. cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
    149. cryptStream.FlushFinalBlock()
    150. decryptedStream.Position = 0
    151. ' Read the memory stream and convert it back into a string
    152. Dim result(CInt(decryptedStream.Length - 1)) As Byte
    153. decryptedStream.Read(result, 0, CInt(decryptedStream.Length))
    154. cryptStream.Close()
    155. Dim myutf As UTF8Encoding = New UTF8Encoding()
    156. Return myutf.GetString(result)
    157. Else
    158. Return Nothing
    159. End If
    160. Catch ex As Exception
    161. Return Nothing
    162. End Try
    163. End Function
    164. ''' <summary>
    165. ''' Entschlüsselung von Textinhalten
    166. ''' </summary>
    167. ''' <param name="value">Verschlüsselter Inhalt</param>
    168. ''' <param name="initVector">Initialisierungsvektor</param>
    169. ''' <returns>
    170. ''' Entschlüsselter Text
    171. ''' Falls der verschlüsselte Inhalt nicht entschlüsselt werden kann, wird der Wert von value unverändert zurückgegeben</returns>
    172. ''' <remarks>
    173. ''' Über den Initialisierungsvector wird die Verschlüsselung festgelegt
    174. ''' Dieser muss beim Verschlüsseln und Entschlüsseln identisch sein.
    175. ''' </remarks>
    176. Friend Function DecryptText(ByRef value As String, initVector As Byte()) As String
    177. Dim myDES As New TripleDES
    178. value = CType(IIf(value Is Nothing, "", value), String)
    179. If value.Length > 0 Then
    180. Dim pwDecryptByte As Byte() = Nothing
    181. Try
    182. ' Testen, ob Inhalt vollständig in Byte konvertiert werden kann...
    183. Dim bReadyToConvert As Boolean = True
    184. For Each valByte In value.Split(CType(",", Char()))
    185. Dim result As Byte
    186. If Not Byte.TryParse(valByte, result) Then
    187. bReadyToConvert = False
    188. Exit For
    189. End If
    190. Next
    191. If bReadyToConvert Then
    192. pwDecryptByte = Array.ConvertAll(value.Split(CType(",", Char())), Function(Str) Byte.Parse(Str))
    193. Else
    194. pwDecryptByte = Nothing
    195. End If
    196. Catch ex As Exception
    197. Return value
    198. End Try
    199. Try
    200. Dim TextDecrypt = myDES.Decrypt(pwDecryptByte, initVector)
    201. Return CType(IIf(TextDecrypt Is Nothing, value, TextDecrypt), String)
    202. Catch ex As Exception
    203. Return value
    204. End Try
    205. Else
    206. Return ""
    207. End If
    208. End Function
    209. ''' <summary>
    210. ''' Entschlüsselung von Textinhalten
    211. ''' </summary>
    212. ''' <param name="value">Verschlüsselter Inhalt</param>
    213. ''' <returns>
    214. ''' Entschlüsselter Text
    215. ''' Falls der verschlüsselte Inhalt nicht entschlüsselt werden kann, wird der Wert von value unverändert zurückgegeben</returns>
    216. Friend Shared Function DecryptText(ByRef value As String) As String
    217. Dim myDES As New TripleDES
    218. Return myDES.DecryptText(value, Nothing)
    219. End Function
    220. ''' <summary>
    221. ''' Verschlüsselung von Inhalten
    222. ''' </summary>
    223. ''' <param name="value">zu verschlüsselnder Textinhalt </param>
    224. ''' <param name="initVector">Initialisierungsvektor</param>
    225. ''' <returns></returns>
    226. ''' <remarks>
    227. ''' Über den Initialisierungsvector wird die Verschlüsselung festgelegt
    228. ''' Dieser muss beim Verschlüsseln und Entschlüsseln identisch sein.
    229. ''' </remarks>
    230. Friend Function EncryptText(ByRef value As String, initVector As Byte()) As String
    231. Dim myDES As New TripleDES
    232. If value.Length > 0 Then
    233. Try
    234. Dim pwBytes As Byte() = myDES.Encrypt(value, initVector)
    235. Return String.Join(",", pwBytes)
    236. Catch ex As Exception
    237. Return ""
    238. End Try
    239. Else
    240. Return ""
    241. End If
    242. End Function
    243. ''' <summary>
    244. ''' Verschlüsselung von Inhalten
    245. ''' </summary>
    246. ''' <param name="value">zu verschlüsselnder Textinhalt </param>
    247. ''' <returns></returns>
    248. Friend Shared Function EncryptText(ByRef value As String) As String
    249. Dim myDES As New TripleDES
    250. If value.Length > 0 Then
    251. Try
    252. Dim pwBytes As Byte() = myDES.Encrypt(value)
    253. Return String.Join(",", pwBytes)
    254. Catch ex As Exception
    255. Return ""
    256. End Try
    257. Else
    258. Return ""
    259. End If
    260. End Function
    261. End Class


    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „BigBen2003“ ()

    @BigBen2003 Pack mal bitte den Code in 3 Spoiler (jede Klasse einzeln).
    Bei mir ist der Designer-Eintrag bereits dann weg, wenn ich in eine andere Property klicke.
    Vielleicht siehst Du Dir mal eine "normale" Spalte im IlSpy an und stellst fest, wie Microsoft das gemacht hat, und empfindest es nach.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Hallo,

    nach dem das Control fertig gestellt wurde, ist mir augefallen, dass bei aktivierter Eigenschaft "UseSystemPasswordChar" folgendes auftritt:

    1. Neueingabe eines Passwortes bei deaktiviertem "UseSystemPasswordChar":

    Nach der Übernahme des neuen Passwortes wird standardmäßig das Passwort im Maskierungsmodus angezeigt.

    Im DataSet wird das Passwort korrekt übernomme; unbhängig von der Einstellung "UseSystemPasswordChar".

    Bei einem Neu eingegebenen Passwort kann die Anzeige das Passwort wahlweise durch Anklicken des rechts eingeblendeten Buttons maskiert bzw. als Klartext umgeschaltet werden.

    2. Änderung eines bestehenden Passwortes bei deaktiviertem UseSystemPasswordChar:

    Wenn ein bestehendes Passwort in Bearbung genommen wird, so werden die Passwort-Zeichen alle durch "●"-Zeichen ersetzt. In der Zelle hingegen steht weiterhin das richtige Passwort, solange, bis es durch das neue ersetzt wird. Im Beispiel wäre es "●●●●Test".

    An irgend einer Stelle wird demnach das Maskierungszeichen als Bearbeitungstext übernommen, sobald ein bestehendes Passwort in Bearbeitung benommen wird.

    Hat jemand eine Idee, wie man die Fehlerursache einschränken kann?

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „BigBen2003“ ()

    BigBen2003 schrieb:

    Fehlerursache
    Kannst Du den relevanten Teil des Projekts hochladen?
    Falls Du ein Teilprojekt postest, stelle bitte sicher, dass es kompiliert und den Effekt reproduziert.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Hier ist das Teilprojekt: FormTestNeu.zip

    Im UserForm "FormNeu" sind einge Test-Controls.

    Mir ist noch etwas aufgefallen:

    In der "InitializeEditingControl" wird ein Button hinzugefügt, der rechts im Textbox eingeblendet wird:
    Falls jedoch im DataGridView eine weitere Spalte vom Typ Textbox enthalten sein sollte, wird der Button seltsamerweise auch dort eingeblendet.

    Der Button darf nur bei dem Passwort-Eingabe-Control eingeblendet werden.

    VB.NET-Quellcode

    1. Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer,
    2. ByVal initialFormattedValue As Object,
    3. ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle)
    4. MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
    5. Dim _TextBox As TextBox = DirectCast(Me.DataGridView.EditingControl, TextBox)
    6. With _TextBox
    7. 'Remember the current password properties of the editing control.
    8. Me.editingControlPasswordChar = .PasswordChar
    9. Me.editingControlUseSystemPasswordChar = .UseSystemPasswordChar
    10. 'Set the new password properties of the editing control.
    11. .PasswordChar = Me._passwordChar
    12. .UseSystemPasswordChar = Me._useSystemPasswordChar
    13. ' Der Button wird nur hinzugefügt, falls noch keiner existiert
    14. If .Controls.Count = 0 Then
    15. ' Button rechts einblenden
    16. '_Button = New ToggleButtonShowHidePassword ' Button
    17. _Button = New Button
    18. _Button.Text = "..." ' ToDo: Umschalter für Sichtbar und unsichtbare Passwörter erstellen
    19. _Button.Dock = DockStyle.Right
    20. _Button.Cursor = Cursors.Hand
    21. _Button.Width = 20
    22. _Button.Visible = False
    23. .Controls.Add(_Button)
    24. AddHandler _TextBox.Enter, AddressOf Textbox_OnEnter
    25. 'AddHandler _TextBox.Enter, Function(sender, e) Textbox_OnEnter(myCell)
    26. AddHandler _TextBox.Leave, AddressOf Textbox_OnLeave
    27. End If
    28. End With
    29. End Sub

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „BigBen2003“ ()

    @BigBen2003 Der Button wird bei mir nicht angezeigt.
    Was muss ich tun, um den Effekt zu reproduzieren?
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Habe mir gerade mal die Klassen in ein leeres Projekt eingebaut und kompiliert. Dazu noch ein leeres DGV und dann per Spalten hinzufügen Deine eigene Column. Visual Studio stürzt daraufhin immer ab. Zumindest Dein Testprojekt bekomm ich zum Laufen. Aber - auch wenn das nicht Teil des Themas ist: Deine ganzen CType(IIF(...-Geschichten kannst Du viel einfacher gestalten:
    Da IIF 1. aus dem Microsoft-VisualBasic-Namespace stammt (was bei mir immer deaktiviert ist/wird) und 2. nur etwas vom Typ Object zurückgibt, wurde was besseres entwickelt, was auch den Typ beibehält.
    Statt z.B.

    VB.NET-Quellcode

    1. myCell.Style.BackColor = CType(IIf(bReadonly, InvertColor(SystemColors.Window), SystemColors.Window), Color)

    kannst Du einfach folgendes schreiben:

    VB.NET-Quellcode

    1. myCell.Style.BackColor = If(bReadonly, InvertColor(SystemColors.Window), SystemColors.Window)


    @RodFromGermany: Ich hab's erst durch Ausprobieren entdeckt: Man muss UserName, Typ und Passwort eingeben und dann nochmal in die schon beschriebene Zelle der UserName-Spalte reingehen, also den UserName nochmal bearbeiten oder alternativ in einer neuen Zeile einen UserName eingeben. Dann taucht der Button auf.
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „VaporiZed“ ()

    @VaporiZed Danke, habs gefunden.
    @BigBen2003 Ich habe mal eine Debug-Ausgabe reingebaut: Console.WriteLine(_txtBox.Text).
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private Sub _Button_Click(sender As Object, e As EventArgs) Handles _Button.Click
    2. Dim _button = DirectCast(sender, Button)
    3. Dim _txtBox = DirectCast(_button.Parent, TextBox)
    4. If _txtBox.InvokeRequired Then
    5. _txtBox.Invoke(New _Button_Click_Delegate(AddressOf _Button_Click), sender, e)
    6. Else
    7. _txtBox.UseSystemPasswordChar = Not _txtBox.UseSystemPasswordChar
    8. Console.WriteLine(_txtBox.Text)
    9. '_txtBox.Refresh()
    10. '_txtBox.Invalidate()
    11. End If
    12. End Sub
    Ich denke, dass das Verhalten völlig in Ordnung ist.
    Wenn die PasswordTextBox den Fokus verloren hat, wird das Passwort geschützt, sonst könnte es per C&P rausgeholt werden.

    MSDN schrieb:

    PasswordChar
    property is set, cut and copy actions in the control using the keyboard
    cannot be performed.">Wenn die PasswordChar festgelegt wird,
    Ausschneiden und Kopieren von Aktionen im Steuerelement mithilfe der
    Tastatur können nicht ausgeführt werden.
    =========
    @BigBen2003 Nimm die Funktion DataGridViewEncryptPasswordTextBoxCell.GetFormattedValue()raus :!:
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „RodFromGermany“ ()

    Hallo,
    vielen Dank für die Unterstützung.

    die Funktion "DataGridViewEncryptPasswordTextBoxCell.GetFormattedValue()" muss drin bleiben, da das Passwort in der Finalen Version mit TripleDes verschlüsselt abgespeichert werden soll.

    Wenn das Verhalten normal ist, dann muss der Benutzer eben das Passwort immer komplett neu eingeben.

    Nachtrag:

    Wenn im TextBox-Objekt in der Enter-Methode das bereits bespeicherte Passwort in der TextBox gesetzt wird, kann auch ein bestehendes Passwort nachträglich noch angezeigt und bearbeitet werden. Dies ist zwar hinsichtlich Datenschutz nicht so optimal, aber von seitens des Bedieners hilfreicher.

    VB.NET-Quellcode

    1. Private Sub _Textbox_Enter(sender As Object, e As EventArgs) Handles _Textbox.Enter
    2. If Not _Textbox.Modified Then
    3. Dim myValue = Me.DataGridView.Rows(Me.RowIndex).Cells(Me.ColumnIndex).Value
    4. _Textbox.Text = IIf(myValue Is Nothing, "", myValue).ToString
    5. End If
    6. End Sub

    Dieser Beitrag wurde bereits 4 mal editiert, zuletzt von „BigBen2003“ ()

    Ein "Problem" ist noch offen:

    Der Button zum Umschalten der Passwort-Anzeige wird erst eingeblendet, wenn ein Passwort zur bereits eingetragen wurde.

    Ursache: Die Initialisierungs wird beim ersten Bearbeiten nicht aufgerufen. Erst ab dem zweiten Aufruf wird die Initialisierungsroutine aufgerufen:

    VB.NET-Quellcode

    1. Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer,
    2. ByVal initialFormattedValue As Object,
    3. ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle)
    4. MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
    5. ' .... Button erstellen
    6. End Sub


    Weiß jemand, warum dieser Aufruf bei der ersten Eingabe nicht erfolgt?
    Hallo, nun sind alle Fehler beseitigt:

    Hier die Klasse DataGridViewEncryptPasswordTextBoxCell:

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Option Explicit On
    2. Option Strict On
    3. Imports System.ComponentModel
    4. Public Class DataGridViewEncryptPasswordTextBoxCell
    5. Inherits System.Windows.Forms.DataGridViewTextBoxCell
    6. Private myTripleDES As New TripleDES
    7. Private _encryptPassword As Boolean
    8. Private _initVector As Byte()
    9. Private _passwordChar As Char
    10. Private _useSystemPasswordChar As Boolean
    11. Private editingControlPasswordChar As Char
    12. Private editingControlUseSystemPasswordChar As Boolean
    13. Private _Copy As DataGridViewEncryptPasswordTextBoxCell
    14. ' Button im Textbox rechts einblenden, wenn der Wert bearbeitet wird
    15. 'Private WithEvents _Button As ToggleButtonShowHidePassword
    16. Private WithEvents _Button As Button
    17. Private WithEvents _Textbox As TextBox
    18. Private Shared _isInitialized As Boolean
    19. Sub New()
    20. MyBase.New
    21. If Not _isInitialized Then
    22. Me._encryptPassword = True
    23. Me._initVector = Nothing
    24. Me._passwordChar = CChar("")
    25. Me._useSystemPasswordChar = True
    26. _isInitialized = True
    27. End If
    28. End Sub
    29. <Description("Soll das eingegebene Passwort mit dem TripleDES-Verfahren verschlüsselt abgespeichert werden?")>
    30. <DefaultValue(True)>
    31. Public Property encryptPassword() As Boolean
    32. Get
    33. Return Me._encryptPassword
    34. End Get
    35. Set(value As Boolean)
    36. Me._encryptPassword = value
    37. End Set
    38. End Property
    39. <Description("InitVector muss 8 Bytes beinhalten, die durch Kommatas voneinander getrennt sind. Beispiel: 65, 110, 68, 26, 69, 178, 200, 219")>
    40. <DefaultValue("")>
    41. Public Property initVector As String
    42. Get
    43. If Me._initVector IsNot Nothing Then
    44. Return String.Join(",", Me._initVector)
    45. Else
    46. Return Nothing
    47. End If
    48. End Get
    49. Set(value As String)
    50. Dim IsValid As Boolean
    51. Dim myItems = myTripleDES.ConvertToBytes(value, 8, IsValid)
    52. If IsValid Then
    53. Me._initVector = myItems
    54. End If
    55. End Set
    56. End Property
    57. <Description("Passwort-Maskierungszeichen - Es kann nur ein Zeichen eingegeben werden!")>
    58. <DefaultValue("")>
    59. Public Property PasswordChar() As Char
    60. Get
    61. Return Me._passwordChar
    62. End Get
    63. Set(ByVal value As Char)
    64. Me._passwordChar = value
    65. End Set
    66. End Property
    67. <Description("Passwort maskiert anzeigen? True = Passwort wird maskiert angezeigt; False = Passwort wird in Klartext angezeigt")>
    68. <DefaultValue(True)>
    69. Public Property UseSystemPasswordChar() As Boolean
    70. Get
    71. Return Me._useSystemPasswordChar
    72. End Get
    73. Set(ByVal value As Boolean)
    74. Me._useSystemPasswordChar = value
    75. End Set
    76. End Property
    77. <Browsable(False)>
    78. <EditorBrowsable(EditorBrowsableState.Never)>
    79. Public Overrides Function Clone() As Object
    80. _Copy = DirectCast(MyBase.Clone(), DataGridViewEncryptPasswordTextBoxCell)
    81. _Copy.PasswordChar = Me._passwordChar
    82. _Copy.UseSystemPasswordChar = Me._useSystemPasswordChar
    83. _Copy.encryptPassword = Me._encryptPassword
    84. If Me._initVector IsNot Nothing Then
    85. _Copy.initVector = String.Join(",", Me._initVector)
    86. Else
    87. _Copy.initVector = ""
    88. End If
    89. Return _Copy
    90. End Function
    91. Protected Overrides Sub OnKeyPress(e As KeyPressEventArgs, rowIndex As Integer)
    92. MyBase.OnKeyPress(e, rowIndex)
    93. If _Button IsNot Nothing Then
    94. _Button.Visible = True
    95. End If
    96. End Sub
    97. 'Protected Overrides Sub OnKeyDown(e As KeyEventArgs, rowIndex As Integer)
    98. ' MyBase.OnKeyDown(e, rowIndex)
    99. ' If e.KeyCode = Keys.F2 Then
    100. ' 'Me.DataGridView.Rows(Me.RowIndex).Cells(Me.ColumnIndex).Value = ""
    101. ' If Me.DataGridView.EditingControl Is Nothing Then
    102. ' Me.DataGridView.Rows(Me.RowIndex).Cells(Me.ColumnIndex).Value = ""
    103. ' End If
    104. ' End If
    105. ' If _Button Is Nothing Then
    106. ' InitializeButton()
    107. ' End If
    108. ' If _Button IsNot Nothing Then
    109. ' _Button.Visible = True
    110. ' End If
    111. 'End Sub
    112. Protected Overrides Sub OnEnter(rowIndex As Integer, troughMouseClick As Boolean)
    113. MyBase.OnEnter(rowIndex, troughMouseClick)
    114. If _Button Is Nothing Then
    115. InitializeButton()
    116. End If
    117. If _Button IsNot Nothing Then
    118. _Button.Visible = True
    119. End If
    120. End Sub
    121. Protected Overrides Sub OnLeave(rowIndex As Integer, throughMouseClick As Boolean)
    122. MyBase.OnLeave(rowIndex, throughMouseClick)
    123. If _Button Is Nothing Then
    124. InitializeButton()
    125. End If
    126. If _Button IsNot Nothing Then
    127. _Button.Visible = False
    128. End If
    129. _Button = Nothing
    130. _Textbox = Nothing
    131. End Sub
    132. Protected Overrides Function GetFormattedValue(ByVal value As Object,
    133. ByVal rowIndex As Integer,
    134. ByRef cellStyle As System.Windows.Forms.DataGridViewCellStyle,
    135. ByVal valueTypeConverter As System.ComponentModel.TypeConverter,
    136. ByVal formattedValueTypeConverter As System.ComponentModel.TypeConverter,
    137. ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object
    138. Dim formattedValue As Object
    139. If Me._encryptPassword AndAlso value IsNot Nothing Then
    140. value = myTripleDES.DecryptText(CType(value, String), Me._initVector)
    141. End If
    142. If Me._useSystemPasswordChar AndAlso value IsNot Nothing Then
    143. 'Display the system password character in place of each actual character.
    144. 'TODO: Determine the actual system password character instead of hard-coding this value.
    145. formattedValue = New String(Convert.ToChar(&H25CF), CStr(value).Length)
    146. ElseIf Me._passwordChar <> Char.MinValue AndAlso value IsNot Nothing Then
    147. 'Display the user-defined password character in place of each actual character.
    148. formattedValue = New String(Me._passwordChar, CStr(value).Length)
    149. Else
    150. 'Display the value as is.
    151. formattedValue = MyBase.GetFormattedValue(value,
    152. rowIndex,
    153. cellStyle,
    154. valueTypeConverter,
    155. formattedValueTypeConverter,
    156. context)
    157. End If
    158. Return formattedValue
    159. End Function
    160. Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer,
    161. ByVal initialFormattedValue As Object,
    162. ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle)
    163. MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
    164. InitializeButton()
    165. End Sub
    166. Private Sub InitializeTextbox()
    167. _Textbox = DirectCast(Me.DataGridView.EditingControl, TextBox)
    168. If _Textbox Is Nothing Then
    169. Dim myCell = Me.DataGridView.Rows(Me.RowIndex).Cells(ColumnIndex)
    170. Dim myValue = myCell.Value
    171. End If
    172. If _Textbox IsNot Nothing Then
    173. With _Textbox
    174. 'Remember the current password properties of the editing control.
    175. Me.editingControlPasswordChar = .PasswordChar
    176. Me.editingControlUseSystemPasswordChar = .UseSystemPasswordChar
    177. 'Set the new password properties of the editing control.
    178. .PasswordChar = Me._passwordChar
    179. .UseSystemPasswordChar = Me._useSystemPasswordChar
    180. End With
    181. End If
    182. End Sub
    183. Private Sub InitializeButton()
    184. If _Textbox Is Nothing Then
    185. InitializeTextbox()
    186. End If
    187. If _Textbox IsNot Nothing Then
    188. With _Textbox
    189. ' Der Button wird nur hinzugefügt, falls noch keiner existiert
    190. If .Controls.Count = 0 Then
    191. ' Button rechts einblenden
    192. '_Button = New ToggleButtonShowHidePassword ' Button
    193. _Button = New Button
    194. _Button.Text = "..." ' ToDo: Umschalter für Sichtbar und unsichtbare Passwörter erstellen
    195. _Button.Dock = DockStyle.Right
    196. _Button.Cursor = Cursors.Hand
    197. _Button.Width = 20
    198. _Button.Visible = False
    199. .Controls.Add(_Button)
    200. Else
    201. _Button = DirectCast(.Controls(0), Button)
    202. End If
    203. End With
    204. End If
    205. If _Button IsNot Nothing Then
    206. _Button.Visible = True
    207. End If
    208. End Sub
    209. ''' <summary>
    210. ''' Bei Bedarf wird der eingegebene Text vor einer Übernahme verschlüsselt
    211. ''' </summary>
    212. ''' <param name="rowIndex">Index im DataGridView</param>
    213. ''' <param name="Value">zu übernehmender Wert</param>
    214. ''' <returns>True oder False</returns>
    215. Protected Overrides Function SetValue(rowIndex As Integer, Value As Object) As Boolean
    216. If Me._encryptPassword AndAlso Value IsNot Nothing Then
    217. Value = myTripleDES.EncryptText(CType(Value, String), Me._initVector)
    218. End If
    219. Return MyBase.SetValue(rowIndex, Value)
    220. End Function
    221. ''' <summary>
    222. ''' Nach dem Bearbeiten des Zellinhalts, die Darstellung gemäß der festgelegten Art anpassen.
    223. ''' </summary>
    224. Public Overrides Sub DetachEditingControl()
    225. MyBase.DetachEditingControl()
    226. With DirectCast(Me.DataGridView.EditingControl, TextBox)
    227. 'Reset the old password properties of the editing control.
    228. .PasswordChar = Me.editingControlPasswordChar
    229. .UseSystemPasswordChar = Me.editingControlUseSystemPasswordChar
    230. End With
    231. End Sub
    232. Delegate Sub _Button_Click_Delegate(sender As Object, e As EventArgs)
    233. Private Sub _Button_Click(sender As Object, e As EventArgs) Handles _Button.Click
    234. Dim _button = DirectCast(sender, Button)
    235. Dim _txtBox = DirectCast(_button.Parent, TextBox)
    236. If _txtBox.InvokeRequired Then
    237. _txtBox.Invoke(New _Button_Click_Delegate(AddressOf _Button_Click), sender, e)
    238. Else
    239. Dim myValue = _txtBox.Text
    240. _txtBox.UseSystemPasswordChar = Not _txtBox.UseSystemPasswordChar
    241. _txtBox.Refresh()
    242. _txtBox.Invalidate()
    243. End If
    244. End Sub
    245. ''' <summary>
    246. ''' In die Textbox wird eine Eingabe getätigt (Vor der Eingabe)
    247. ''' </summary>
    248. ''' <param name="sender"></param>
    249. ''' <param name="e">EventArgumente</param>
    250. Private Sub _Textbox_Enter(sender As Object, e As EventArgs) Handles _Textbox.Enter
    251. If Not _Textbox.Modified Then
    252. Dim myValue = Me.DataGridView.Rows(Me.RowIndex).Cells(Me.ColumnIndex).Value
    253. _Textbox.Text = IIf(myValue Is Nothing, "", myValue).ToString
    254. End If
    255. End Sub
    256. End Class