Link to home
Start Free TrialLog in
Avatar of Peter Chan
Peter ChanFlag for Hong Kong

asked on

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?
Avatar of Ganapathi
Ganapathi
Flag of India image

this.Close();

where "this" is the currently actice form.
Avatar of Peter Chan

ASKER

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?
Avatar of Vishal Patil
Vishal Patil

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.
Thanks.
Yes, it is the only form within the project. How to quit the application, upon that the user has clicked the button?
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.
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

Hello,

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

to the click event, but after I've clicked the button, the form is still there.
Hello,

Me.Close() on Form1 itself will close the application.
Put this inside Button click event.
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.
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.
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.
Chloes,
I do not see IsCancel property to the button. I'm using VS 2013.
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.
Many thanks Ganapathi.
Should I choose

Test > Debug > All Tests

within VS, to directly debug the project?
Any other help to this?
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.
User generated image
Many thanks.
Sorry, do you mind to see my project above? It seems I cannot directly run it.
But the point is, there is no such code

IsCancel="true"

within the project.
Exactly, Its a property of the button. Its default value will be False, change it to True in the form designer.
Sorry, do you mind to see my project, as within that, I really do not see such option to the button?
Please post your project and I will look at it
ASKER CERTIFIED SOLUTION
Avatar of ChloesDad
ChloesDad
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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?
As I need to show the relevant form and then also get value from the form, and pass it to the original VB script.
Just to detect button click from the form and pass it to the VB script.