Thursday 26 January 2012

Using of My.Settings in vb.net




Use of settings in vb.net
Windows Forms applications require data that is needed for running the application. Most of the time you don't want to include this data in the code or you will have to recompile your application each time you make a change. If your application uses a Web service or a database, you probably want to store the URL or the connection string in a separate file so that you can change them easily. If your application stores the window layout and other UI customization, then you want to store this information separately for each user.

When you use settings that time the all settings are in app.config file and it’s code look’s like
  <userSettings>
        <BillPrinting.My.MySettings>
            <setting name="PVer" serializeAs="String">
                <value>150</value>
            </setting>
            <setting name="PHor" serializeAs="String">
                <value>170</value>
            </setting>
            <setting name="BackColor" serializeAs="String">
                <value>White</value>
            </setting>
            <setting name="SpaceTwo" serializeAs="String">
                <value>440</value>
            </setting>
            <setting name="printername" serializeAs="String">
                <value>0</value>
            </setting>
            <setting name="FPSmall" serializeAs="String">
                <value>Courier New, 9.75pt</value>
            </setting>
            <setting name="FPDark" serializeAs="String">
                <value>Arial, 14.25pt, style=Bold</value>
            </setting>
            <setting name="FPDate" serializeAs="String">
                <value>Courier New, 11.25pt</value>
            </setting>
        </BillPrinting.My.MySettings>
    </userSettings>

If the setting names scope is user than run time you change these value. And it the scope is application than you can not change these value.
In image thes example of set setting property.
Example image

Exmplae of access the mysetting value in code
Private Sub BillSetting_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            TxtPver.Text = My.Settings.PVer
            TxtHoriP.Text = My.Settings.PHor
            TxtSpace.Text = My.Settings.SpaceTwo
            Me.BackColor = My.Settings.BackColor
        Catch ex As Exception

        End Try
    End Sub

For change the value of property(If scope is user than only change else it not change).
Try
            Dim x As DialogResult = FontDialog1.ShowDialog()
            If x = Windows.Forms.DialogResult.OK Then
                Try
                    My.Settings.FPSmall = FontDialog1.Font
                Catch ex As Exception

                End Try
            End If
        Catch ex As Exception
            MsgBox("These Font Are Not Suported", MsgBoxStyle.Information, "")
        End Try

No comments:

Post a Comment