none
Trying to create a seperate form for settings which carries over settings to other forms? RRS feed

  • Question

  • I'm a Level 3 student and I'm having issues with this program. I wanted to have a form to let the user set the background and font colour. I thought I could have a public variable for bgcolour and font colour, with an integer representing the user's choice in a list box. The weird thing is that making the changes works within the 'settings' but when I attempt to return to the main form nothing happens - as in I stay on the settings form. 

    Sunday, November 26, 2017 12:55 PM

Answers

  • There is always ApplicationSettings/PropertyBinding where you can bind a property to a user setting (stored in app.config at runtime, not design time).

    On each form, set Application.Setting/PropertyBinding to a user setting e.g.

    Then in a form call another form to set the background color for all forms which will be remembered session to session.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f As New ColorForm
            Try
                f.ShowDialog()
            Finally
                f.Dispose()
            End Try
        End Sub
    End Class

    In the form ColorForm we place a ColorDialog onto the form and use it to set the background color for all open forms. Of course when a form that uses the same Application.Setting/PropertyBinding it will have the new background color too.

    Public Class ColorForm
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If ColorDialog1.ShowDialog = DialogResult.OK Then
                My.Settings.UserBackgroundColor = ColorDialog1.Color
                My.Application.OpenForms.Cast(Of Form).Select(Function(f) f).ToList.ForEach(
                    Sub(f)
                        If f.Name <> "ColorForm" Then
                            f.BackColor = ColorDialog1.Color
                        End If
                    End Sub
               )
            End If
        End Sub
    End Class

    MSDN documentation

    https://msdn.microsoft.com/en-us/library/wabtadw6%28v=vs.90%29.aspx?f=255&MSPPError=-2147217396

    I created a complete working demo found here.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, November 26, 2017 2:01 PM
    Moderator

All replies

  • If you mean the My.Settings, those are values that are saved when you close the application.

    Then when you start the app again your can retrieve the values that were saved. But unless the settings are bound to something, they are just values that are saved. So you have to add code to use the values.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Me.BackColor = My.Settings.myBGcolor
    
    

    Sunday, November 26, 2017 1:05 PM
  • There is always ApplicationSettings/PropertyBinding where you can bind a property to a user setting (stored in app.config at runtime, not design time).

    On each form, set Application.Setting/PropertyBinding to a user setting e.g.

    Then in a form call another form to set the background color for all forms which will be remembered session to session.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f As New ColorForm
            Try
                f.ShowDialog()
            Finally
                f.Dispose()
            End Try
        End Sub
    End Class

    In the form ColorForm we place a ColorDialog onto the form and use it to set the background color for all open forms. Of course when a form that uses the same Application.Setting/PropertyBinding it will have the new background color too.

    Public Class ColorForm
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If ColorDialog1.ShowDialog = DialogResult.OK Then
                My.Settings.UserBackgroundColor = ColorDialog1.Color
                My.Application.OpenForms.Cast(Of Form).Select(Function(f) f).ToList.ForEach(
                    Sub(f)
                        If f.Name <> "ColorForm" Then
                            f.BackColor = ColorDialog1.Color
                        End If
                    End Sub
               )
            End If
        End Sub
    End Class

    MSDN documentation

    https://msdn.microsoft.com/en-us/library/wabtadw6%28v=vs.90%29.aspx?f=255&MSPPError=-2147217396

    I created a complete working demo found here.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, November 26, 2017 2:01 PM
    Moderator
  • The weird thing is that making the changes works within the 'settings' but when I attempt to return to the main form nothing happens - as in I stay on the settings form. 

    There would not usually be any code to 'return to the main form'.  When the user is finished with the settings form, they close it, so there is no way they can 'stay on the settings form'.  Whatever other forms they have opened remain open and accessible.   You should show what the code is to 'return to the main form' and describe why you are using it.

    Sunday, November 26, 2017 8:47 PM
  • Add a Module to your project and declare all your public variables in that module. Then, dialogs and any forms can manipulate those public variables. You need to add code to your main forms closing event to save the relevant variables to My.Settings - -  so they can be read on next start up. Or you can use a dedicated file to keep necessary data ready for next start-up.

    Burn all valuable data to CD/DVD disks. EMP proof.

    Monday, November 27, 2017 9:09 PM
  • Hello,

    Just checking in to see if you have tried what has been suggested?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, November 29, 2017 10:37 AM
    Moderator