Solved

Bind textbox to custom property in My.Settings

Posted on 2016-10-24
2
90 Views
Last Modified: 2016-10-26
I need to store a complete set of database connection information in a utility app's settings file. Storing a password in clear text is not acceptable, but the user should not have to worry about that. I have the server, database and user name settings all bound to text controls as usual. When it comes to bind to the clear text password setting, it does not show up in the list (illustrated below).

Visual Studio Property Binding Dialog
Here's my customized settings class. The three code attributes are copied directly from the Settings.Designer.vb file generated for the other simple text settings. I would think this property would show up, but it does not. I re-compiled, closed Visual Studio, re-opened -- nothing.

Namespace My
  Partial Friend NotInheritable Class MySettings
    <Global.System.Configuration.UserScopedSettingAttribute(), _
     Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), _
     Global.System.Configuration.DefaultSettingValueAttribute("")> _
    Public Property Password As String
      Get
        Return Utility.Security.Decrypt(My.Settings.PasswordEncrypted)
      End Get
      Set(value As String)
        My.Settings.PasswordEncrypted = Utility.Security.Encrypt(value)
      End Set
    End Property
  End Class
End Namespace

Open in new window


If there is a better way to handle this I'm open, but the above is how it ought to work.
0
Comment
Question by:CraigYellick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 28

Accepted Solution

by:
Ark earned 500 total points
ID: 41859636
There are 3 options:
1. Bind control property at run-time:
PasswordTextBox.DataBindings.Add("Text", My.Settings, "Password")

Open in new window

2. Decript password on loading settings and encript on saving:
Namespace My
  Partial Friend NotInheritable Class MySettings
      Private Sub MySettings_SettingsLoaded(ByVal sender As Object, ByVal e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded
        My.Settings.PasswordEncrypted = Utility.Security.Decrypt(My.Settings.PasswordEncrypted)
      End Sub
      Private Sub MySettings_SettingsSaving(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Me.SettingsSaving
        My.Settings.PasswordEncrypted = Utility.Security.Encrypt(My.Settings.PasswordEncrypted)
      End Sub
  End Class
End Namespace

Open in new window

3. Add Password setting via settings tab in IDE and change code for this property directly in Settings.Designer.vb
0
 
LVL 11

Author Comment

by:CraigYellick
ID: 41860747
While it'd be nice to get the setting to bind like the others, it's not worth spending a lot of time on it. Option #2 seems safe enough but I did add a line to clear out the clear-text password when saving.

Namespace My
  Partial Friend NotInheritable Class MySettings
    Private Sub MySettings_SettingsLoaded(sender As Object, e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded
      My.Settings.Password = Utility.Security.Decrypt(My.Settings.PasswordEncrypted)
    End Sub
    Private Sub MySettings_SettingsSaving(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Me.SettingsSaving
      My.Settings.PasswordEncrypted = Utility.Security.Encrypt(My.Settings.Password)
      My.Settings.Password = "" ' Do not store cleartext password
    End Sub
  End Class
End Namespace

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question