Close a window in C#

Posted on 2016-09-14
Last Modified: 2016-10-05
Hello Experts,
In ASP.NET and C#, I am trying to close a window if there is no error in the form.  If error exists, I don't want to close it.  Is there a way?

Thank you in advance.
Question by:RadhaKrishnaKiJaya
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
  • 6
  • 4
LVL 44

Expert Comment

ID: 41799223
air code in C#:


Should close the form running the above code.  You would need to code the NoErrorOnForm function yourself to check for an error, it should return true if everything was OK.

Author Comment

ID: 41799229
Hello AndyAinscow,
Thank you for your reply.  I have btnModify_Click event.  Inside it I do all kind of validations.  I want to close the form if everything is valid.  Otherwise I want to leave the form as it is.  I have already tried this.Close().  But it does not compile.

Thank you!
LVL 44

Expert Comment

ID: 41799230
>>But it does not compile.

What is the compile error?  Can you post the code and indicate the line(s) the compiler objects to.
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.


Author Comment

ID: 41799238
Error      92      'ProjectName.ProgramName' does not contain a definition for 'Close' and no extension method 'Close' accepting a first argument of type 'ProjectName.ProgramName' could be found (are you missing a using directive or an assembly reference?)

Author Comment

ID: 41799245
Hey Andy,  It is already 2:00am.  I will reply you back in the morning.  Thank you!
LVL 44

Expert Comment

ID: 41799248
OK, no problems.
Are you running this code on a form - Close is a method a Form would have built in (but not some other class in general).

Author Comment

ID: 41799778
Yes, I am running in a Form.
LVL 44

Expert Comment

ID: 41799786
Please post the code - that compile error doesn't make sense

Accepted Solution

RadhaKrishnaKiJaya earned 0 total points
ID: 41807080
Due to strict company policy in place, I will not be able to post my code.  If you have a sample program (could be small), please provide me.  Here is the JavaScript code I found and working.

string strScript = "window.close();";
ScriptManager.RegisterStartupScript(this, typeof(string), "key", strScript, true);

Thank you for your help!

Author Closing Comment

ID: 41829528
Since I did find any better solution, I need to close this question.

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What!!!??? 5 63
C# Change the instruction to user. 11 39
What is needed to become a DBA? 7 56
.NET Core supports which cell phone platforms? 3 38
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

710 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