Hi Everyone,

I want to create program, with different forms, in that it would have a main form in which if the user decides to select other forms, it would change right in the main form, so disallowing the user from noticing that he/she is viewing another form.

Is it possible to make this happen?

Recommended Answers

All 14 Replies

i think maybe you would make all the other form borderless then show'em inside the main form.
but you should use the setParent API function.
make new module :

Imports System.Runtime.InteropServices
Module Module1
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Public Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    End Function
End Module

then just write form2.show() in the button event and in it's load or shown (form2.shown) event write setParent(form2.handle,mainForm.handle) and maybe form2.location to adjust the location inside the mainform.
or BringToFront() and SendToBack() to control the z-coords of that shown form .

Please elaborate.

If you are saying change a Forms appearance on Button Click yes that is possible.

Hi finito

I don't mean changing a Forms appearance, I mean switching between different forms on button click in a main form in which the user doesn't notice he/she is switching between forms

Thanks

Hi ÜnLoCo

What if i have more than 2 forms, will i still have to use the same approach or need add anymore code?

Just wanna ask where should i connect this code?. I Follow the instruction but there is still a run time error. can you give us a step by step procedure???

I love VB.net - Spiritual Growth

Just wanna ask where should i connect this code?. I Follow the instruction but there is still a run time error. can you give us a step by step procedure???

make a new module and paste all this code in it

Imports System.Runtime.InteropServices
Module Module1
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Public Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    End Function
End Module

then make a button or more and name them "show form 2" "show form 3" etc
on each button just write form2.show() or form3.show() etc ..
then
on the load of each form you should write

setParent(me.handle,form1.handle) ' make form1 parent of this form

Thanks for your help, it worked fine as expected...

But another problem i ran into was, when i try to resize the Main Form(parent form), the child form did not resize with it.

Please could you tell me how to control this... thanks!!

i think you could use the form1.ResizeEnd the event

insideForm is the form inside form1

Private Sub Form1_ResizeEnd(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ResizeEnd
'caution: you'll need a try catch or an If Not insideForm.Equals(Nothing) to avoid exceptions.
        insideForm.size = New Point(Me.Width - 100, Me.Height - 100)
    End Sub

insideForm is declared this way.

'declared inside form1 code
Public insideForm As Form = Nothing

you can assign a value to it after the setParent line.
like this maybe

'inside form2 code for example
setParent(me.handle,form1.handle) ' make form1 parent of this form
form1.insideForm = me

and you could make use of a properly anchored panel inside form1 and change the insideForm.size.. line to

insideForm.size = Panel1.size

wish you luck

Thanks once again

Please i'm finding it difficult to control, since i have to close one in order to open another form still within the main form. I know i need to check if a form is open

Please guide through steps on how to achieve this task?

Sorry Thanks once again

Please how do i check if a form is open, that is if i have 3 forms, still using the same concept of showing them on a main form.
I mean so i could be able to close a form in order to open another one, and aviod opening more than one form.

opening more than a form is not really a problem because you can add a BringToFront() on every load event of the forms you want to show inside form1.
but if you want you can check if FormX.Visible is true (means the form is not closed thus open)
but the best way is this way i think

Dim OpenForms = Application.OpenForms 'get all open forms includin this one of course
        For Each _form As Form In OpenForms
            If Not (_form.Equals(Me)) Then 'if it's not this form (the main form (form1)) then
                _form.Close() 'close it
            End If
        Next
        'then open another one

Hi u said u can make the form borderless how can you do that, would you just give the steps..please thnks

me.FormBorderStyle = Windows.Forms.FormBorderStyle.None

in the load event of each form you want borderless
or just look for FormBorderStyle in the properties panel of the form and set it to none
Greetz

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.