Results 1 to 13 of 13

Thread: [RESOLVED] 2 quick questions about forms

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    61

    Resolved [RESOLVED] 2 quick questions about forms

    1. How do I stop users of my programs moving the form around? I want it to remain static while the program is being used

    2. If I have 2 forms on the screen at the same time is there any way that they can be minimized together i.e. if you minimize one the other is mimimized automatically?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: 2 quick questions about forms

    1. Follow the relevant link in my signature.

    2. You can create an owned form. When you display the second form pass the first form as an argument to Show, e.g.
    vb.net Code:
    1. Dim f2 As New Form2
    2.  
    3. f2.Show(Me)
    That will cause the second form to remain on top of the first form, even if the first form has focus. The second form will also be minimised and closed when the first form is.

    If that's not the behaviour you want then you'd have to make each form handle the SizeChanged event of the other. In each event handler you set the current form's WindowState to the same value as the form that raised the event:
    vb.net Code:
    1. Me.WindowState = DirectCast(sender, Form).WindowState
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    61

    Re: 2 quick questions about forms

    Thanks jmcilhinney

    I've managed to get the minimizing business sorted but I'm having a few problems making the form(s) immovable. I followed the Disable Close Button / Immovable Form link in your sig but I have a few questions

    1. How do you add those files to a project?
    2. Do I need to put an Imports statement at the top of each form that uses those files?
    3.Will either of the two files allow me to make forms immovable?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: 2 quick questions about forms

    1. Right-click your project in the Solution Explorer and select Add -> Existing Item.

    2. Do you know what an Imports statement does? If not, go to the MSDN Library and find out. Now ask yourself whether adding this file to your project means that you need it for this.

    3. That question is specifically addressed in the very first post in that thread. I don't write those posts for people to ignore and then ask me to repeat myself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: 2 quick questions about forms

    If you don't want the user to move your form or resize it then just change the forms locked property to true.

  6. #6
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: 2 quick questions about forms

    Quote Originally Posted by noahssite
    If you don't want the user to move your form or resize it then just change the forms locked property to true.
    Locked property doesn't work for forms, it only locks the controls placed on a form.

  7. #7
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: 2 quick questions about forms

    Quote Originally Posted by Deepak Sakpal
    Locked property doesn't work for forms, it only locks the controls placed on a form.
    Oh... whell then you can always handle the form_move event and also in the form properties you can set a min. and max. size so just change the min and max to the forms size. Or you can just handle the form_size event too.

  8. #8
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: 2 quick questions about forms

    Quote Originally Posted by noahssite
    Oh... whell then you can always handle the form_move event and also in the form properties you can set a min. and max. size so just change the min and max to the forms size. Or you can just handle the form_size event too.
    yes... than can be a way.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: 2 quick questions about forms

    The Locked property refers to design-time behaviour of controls only, i.e. they cannot be dragged on the design surface.

    Handling events to prevent the form being moved or resized is a bit of a hack because it relies on changing the position or size back after the fact, so the user will see some ugly repainting. The way my FormImmobiliser class works is to prevent the Location property from ever changing, rather than changing it back after it has changed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    61

    Re: 2 quick questions about forms

    OK guys, I've managed to get it woring using the following code

    Code:
      Private immobiliser As New FormImmobiliser(Me)
    along with the FormImmobiliser.vb file. The problem is that for one of the forms I am using I am setting its position manuallly using the code

    Code:
            Me.Top = 500
            Me.Left = 0
    in the forms load event. Of course because I am declaring immobiliser at the top of the code it is immmobilising the form before its position can be set. Is there any way to get around this

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: 2 quick questions about forms

    Add an Enabled property to your FormImmobiliser class:
    vb.net Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class FormImmobiliser
    4.     Inherits NativeWindow
    5.  
    6.     Private Structure WindowPos
    7.         Public hwnd As Int32
    8.         Public hWndInsertAfter As Int32
    9.         Public x As Int32
    10.         Public y As Int32
    11.         Public cx As Int32
    12.         Public cy As Int32
    13.         Public flags As Int32
    14.     End Structure
    15.  
    16.     Private Const WM_WINDOWPOSCHANGING As Int32 = &H46
    17.  
    18.     Private WithEvents target As Form
    19.  
    20.     Private _enabled As Boolean = True
    21.  
    22.     Public Property Enabled() As Boolean
    23.         Get
    24.             Return Me._enabled
    25.         End Get
    26.         Set(ByVal value As Boolean)
    27.             Me._enabled = value
    28.         End Set
    29.     End Property
    30.  
    31.  
    32.     Public Sub New(ByVal target As Form)
    33.         Me.target = target
    34.     End Sub
    35.  
    36.     Private Sub target_HandleCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles target.HandleCreated
    37.         AssignHandle(Me.target.Handle)
    38.     End Sub
    39.  
    40.     Private Sub target_HandleDestroyed(ByVal sender As Object, ByVal e As System.EventArgs) Handles target.HandleDestroyed
    41.         ReleaseHandle()
    42.     End Sub
    43.  
    44.     Protected Overrides Sub WndProc(ByRef m As Message)
    45.         If Me._enabled AndAlso m.Msg = WM_WINDOWPOSCHANGING Then
    46.             Dim pos As WindowPos = DirectCast(Marshal.PtrToStructure(m.LParam, _
    47.                                                                      GetType(WindowPos)),  _
    48.                                               WindowPos)
    49.  
    50.             pos.x = Me.target.Left
    51.             pos.y = Me.target.Top
    52.  
    53.             Marshal.StructureToPtr(pos, m.LParam, True)
    54.         End If
    55.  
    56.         MyBase.WndProc(m)
    57.     End Sub
    58.  
    59. End Class
    Now you just set its Enabled property to False if you want to be able to move it. You might also like to make the default value of the property True and/or add another constructor that let's you set it when you create it. That said, if you're using VB 2008 then you can use initialise syntax:
    vb.net Code:
    1. Private immobiliser As New FormImmobiliser(Me) With {.Enabled = False}
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Lively Member
    Join Date
    May 2008
    Posts
    117

    Re: 2 quick questions about forms

    Hi micki_free!

    Hmm. By default I don't like designs like that.
    IMO a user has the right to move and size his forms as he wants.

    And having several Forms running araound in one Application I don't like either (by default). Because that causes a pretty bunch of programming-problems (what, if user clicks away one of them?...), not easy to handle, mostly handled by restricting users freedom to treat his Forms as a common Windows Form.

    Is there no possibility to design ur application in one-form-manner (eg. using TabControl, UserControls and stuff)?

    (Me myself always found a possibility to do so)
    whats the benefit of beeing rated?


  13. #13

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    61

    Re: 2 quick questions about forms

    Thank you once again jmcilhinney

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width