?
Solved

Close the form

Posted on 2014-09-18
30
Medium Priority
?
62 Views
Last Modified: 2015-09-10
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?
0
Comment
Question by:HuaMinChen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 7
  • 6
  • +1
30 Comments
 
LVL 8

Expert Comment

by:Ganapathi
ID: 40331951
this.Close();

where "this" is the currently actice form.
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 40331972
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
 
LVL 2

Expert Comment

by:Vishal Patil
ID: 40331981
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Author Comment

by:HuaMinChen
ID: 40331987
Thanks.
Yes, it is the only form within the project. How to quit the application, upon that the user has clicked the button?
0
 
LVL 8

Expert Comment

by:Ganapathi
ID: 40331989
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
 
LVL 8

Expert Comment

by:Ganapathi
ID: 40331990
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
 
LVL 2

Expert Comment

by:Vishal Patil
ID: 40332007
Hello,

Me.Close() on Form1 itself will close the application.
Put this inside Button click event.
0
 
LVL 8

Expert Comment

by:Ganapathi
ID: 40332011
Are you calling the ShowDialog() method more than once without closing the previous form instance?
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 40332019
Sorry, I put
Environment.Exit(0)

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

Expert Comment

by:Vishal Patil
ID: 40332026
Hello,

Me.Close() on Form1 itself will close the application.
Put this inside Button click event.
0
 
LVL 8

Expert Comment

by:Ganapathi
ID: 40332035
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
 
LVL 15

Expert Comment

by:ChloesDad
ID: 40332063
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
 
LVL 11

Author Comment

by:HuaMinChen
ID: 40332085
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
 
LVL 11

Author Comment

by:HuaMinChen
ID: 40332090
Chloes,
I do not see IsCancel property to the button. I'm using VS 2013.
0
 
LVL 8

Expert Comment

by:Ganapathi
ID: 40332095
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
 
LVL 11

Author Comment

by:HuaMinChen
ID: 40332108
Many thanks Ganapathi.
Should I choose

Test > Debug > All Tests

within VS, to directly debug the project?
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 40332133
Any other help to this?
0
 
LVL 8

Expert Comment

by:Ganapathi
ID: 40332171
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
 
LVL 11

Author Comment

by:HuaMinChen
ID: 40332175
Many thanks.
Sorry, do you mind to see my project above? It seems I cannot directly run it.
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 40332340
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 40334142
But the point is, there is no such code

IsCancel="true"

within the project.
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 40334346
Exactly, Its a property of the button. Its default value will be False, change it to True in the form designer.
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 40335222
Sorry, do you mind to see my project, as within that, I really do not see such option to the button?
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 40335264
Please post your project and I will look at it
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 40335333
0
 
LVL 15

Accepted Solution

by:
ChloesDad earned 1500 total points
ID: 40335405
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
 
LVL 11

Author Comment

by:HuaMinChen
ID: 40335854
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
 
LVL 15

Expert Comment

by:ChloesDad
ID: 40336037
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
 
LVL 11

Author Comment

by:HuaMinChen
ID: 40336052
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
 
LVL 11

Author Comment

by:HuaMinChen
ID: 40336053
Just to detect button click from the form and pass it to the VB script.
0

Featured Post

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question