user7598312
user7598312

Reputation:

How to convert String into code in VB.net?

First, let's have a look at my code:

Private Sub FormLoad(sender As Object, e As EventArgs) Handles MyBase.Load

    txtMDF.Text = My.Settings.MDF

End Sub

Assume My.Settings.MDF has a string value of Application.StartupPath + "\MyDB.mdf". I get this result:

Image 1

But I want the result to be:

Image 2

I have tried the following links and methods:

Upvotes: 0

Views: 983

Answers (1)

As ProGamer Suggested,

First

save you My.Settings.MDF String = None

Second

Edit your code as follows:

Private Sub FormLoad(sender As Object, e As EventArgs) Handles MyBase.Load

    If My.Settings.MDF = "None" Then
        txtMDF.Text = Application.StartupPath + "\MyDB.mdf"
    Else
        txtMDF.Text = My.Settings.MDF
    End If
    txtMDF.Text = My.Settings.MDF

End Sub

Third

Add the following code to YourFormClose_Event

Private Sub FormClosing(sender As Object, e As CancelEventArgs) Handles Me.Closing
    My.Settings.MDF = txtMDF.Text
    My.Settings.Save()
End Sub

And NOTE that you should select "User" from 'Scope Drop Down' in the Settings for MDF instead of "Application" or else My.Settings.Save() will not work and it will remain "None"

Example:

enter image description here

Upvotes: 2

Related Questions