Results 1 to 11 of 11

Thread: Showing and hiding a form[Solved]

  1. #1

    Thread Starter
    Addicted Member haihems's Avatar
    Join Date
    Oct 2004
    Posts
    150

    Resolved Showing and hiding a form[Solved]

    Hi,

    I know vb6 but i'm new to vb.net.

    In vb.net windows forms, I like to show from2 by clicking a button in from1. At that time I dont want the from1 still open (I want to close it or hide it). After working in form2 by clicking the button in that from, the form1 should be open.What can i do for that?

    I did the following, but it is not working.....

    vb coding:
    'Form1 button click

    Dim x as from2
    x.show()
    me.hide()

    'Form2 button click

    dim y as form1
    me.close()
    y.show()

    This thing is working like , it hides form1 and show form2, after that while i click the button in form2 it closed and the form1 is shown. But when i closed the window vb couldn't close the project, it still running.

    Please tell me the solution.

    Thanks...

    haihems
    Last edited by haihems; Oct 19th, 2004 at 01:06 AM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Just a few threads below yours...

    http://www.vbforums.com/showthread.p...hreadid=309022


    If you do a search, you'll find lots of threads about this same issue. It's a popular one.

  3. #3
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036
    Hi

    put the following in form2 class declaration
    VB Code:
    1. Private _form1_ As form1
    2. Public Property MyForm1() As form1
    3.         Get
    4.             Return _form1_
    5.         End Get
    6.         Set(ByVal Value As String)
    7.             _form1_ = Value
    8.         End Set
    9. End Property

    form1 button1 click
    VB Code:
    1. dim f as new form1
    2. f.MyForm1 = me
    3. f.show
    4. me.hide

    form2 button1 click
    VB Code:
    1. if _form1_ is nothing then
    2.    dim f as new form1
    3.   f.show
    4.   me.close
    5. else
    6.   _form1_.show
    7.   me.close
    8. end if

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  4. #4
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Uhmmmm....I think your code doesn't work just as you wrote, probably by memory, I guess!

    Dim x as from2
    x.show()
    me.hide()

    I think you wanted to write
    Dim x as NEW form2
    ....etc...

    Anyway, the point is another one! The solution to ur mistery is you don't close Form1, that probably is your starting object (Project---->Properties). You reistance a new form1:

    dim y as form1 (you need as NEW....anyway!)
    me.close()
    y.show()

    So you have to different instances of Form1. The first one was generated when your application started, the second one was generated by clicking button in form2.

    You need only to write (in Form2's button click event)

    Form1.show

    So you reveal the previously hidden form!

    To be sure that you can refer Form1, you should change your startup object (?)(.... I have italian version, so I'm not sure about terms used in the english one!) of the project, choosing Sub Main. So you can declare your Form1 as public in a module (the same where you put the sub main, generally) and so it will be referable from any point of your project. Something like:


    Module Module1
    Public F1 As Form1
    Public F2 As Form2

    Public Sub Main()
    F1 = New Form1
    Windows.Forms.Application.Run(F1)
    End Sub

    End Module

    You can also use the shorter way, declaring and instancing at the same time, but I suggest you to keep habit to the longer one:

    Module Module1
    Public F1 As NEW Form1
    Public F2 As NEW Form2

    Public Sub Main()
    Windows.Forms.Application.Run(F1)
    End Sub

    End Module


    Good morning and good job, friend!
    Live long and prosper (Mr. Spock)

  5. #5
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    In time I was composing my post Mendhak and Asgorath gave their answers....

    How fast you are, friends!
    Live long and prosper (Mr. Spock)

  6. #6
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036
    Originally posted by alextyx
    In time I was composing my post Mendhak and Asgorath gave their answers....

    How fast you are, friends!
    Hi alextyx , i being using properties to access for forms for a long time its just a matter of changing the name of the forms
    "The dark side clouds everything. Impossible to see the future is."

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    In fact, mendhak traveled back in time to answer someone else, then he came back and linked to the existing answer.

  8. #8

    Thread Starter
    Addicted Member haihems's Avatar
    Join Date
    Oct 2004
    Posts
    150

    Cool Reg. showing and hiding forms

    Hi Asgorath

    Thanks for your reply...

    I've tried your coding, but i got the following 3 build errors:

    1) 'MyForm1' is not a member of 'WindowsApplication3.Form1'.
    2) 'Set' parameter must have the same type as the containing property.
    3) Value of type 'String' cannot be converted to 'WindowsApplication3.Form1'.

    Why it is coming like this and what can I do for that????

    Please tell me...


  9. #9
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472
    hii...
    in the form 1:
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim f As New Form2(Me)
    3.         f.Show()
    4.         Me.Hide()
    5.     End Sub
    in the form2:
    VB Code:
    1. Public Sub New(ByVal f As Form)
    2.         MyBase.New()
    3.  
    4.         'This call is required by the Windows Form Designer.
    5.         InitializeComponent()
    6.         Me.f = f
    7.         'Add any initialization after the InitializeComponent() call
    8.  
    9.     End Sub
    10.  
    11. Dim f As Form1
    12.     Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    13.         Me.Hide()
    14.         f.Show()
    15.     End Sub

  10. #10
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036
    Hi haihems
    Corrected the errors should work now ...

    put the following in form2 class declaration
    VB Code:
    1. Private _form1_ As form1
    2. Public Property MyForm1() As form1
    3.         Get
    4.             Return _form1_
    5.         End Get
    6.         Set(ByVal Value As form1)
    7.             _form1_ = Value
    8.         End Set
    9. End Property

    form1 button1 click
    VB Code:
    1. dim f as new form2
    2. f.MyForm1 = me
    3. f.show
    4. me.hide


    form2 button1 click
    VB Code:
    1. if _form1_ is nothing then
    2.    dim f as new form1
    3.   f.show
    4.   me.close
    5. else
    6.   _form1_.show
    7.   me.close
    8. end if

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  11. #11

    Thread Starter
    Addicted Member haihems's Avatar
    Join Date
    Oct 2004
    Posts
    150

    Thumbs up

    Hi all,

    Thanks for ur help...now i know how to show & hide a form (but not so easy as VB6...isn't it?) Any how Thanks once again.

    Regards,
    Hems.

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