Close the form

Hi,
I show the relevant form, like
Public Class MyFunction
    Public Shared frm As New Form1()
    Public Shared s_ret As String = ""

    Public Function Func1(ByVal Value1 As String, ByVal Value2 As String, ByVal Value3 As String) As String
        frm.ShowDialog()
        ...

Open in new window


within the class above, that is on the same project. How to close the form

Public Class Form1

    Protected Sub n_Click(ByVal sender As Object, ByVal e As EventArgs)
        ...

Open in new window

       
within the code-behind event of the form above?
LVL 11
HuaMin ChenProblem resolverAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GanapathiFacets DeveloperCommented:
this.Close();

where "this" is the currently actice form.
0
HuaMin ChenProblem resolverAuthor Commented:
I use Me.Close() to close it as this.Close is not accepted. But when I click the button, when running the recompiled project, the form cannot be closed. why?
0
Vishal PatilSoftware EngineerCommented:
Hello,

Put following code inside n_Click Event.

If Me.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.Close()
End If


But please take care that if Form1 is the only form in your solution then you will not be able to Close it.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

HuaMin ChenProblem resolverAuthor Commented:
Thanks.
Yes, it is the only form within the project. How to quit the application, upon that the user has clicked the button?
0
GanapathiFacets DeveloperCommented:
You can go with this.Hide() as well. This will not close the form, but will still be there in the memory. If you feel that your form does not have much controls, then you can go with this.

See if it works.
0
GanapathiFacets DeveloperCommented:
To quit the entire application and exit. Use this. Where 0 is the code that you are returning to the source application that calls this. You can pass 0 by default.
Environment.Exit(0)

Open in new window

0
Vishal PatilSoftware EngineerCommented:
Hello,

Me.Close() on Form1 itself will close the application.
Put this inside Button click event.
0
GanapathiFacets DeveloperCommented:
Are you calling the ShowDialog() method more than once without closing the previous form instance?
0
HuaMin ChenProblem resolverAuthor Commented:
Sorry, I put
Environment.Exit(0)

to the click event, but after I've clicked the button, the form is still there.
0
Vishal PatilSoftware EngineerCommented:
Hello,

Me.Close() on Form1 itself will close the application.
Put this inside Button click event.
0
GanapathiFacets DeveloperCommented:
It must close all the forms and exit to the OS instantly. You are doing something wrong. Try these options and see if you get it.

Do a clean up and build it again.

Try placing break-points on each line of your code and debug it.

Dispose all forms in your Final clause or call the Garbage collector, let it clean the mess for you.

Restart the VS and try it.
0
ChloesDadCommented:
Set the IsCancel property of the button to true

Taken from the MSDN page

If a window, opened by calling ShowDialog, and with a Button with its IsCancel property set to true, will automatically close when the button is either clicked, or ESC is pressed. If the window was opened using Show, however, Close must be explicitly called, such as from Click event handler for the Button.
0
HuaMin ChenProblem resolverAuthor Commented:
Many many Ganapathi,
I did try the way within this project
https://dl.dropboxusercontent.com/u/40211031/CSDemo.zip

and did re-register it within Win 7 machines, by these steps

Register COM DLL file in Windows 7
Start --> Run --> comexp.msc
Navigate to Console Root - Componet Services - Computers - My Computer - COM+ Applications.
Right click on 'COM+ Applications' and create New Application.
Follow the wizard carefully. Hit Next. Create an Empty Application. Name it (mycomapp1 for instance). Make sure Server application radio box is selected. Pick the user account based on the requirement. Hit Next. Next. Finish.
Now you have created a COM+ application named mycomapp1.
Now, you need to add individual COM components to it.
Expand mycomapp1 from the list. Right click on Components. Pick New Component. 
Hit Next in the wizard. This next wizard screen is up to you. I would pick 'Install New components' if I haven’t run a regsvr32 or regasm on it. Pick the told or the dll (containing the tlb) file. And go ahead.. That’s how you do it.

Open in new window


and use this VB script to raise the form

https://dl.dropboxusercontent.com/u/40211031/CallDotNetDll.vbs

but really the button click event cannot close the form, using your suggested way.
0
HuaMin ChenProblem resolverAuthor Commented:
Chloes,
I do not see IsCancel property to the button. I'm using VS 2013.
0
GanapathiFacets DeveloperCommented:
Is the function actually called on click_event? Did you try debugging it?

Try adding a break-point at the beginning of the method. Run the application and click the button, see if it calls the method.
0
HuaMin ChenProblem resolverAuthor Commented:
Many thanks Ganapathi.
Should I choose

Test > Debug > All Tests

within VS, to directly debug the project?
0
HuaMin ChenProblem resolverAuthor Commented:
Any other help to this?
0
GanapathiFacets DeveloperCommented:
See. Before every line, near line number, you can click and a filled-red-circle(break-point) will appear. When you execute/run the application, it will start executing the statements. When it sees the Break-point the process will pause and give the control to you. By that way you can see if your button_click reaches your line of code. To proceed to the next line press F11 or to execute rest of the application press F5. Refer the attached.
Break point
0
HuaMin ChenProblem resolverAuthor Commented:
Many thanks.
Sorry, do you mind to see my project above? It seems I cannot directly run it.
0
ChloesDadCommented:
0
HuaMin ChenProblem resolverAuthor Commented:
But the point is, there is no such code

IsCancel="true"

within the project.
0
ChloesDadCommented:
Exactly, Its a property of the button. Its default value will be False, change it to True in the form designer.
0
HuaMin ChenProblem resolverAuthor Commented:
Sorry, do you mind to see my project, as within that, I really do not see such option to the button?
0
ChloesDadCommented:
Please post your project and I will look at it
0
HuaMin ChenProblem resolverAuthor Commented:
0
ChloesDadCommented:
This is a configured to be a dll, is that your intention?

When I change it to a exe so that I can run it and set the form to be the startup form, it works as it should using the me.close. It doesn't need the environment.close.

I then added a sub_main in a module so that I can create a new instance of the form using the code

Module Module1

    Public Sub Main()

        Dim fred As New Form1

        fred.ShowDialog()


    End Sub

End Module

Open in new window


again the form closes when I click on a button.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HuaMin ChenProblem resolverAuthor Commented:
Many thanks.

I need to use it as a DLL. when I call the above project, from this VB script below
https://dl.dropboxusercontent.com/u/40211031/CallDotNetDll.vbs

I get the attached error. why?
t821.png
0
ChloesDadCommented:
This is now a completely different question from the one originally posted. I no nothing about running dlls in a script so I cant help anymore.

Why do you need to run a script rather than as an exe?
0
HuaMin ChenProblem resolverAuthor Commented:
As I need to show the relevant form and then also get value from the form, and pass it to the original VB script.
0
HuaMin ChenProblem resolverAuthor Commented:
Just to detect button click from the form and pass it to the VB script.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.