[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?
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:
Dim f2 As New Form2
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:
Me.WindowState = DirectCast(sender, Form).WindowState
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?
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.
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.
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.
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.
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.
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.
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
Re: 2 quick questions about forms
Add an Enabled property to your FormImmobiliser class:
vb.net Code:
Imports System.Runtime.InteropServices
Public Class FormImmobiliser
Inherits NativeWindow
Private Structure WindowPos
Public hwnd As Int32
Public hWndInsertAfter As Int32
Public x As Int32
Public y As Int32
Public cx As Int32
Public cy As Int32
Public flags As Int32
End Structure
Private Const WM_WINDOWPOSCHANGING As Int32 = &H46
Private WithEvents target As Form
Private _enabled As Boolean = True
Public Property Enabled() As Boolean
Get
Return Me._enabled
End Get
Set(ByVal value As Boolean)
Me._enabled = value
End Set
End Property
Public Sub New(ByVal target As Form)
Me.target = target
End Sub
Private Sub target_HandleCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles target.HandleCreated
AssignHandle(Me.target.Handle)
End Sub
Private Sub target_HandleDestroyed(ByVal sender As Object, ByVal e As System.EventArgs) Handles target.HandleDestroyed
ReleaseHandle()
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
If Me._enabled AndAlso m.Msg = WM_WINDOWPOSCHANGING Then
Dim pos As WindowPos = DirectCast(Marshal.PtrToStructure(m.LParam, _
GetType(WindowPos)), _
WindowPos)
pos.x = Me.target.Left
pos.y = Me.target.Top
Marshal.StructureToPtr(pos, m.LParam, True)
End If
MyBase.WndProc(m)
End Sub
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:
Private immobiliser As New FormImmobiliser(Me) With {.Enabled = False}
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)
Re: 2 quick questions about forms
Thank you once again jmcilhinney