How do we close a modeless form in vb.net ?

dominico

Well-known member
Joined
Mar 9, 2005
Messages
57
Programming Experience
3-5
I need help with closing a modeless form. When I use form.close(), nothing happens. I can open it but cannot close it or hide it or focus it.

I also need to know how to check if it is under "Show" state or not.

Please advise me on how to do it.


Below are my code:

PrivateSub ShowForm(ByVal SomeForm As Form, ByVal doThis AsString)
SomeForm.WindowState = FormWindowState.Normal

SelectCase doThis

Case "Show"

SomeForm.Show()

SomeForm.Controls.Item(0).Text = sAll

Case "Off"

SomeForm.Close() ' This line has no affect on my MODELESS FORM

EndSelect

EndSub


Thank you everyone.

dominico
 
Last edited:
You are not reading my code properly. You have declared your form variable inside the the procedure and instantiated it on the same line:
VB.NET:
[size=2][color=#0000ff]Dim[/color][/size][size=2] DisplayScript [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff] New[/color][/size][size=2] frmDisplay[/size]
In my code the form variable is declared at the class-level and it is not instantiated anywhere except within the If statement.

This unrelated to your issue, but if you are going to use Hungarian notation it is the convention to give the class a regular name starting with an upper case letter and the prefix, e.g. "frm" gets added to any variables of that type. Notice that if you see code that uses that notation it would be something like:
VB.NET:
Dim strText As String
so the prefix is on the variable name, not the class name. This is so that you can easily distinguish the type of each variable as you look over the code.
 
jmcilhinney said:
You are not reading my code properly. You have declared your form variable inside the the procedure and instantiated it on the same line:
VB.NET:
[size=2][color=#0000ff]Dim[/color][/size][size=2] DisplayScript [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff] New[/color][/size][size=2] frmDisplay[/size]
In my code the form variable is declared at the class-level and it is not instantiated anywhere except within the If statement.

This unrelated to your issue, but if you are going to use Hungarian notation it is the convention to give the class a regular name starting with an upper case letter and the prefix, e.g. "frm" gets added to any variables of that type. Notice that if you see code that uses that notation it would be something like:
VB.NET:
Dim strText As String
so the prefix is on the variable name, not the class name. This is so that you can easily distinguish the type of each variable as you look over the code.

Okay sir, I fix it like you said but still not happening. I am missing something I guess.

Dim DisplayScript As New Form ' I declared this line at the top right under the class' name.

Sub DisplayMe(ByVal doThis As String)

DisplayScript.WindowState = FormWindowState.Normal

If DisplayScript Is Nothing OrElse DisplayScript.IsDisposed Then

'Form has not been created or has been disposed (i.e. closed).

DisplayScript = New frmDisplay

AddOwnedForm(DisplayScript)

DisplayScript.Show()

DisplayScript.Controls.Item(0).Text = sAll

Else

DisplayScript.Activate()

End If

End Sub

Note: as for the way I declare the variables, I think I learned it from VB tutorial that came with VB6. Are you sure it is not the right way ? I have seen other programmers usign the same format too. But anyway, too late for me to change now. I may do it on my next appl.
 
I said that you declare the form but don't instantiate it. The first line you provided is this:
VB.NET:
Dim DisplayScript As New Form
which creates a new Form object and assigns it to the variable, which means that it is not nothing and it is not disposed. This means that every time through the If statement it goes to the Else block and calls Activate on a form that has never been Shown, so nothing happens. Change that first line to this:
VB.NET:
Dim DisplayScript As frmDisplay
and everything should be OK.

Also, to debug issues like this you need to start using breakpoints and other debugging techniques. If you had done that you would have seen that execution never entered the section where you try to create the form and Show it. You could have then used the Locals or Watch window to check the values of variables to see why.
 
jmcilhinney said:
I said that you declare the form but don't instantiate it. The first line you provided is this:
VB.NET:
Dim DisplayScript As New Form
which creates a new Form object and assigns it to the variable, which means that it is not nothing and it is not disposed. This means that every time through the If statement it goes to the Else block and calls Activate on a form that has never been Shown, so nothing happens. Change that first line to this:
VB.NET:
Dim DisplayScript As frmDisplay
and everything should be OK.

Also, to debug issues like this you need to start using breakpoints and other debugging techniques. If you had done that you would have seen that execution never entered the section where you try to create the form and Show it. You could have then used the Locals or Watch window to check the values of variables to see why.

I did declare it the other way too(as frmDisplay) the first time but it went straight into the "Activate" method. That's why I declared it with "as form." I always use breakpoints to debug. I realized both cases did not invoke the "Show" method. Unless I change the IF statement to "NOT" but then once I close the form, I cannot get it back up. Anyway, I changed it as instructed and it still won't execute the "Show" method.
 
The fact that you said "nothing happens" rather than "it always enters the Else block" or something like that led me to believe that you hadn't stepped through the code. If you're trying to get help on an error you should always be explicit about what that error is. In this case, the fact that you didn't see the window appear doesn't mean that nothing happened.
 
jmcilhinney said:
The fact that you said "nothing happens" rather than "it always enters the Else block" or something like that led me to believe that you hadn't stepped through the code. If you're trying to get help on an error you should always be explicit about what that error is. In this case, the fact that you didn't see the window appear doesn't mean that nothing happened.

hehehe...you got me, bro. It works after I remove the "New." I don't understand why "New" didn't work. My apology, I will be explicit next time. I thought you knew my phrase "nothing happens" because I did mention in my very first post that vb ignored my "Close()" statement (SomeForm.Close() ' This line has no affect on my MODELESS FORM)

So when I stated "Nothing happens" I meant VB ignoring my command. Anyways, I thank you with all my heart. I wish I could make you the president of DownUnder. :))
 
All's well that ends well. The reason it wasn't working with the "New" there is because by doing that you were creating an object and assigning it to the variable. That way, every time your code got to the If statement the variable was not Nothing and it was not disposed, so it never got into the first section to display the form by calling Show.
 
jmcilhinney said:
All's well that ends well. The reason it wasn't working with the "New" there is because by doing that you were creating an object and assigning it to the variable. That way, every time your code got to the If statement the variable was not Nothing and it was not disposed, so it never got into the first section to display the form by calling Show.


I thought "New" was being used to create a fixed memory space for an object. I sure learned a lot from you. My hat is off to you. You are an exceptional programmer. I can't argue that. Keep up the good work. You shall be rewarded my friend.

So do you own this forum ? Do you get paid for being an advisor ?

God bless you.
 
Back
Top