none
MySetting save start location of form RRS feed

  • Question

  • Hi all

    I have a win form application in visual basic that uses several forms

    The user can move and resize each form as he wants and I save in My.settings the size and location of each form.

    In this way if the user close a form and  after he opens it again, he find the form in the last position with the last size.

    In my.settings i use this type

    Name: Form1Location

    Type : System.Drawing.Point

    Value : 0;0

    Now. When the application is closed and opend againg i wish that each form will be located at center screen as default position, with default size.

    The answer is :

    how can I save the value "centerscreen" in the System.Drawing.Point type in My.settings ?

    The My.Setting.Reset Method is OK for resetting size of all forms but since I dont know the user's screen resolution I dont know wich is the centerscreen.

    Claudio

    Sunday, December 3, 2017 12:20 PM

Answers

  •  The following example works on my end.  If the application has just been started,  then Form2 will be opened at its default size in the center of the screen.  However,  if you open Form2 and move and/or resize it and then close it,  it will be opened in the same location and size as it was closed.  If you close the application and then re-open it again,  then Form2 will be opened in the center of the screen at it's default size again.  I believe this is exactly what you want to do.

     Follow these steps in a new form project just so you can test it.

     First,  open a new form project and add a second form (Form2) to the project.  Set Form2's StartPosition property to CenterScreen.

     Now go to the (Project -> Properties -> Settings) and add a setting named Form2_Bounds (as seen in image below).  Then you need to set it's Type to a Rectangle.  To use a Rectangle type,  you need to open the Drop Down and select (Browse) at the bottom (as seen in image below).  I already did this beforehand as can be seen,  this is just to show you the steps.

     

     After selecting (Browse),  you will need to open these nodes to select a Rectangle type.  (System.Drawing -> System.Drawing) as seen in the image.  Then you can select the (Rectangle) type and press the (Ok) button.

     

     Now you can add 1 Button (Button1) to the main form (Form1) and use this code for Form1.

    Imports System.ComponentModel
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f2 As New Form2
            If My.Settings.Form2_Bounds <> Rectangle.Empty Then 'if Form2_Bounds setting is 0,0,0,0 then it has not been opened yet
                f2.StartPosition = FormStartPosition.Manual
                f2.Bounds = My.Settings.Form2_Bounds
            End If
            f2.Show()
        End Sub
    
        Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
            My.Settings.Reset() 'resets the Form2_Bounds setting back to it's default 0,0,0,0
        End Sub
    
    End Class
     

     Now add this code to Form2.

    Imports System.ComponentModel
    
    Public Class Form2
        Private Sub Form2_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
            My.Settings.Form2_Bounds = Me.Bounds 'set the Form2_Bounds setting to the current Form2 bounds
        End Sub
    End Class
     

     You will just need to add a Rectangle setting for each of your forms and implement the code shown above for each of your Forms.

     Here is an animated image showing the above example in action...


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Sunday, December 3, 2017 9:09 PM
    • Marked as answer by Claudio111 Thursday, December 7, 2017 5:43 PM
    Sunday, December 3, 2017 6:29 PM

All replies

  • Hi all

    I have a win form application in visual basic that uses several forms

    The user can move and resize each form as he wants and I save in My.settings the size and location of each form.

    In this way if the user close a form and  after he opens it again, he find the form in the last position with the last size.

    In my.settings i use this type

    Name: Form1Location

    Type : System.Drawing.Point

    Value : 0;0

    Now. When the application is closed and opend againg i wish that each form will be located at center screen as default position, with default size.

    The answer is :

    how can I save the value "centerscreen" in the System.Drawing.Point type in My.settings ?

    The My.Setting.Reset Method is OK for resetting size of all forms but since I dont know the user's screen resolution I dont know wich is the centerscreen.

    Claudio

    Hi

    It looks like you do not want to use the My.Settings at all for this.

    Just use the following code in the Form Load Events for each Form.

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
     StartPosition = FormStartPosition.CenterScreen
    
     ' and/or
    
     Size = New Size(600, 400)
    
    


    Regards Les, Livingston, Scotland

    Sunday, December 3, 2017 12:51 PM
  • Hi Les

    This is ok if I want the form in center screen every time

    But i want this just when I open the application

    If  the user is already in the application the form must use the values in My.Settings,   values  that are changed  and stored as the user change  location and size of the form 

    Claudio

    Sunday, December 3, 2017 1:09 PM
  • Hi Les

    This is ok if I want the form in center screen every time

    But i want this just when I open the application

    If  the user is already in the application the form must use the values in My.Settings,   values  that are changed  and stored as the user change  location and size of the form 

    Claudio

    Hi

    In which case, use the FormStartPosition in the first Form and the My.settings for the other Forms.


    Regards Les, Livingston, Scotland

    Sunday, December 3, 2017 1:28 PM
  • Claudio,

    You might consider "rolling your own" for this.

    No more than this is, a class would work well and you can use XML to persist the data - then store the XML in the program's Application Data directory (that you create if it doesn't exist).

    Food for thought.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, December 3, 2017 2:14 PM
  •  The following example works on my end.  If the application has just been started,  then Form2 will be opened at its default size in the center of the screen.  However,  if you open Form2 and move and/or resize it and then close it,  it will be opened in the same location and size as it was closed.  If you close the application and then re-open it again,  then Form2 will be opened in the center of the screen at it's default size again.  I believe this is exactly what you want to do.

     Follow these steps in a new form project just so you can test it.

     First,  open a new form project and add a second form (Form2) to the project.  Set Form2's StartPosition property to CenterScreen.

     Now go to the (Project -> Properties -> Settings) and add a setting named Form2_Bounds (as seen in image below).  Then you need to set it's Type to a Rectangle.  To use a Rectangle type,  you need to open the Drop Down and select (Browse) at the bottom (as seen in image below).  I already did this beforehand as can be seen,  this is just to show you the steps.

     

     After selecting (Browse),  you will need to open these nodes to select a Rectangle type.  (System.Drawing -> System.Drawing) as seen in the image.  Then you can select the (Rectangle) type and press the (Ok) button.

     

     Now you can add 1 Button (Button1) to the main form (Form1) and use this code for Form1.

    Imports System.ComponentModel
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f2 As New Form2
            If My.Settings.Form2_Bounds <> Rectangle.Empty Then 'if Form2_Bounds setting is 0,0,0,0 then it has not been opened yet
                f2.StartPosition = FormStartPosition.Manual
                f2.Bounds = My.Settings.Form2_Bounds
            End If
            f2.Show()
        End Sub
    
        Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
            My.Settings.Reset() 'resets the Form2_Bounds setting back to it's default 0,0,0,0
        End Sub
    
    End Class
     

     Now add this code to Form2.

    Imports System.ComponentModel
    
    Public Class Form2
        Private Sub Form2_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
            My.Settings.Form2_Bounds = Me.Bounds 'set the Form2_Bounds setting to the current Form2 bounds
        End Sub
    End Class
     

     You will just need to add a Rectangle setting for each of your forms and implement the code shown above for each of your Forms.

     Here is an animated image showing the above example in action...


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Sunday, December 3, 2017 9:09 PM
    • Marked as answer by Claudio111 Thursday, December 7, 2017 5:43 PM
    Sunday, December 3, 2017 6:29 PM
  • Claudio111,

     I see you have not replied back or marked any answers in this thread for a few days now.  Has your question been answered?  If it has,  then please remember to mark the post or posts that have answered your question as the answer(s).  If you are still having a problem with one of the examples or suggestions that have been given to you,  then let us know so we can help solve the problem.  Thanks.


    If you say it can`t be done then i`ll try it

    Wednesday, December 6, 2017 8:46 PM