Printform in c# 2008 - c#

How to use the powerpack PrintForm? i want to print the winform.

First use the name space
using Microsoft.VisualBasic.PowerPacks.Printing;
Suppose you have two forms say Form1 and Form2. You have a print button in Form1 and on click the button you need to print Form2. Foe this do as
Form 1
private void printButton_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
Form 2
private void Form2_Load(object sender, EventArgs e)
{
this.Hide();
printForm1.Print(this, PrintForm.PrintOption.ClientAreaOnly);
this.Close();
}

Related

How can I restore a form?

I'm working on a Form application which is written in C#. How can I hide a form and then restore it?
Form 1:
private void PlayButton_Click(object sender, EventArgs e)
{
this.Hide();
}
Form 2:
private void BackButton_Click(object sender, EventArgs e)
{
this.Close();
//code which shows again form1
}
You will need to have a reference to the first form (Form1 in your example) so that you can call the Show() method against it.
A simple approach might be to have a property on Form2 that you assign with a reference to the first form.

Close existing form on opening another form

I am working in compact framework 3.5,i am working on windows mobile application.
form1.cs
internal void menuItem_Click(object sender, EventArgs e)
{
this.Close();
form2 obj = new form2();
obj.ShowDialog();
/////other code
}
form2.cs
public void backbutton_Click(object sender, EventArgs e)
{
form1 obj = new form1();
obj.ShowDialog();
/////other code
}
Initially i am in form1 and i am navigating to form2.In form2,a back button is there and on clicking backbutton i need to show the from1.Now i can show the form1 on backbutton click,but i need to close the previous opened form1.
I tried with this.Close() and this.Dispose(). This makes me exist from my application.
How to do this in compact framework.
Best way to work with that kind of things is using a singleton. When you show your first form, you instance the first form, then you show the second creating it instance and hiding the first one (use hide(), not close(), then your instance will still on memory). On back button click, you only have to hide() the second, and show() the first.
I would recommend instead of creating new form1 from form2, just close form2 and go back to form1:
form2.cs:
public void backbutton_Click(object sender, EventArgs e)
{
this.Close();
}
form1.cs:
internal void menuItem_Click(object sender, EventArgs e)
{
form2 obj = new form2();
obj.ShowDialog();
/////other code
}

Close Main Form

I am developing a simple app using c# Windows forms. The main form open another form, but i dont want the both forms. I want it that when the second form opens the first form closes. Since the first form is the main using
this.Close();
after showing the second form will close the both. So i used this instead
private void btnSubmit_Click(object sender, EventArgs e)
{
frmData QS = new frmData();
QS.Show();
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}
private void frmData_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
I want to know if there is any other way to do this.
Any help will be appreciated.
Do not pass your main form as argument to Application.Run:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm frmMain = new MainForm();
frmMain.Show();
Application.Run();
Thus you will be able to close it when showing another form:
private void btnSubmit_Click(object sender, EventArgs e)
{
frmData QS = new frmData();
QS.Show();
this.Close();
}
To close application you should use
Application.Exit();
Hide the first form.
private void btnSubmit_Click(object sender, EventArgs e)
{
frmData QS = new frmData();
QS.Show();
this.Hide();
}
private void frmData_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
Actually there's no possible way to close the main form if you close the main form it closes all forms of the same project. You can only hide it.
BTW you can open and show a form in only one simple line like this: new FormName().Show();

Finding out parent form

I've got three forms, Form1, Form2 ,Form3.
A button in Form1 can open Form3 and a button in Form2 can also open Form3.
When either button is pressed the respective form is hidden and Form3 is opened.
When Form3 is closed it should open the form that has been hidden.
How would I go about doing this?
Form.Show method can take OwnerForm as argument so call it like that:
var frm = new Form3();
frm.Show(this);
you can access parent in Form3 by property Owner so in closing event:
private void FormIsClosing(object sender, FormClosingEventArgs e)
{
var owner = this.Owner;
if (owner != null)
{
owner.Show();
}
}
Another way is to use Acivated and FormClosed events.
Let's say in Form1 you Click a Button to show Form2
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Activated += new EventHandler(frm2_Activated);
frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
frm2.Show();
}
Now, this one is when the Form2 showed or is Activated you hide the calling form, in this case the Form1
private void frm2_Activated(object sender, EventArgs e)
{
this.Hide(); // Hides Form1 but it is till in Memory
}
This one when the Called form is Closed in this case Form2 it will then show the hidden Form1 again.
private void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
this.Show(); // Show hidden Form1
}

Close two form by one click?

I'm working on project with sign in feature
When I run the project there is a form (form1) run the sign in .
after i click on login button build another form (form2) - It's the form of my program .
and made the first form (form1) hide .
The problem is when I press at the X button in form2 it's close but the form1 it's still running .
I tried to close the form1 instead of hide ... but this will close form2 before launching
In form1:
this.Hide();
Form2 x = new Form2();
x.Show();
I think you have your forms around the wrong way.
Form1 sould be your app and shold show Form2 as a dialog when it first loads, then when it closes you can process the result and decide wether to continue or close the application.
Something like:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
Form2 myDialog = new Form2();
if (myDialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
{
// failed login
// exit application
}
// all good, continue
}
}
You could subscribe to the child forms FormClosed event and use that to call Close on the parent form.
x.FormClosed += new FormClosedEventHandler(x_FormClosed);
void x_FormClosed(object sender, FormClosedEventArgs e)
{
this.Close();
}
try this, in the log in button if access is granted
private void logInBtn_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.ShowDialog();
this.Hide();
}
then in form2 if you want to exit
private void exitBtn_Click(object sender, EventArgs e)
{
Application.Exit();
}
hope this helps.
You go to your form2 then in the event of the form look for FormClosed.
Put this code in your eventhandler:
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
FormClosed is the event whenever the user closes the form. So when you close the form put a code that will exit your application that is -applicationn.exit();-
Hope this will work.

Categories

Resources