[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 654
  • Last Modified:

How to close Win Form without User Interaction

Hi

I have a windows Form which display reports using Crystal Report Object & then export the reports in .pdf format & save it.
This application need to run every day. I scheduled the .exe through Windows Schedular but my problem is everyday it runs & open up a new form & then the form stays there. How can I close the Form without any user as there is no one to click the 'X' button in the form.

Please suggest.

Regards,
0
kakali
Asked:
kakali
  • 4
  • 2
  • 2
  • +5
3 Solutions
 
ExcelGuideConsultantCommented:
You should add this line to your form_load

Me.WindowState = FormWindowState.Minimized
Me.Hide()
0
 
bklmnshCommented:
call this.Close() in your form's code after all tasks are completed.
0
 
kakaliAuthor Commented:
Sorry none of the above is closing the form autromatically after it is displayed. Only if I close the 'X' button then only it is closing.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
Reza RadCommented:
you must put this.Close(); or me.close() in your new form_load method. don't call it from other forms
0
 
ExcelGuideConsultantCommented:
yeah like I said: "You should add this line to your form_load" :-)

Good luck
0
 
kakaliAuthor Commented:
I am adding here

//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(986, 501);
this.Controls.Add(this.crystalReportViewer1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);

this.ResumeLayout(false);
this.Close();
but this one is giving the run time error:
Cannot access a disposed object.
Object name: 'Form1'.

I even tried in the Form_Load method to call close() method as the last statement. Have you ever tried it before ? I have already tried to call the Close() method at various places but none are working. Once the Form is displayed the control does not come back to the code it is left for the user to handle the form manually.
0
 
bklmnshCommented:
ok, first of all your application performs some operations w/out user interaction, so it would be better to use console application for this purpose.
if you still want to do this in winforms app, do the following:

add Load event handler to the form: this.Load += new System.EventHandler(this.Form1_Load);
in Form1_Load method perform all necessary operations and in the last line in this method call this.Close().
If this.Close() for some reason doesn't work, try Application.Exit or System.Threading.CurrentThread.Abort()
0
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
zadeveloperCommented:
What you really should do is create a windows service (which also will start up with windows on a power failure). Then export the report using only the reportobject (ie: without placing it on a form). I have just done the same thing , only using SSRS. Works great. If you are keen for more help on this approach .. please let me know.
0
 
AndyAinscowCommented:
>>I have a windows Form which display reports using Crystal Report Object & then export the reports in .pdf >>format & save it.

At the end of your code that does the above you need to add just one extra line

Application.Exit();
That should do what you want.
0
 
williamcampbellCommented:
As a last resort ///

You could write a little executive app that gets the Forms process. Then uses TerminateProcess to kill the app.

Snippet of code here

http://www.codeguru.com/forum/archive/index.php/t-70926.html

It could also detect when the report has been written and then terminate the Form.
0
 
kakaliAuthor Commented:
Hi All
Thanks for your help. In fact Application.Exit(),form.Close() nothing worked. I wanted to keep the form application but looks like not possible so I changed it to Console Application because no other choice.
0
 
kakaliAuthor Commented:
Not the solution which I was looking for. I changed it to Console Application instead Form Application.
0
 
AndyAinscowCommented:
From help files about Aplication.Exit()


Informs all  message pumps that they must terminate, and then closes all application windows  after the messages have been processed.


Odd that it didn't work !
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now