|
-
Oct 18th, 2004, 03:19 AM
#1
Thread Starter
Addicted Member
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.
-
Oct 18th, 2004, 03:36 AM
#2
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.
-
Oct 18th, 2004, 03:44 AM
#3
Hi
put the following in form2 class declaration
VB Code:
Private _form1_ As form1
Public Property MyForm1() As form1
Get
Return _form1_
End Get
Set(ByVal Value As String)
_form1_ = Value
End Set
End Property
form1 button1 click
VB Code:
dim f as new form1
f.MyForm1 = me
f.show
me.hide
form2 button1 click
VB Code:
if _form1_ is nothing then
dim f as new form1
f.show
me.close
else
_form1_.show
me.close
end if
Regards
Jorge
"The dark side clouds everything. Impossible to see the future is."
-
Oct 18th, 2004, 03:51 AM
#4
Hyperactive Member
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)
-
Oct 18th, 2004, 03:56 AM
#5
Hyperactive Member
In time I was composing my post Mendhak and Asgorath gave their answers....
How fast you are, friends!
Live long and prosper (Mr. Spock)
-
Oct 18th, 2004, 04:02 AM
#6
"The dark side clouds everything. Impossible to see the future is."
-
Oct 18th, 2004, 04:03 AM
#7
In fact, mendhak traveled back in time to answer someone else, then he came back and linked to the existing answer.
-
Oct 18th, 2004, 09:29 PM
#8
Thread Starter
Addicted Member
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...
-
Oct 19th, 2004, 12:30 AM
#9
Hyperactive Member
hii...
in the form 1:
VB Code:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New Form2(Me)
f.Show()
Me.Hide()
End Sub
in the form2:
VB Code:
Public Sub New(ByVal f As Form)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
Me.f = f
'Add any initialization after the InitializeComponent() call
End Sub
Dim f As Form1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Hide()
f.Show()
End Sub
-
Oct 19th, 2004, 02:41 AM
#10
Hi haihems
Corrected the errors should work now ...
put the following in form2 class declaration
VB Code:
Private _form1_ As form1
Public Property MyForm1() As form1
Get
Return _form1_
End Get
Set(ByVal Value As form1)
_form1_ = Value
End Set
End Property
form1 button1 click
VB Code:
dim f as new form2
f.MyForm1 = me
f.show
me.hide
form2 button1 click
VB Code:
if _form1_ is nothing then
dim f as new form1
f.show
me.close
else
_form1_.show
me.close
end if
Regards
Jorge
"The dark side clouds everything. Impossible to see the future is."
-
Nov 1st, 2004, 12:24 AM
#11
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|