Set Form Location and Size before calling Show method

M

mark

I am developing an application in .Net C# that needs to restore a
number of tool windows to some previous location and size. The problem
I have is that when I create the form and set the Location and Size
properties before calling the Show method, the form is not created in
the Location that I set. If I call Show first everything works fine but
there is excesive flicker when the Location and Size properties are
set.

I tried calling CreateControl() first but it does not seem to make a
differnce. Is there any way I can move and resize a created form before
showing it?
 
J

Josip Habjan

Hi,

I wrote this for LayoutSaver component, this class will raise me event
before Form_Load event and there i can set location and size of dialog.
Before you call MyForm.ShowDialog put 'ReferencedForm reffrm = new
ReferencedForm(MyForm)' and set event handler for BeforeLoadEvent. There you
will set location and size for your form.
Code in vb.net, but i hope it will help you.

--
Public Class ReferencedForm
Inherits NativeWindow

Private Const WM_SHOWWINDOW As Integer = &H18

Private _originalform As Form
Private _ishandleset As Boolean
Private _beforeloadraised As Boolean = False

Public Event BeforeLoadEvent As EventHandler

Sub New(ByVal originalform As Form)
If Not originalform Is Nothing Then
_originalform = originalform
AddHandler _originalform.HandleCreated, AddressOf
Me.OnHandleCreated
AddHandler _originalform.HandleDestroyed, AddressOf
Me.OnHandleDestroyed
End If
End Sub

Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_SHOWWINDOW Then
If Not _beforeloadraised Then
RaiseEvent BeforeLoadEvent(Me, New EventArgs)
_beforeloadraised = True
End If

End If

MyBase.WndProc(m)
End Sub

Private Sub OnHandleCreated(ByVal sender As Object, ByVal e As
EventArgs)
AssignHandle(CType(sender, Form).Handle)
End Sub

Private Sub OnHandleDestroyed(ByVal sender As Object, ByVal e As
EventArgs)
_originalform = Nothing
ReleaseHandle()
End Sub

Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub

End Class
 
G

Guest

Set the StartPosition to Manual

viz

Form f = new Form();
f.StartPosition = FormStartPosition.Manual;

Nigel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top