Solved

Close the form

Posted on 2014-09-18
30
56 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
  • 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 10

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
 
LVL 10

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 10

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 10

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 10

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 10

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 10

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 10

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 10

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 10

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 10

Author Comment

by:HuaMinChen
ID: 40335333
0
 
LVL 15

Accepted Solution

by:
ChloesDad earned 500 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 10

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 10

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 10

Author Comment

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now