PrintForm Query in C# - c#

I have a print button in form 1, when i click that, I should be able to print another form say form 2(This form has the basic layout of the page to be printed). Can anyone please tell me as to how to make this reference to form 2 in the form 1 print button event handler? Is there any need to use printform component in both the forms?
When I tried to do this, It was throwing an exception.

so put the button in Form1..
in the button click handler:
using (Form1 dialog = new Form1) {
// Do whatever you need to do here to print.
}

Related

How to prevent clicks outside the current form?

I want the form which will open when the user click a button, I don't want the user to click anywhere else outside this form(preventing him from clicking any other buttons on the parent form), I want to restrict his actions inside this form until he close it (For sure this form is top most), and I want the form to make alert(flicker) to any clicks outside it so the user will understand that he have to close it first.
When opening the form instead of
Form form = new Form();
form.Show();
use
Form form = new Form();
form.ShowDialog()
Super easy fix!
I assume your code to show the form right now is
form.Show();
To create/show an instance of the form that is modal (restricts clicking to that form) all you need to change is that line to:
form.ShowDialog();
Liam

should not click on the previously form [C#]

In my form i have button to show other form.
But i want the previously form cannot be clicked before the new form is closed , How to create that ?
Because if the previously form is clicked, and i click the button again, the form is show multiple.
this my code in button click :
MDACS_AOP_CFSTL_InputActivity addProblem = new MDACS_AOP_CFSTL_InputActivity(ParameterSesi, ParameterNamaKaryawan, ParameterTanggal);
//addProblem.Close();
addProblem.Show();
You should use
addProblem.ShowDialog(this);
This will open the dialog as child of the parent (this) dialog. You cant click the parent dialog but you can still see it.
You need to use
addProblem.ShowDialog();
instead of
addProblem.Show();
so that it will open a modal dialog.
You could use
MDACS_AOP_CFSTL_InputActivity addProblem = new MDACS_AOP_CFSTL_InputActivity(ParameterSesi, ParameterNamaKaryawan, ParameterTanggal);
this.Hide;
addProblem.ShowDialog();
this.Show();
The show(); command will not be executed until the dialog is close. So it will stay hidden

Load same instance in C#.net

I am facing a problem in my application. There are multiple forms which can be accessed by quick setup button in the home page,which contains next and back buttons.
And in the last form there is a finish button, which when clicked goes to another form named Scoreboard. The scoreboard form is having a user control which have home button, setting etc.
When I click on home button it goes back to home page and then when the user clicks the quick setup again, I needs to retain the previous values in the form instead of creating new instance.
In the finish button i am using this code :
Scoreboard sc = new Scoreboard();
sc.show();
this.hide();
Any suggestions ?
There are a couple of ways that you can go. The first is since you are loading your Scoreboard form from the same button you can just subscribe to the Scoreboard Forms FormClosing Event and use public properties to get the information back into your parent form, you could then pass that information back to the ScoreBoard when you create the Form. Your other option would be to use UserSettings to persist your values in between sessions.

get focus on first form

I have two forms form1 and form2. I navigate to form2 using a button in form1. In form2 I have a button control; on button click, I show messageBox. when messageBox comes then it also lost focus of form1 but I want that it should not lost focus of form1. I have no concern with form2.
There's no way a message box doesn't show like a dialog. Like Eliran Pe'er said, you should make a Form like a messagebox with a label and a button and use it like this.
MessageForm form = new MessageForm.Show();
If you use ShowDialog it's going to be the same thing as MessageBox.
In your form 1 you can use TopMost property = true in order to keep it in front all the time no matter what. But this is going to keep your form on top of all other open programs.
Another workaround would be after messagebox is closed by the user (this is not a bad option) you can call form 1 to BringToFront(). To do this, you can pass the instance of form1 to the form2 in the Show method. Use that parameter in your form2 constructor.
I don't think there's an easy way to prevent MessageBox from taking focus, and thats because a MessageBox is a dialog. (dialogs take focus from the program until they being closed)
The only way I can think of is creating new form that looks like a MessageBox, and using it instead.
try this
if(MessageBox.Show("something")==DialogResult.OK)
{
form1.Focus();
}
Or
if(MessageBox.Show("something")==DialogResult.OK)
{
form1.Select();
}
Are you using ShowDialog() method or Show() method to show your form2?
If you are using ShowDialog() method, modify it as Show().
Because ShowDialog() method will not allow you to change the focus to main form (form1), until you close the sub form (form2)
Make sure you are using method,
form2.Show()
to Display form2.

How can I access the controls from another running window form in c#

Actually, I am a newbie for C#. In my program, I got two window forms.Each window forms have one label and one activate button.Once I start(load) the program,two window forms will pop-up together.My intention is when I click on the form1 activate button,my form1 label's back colour will change to "green"(to let user know this form1 is activated).Up to here I can write the soft code.But when I click on the form2 activate button,form1 will no longer active, my form2 will be activated and form2 label's back colour will also change to "green".Since form1 is no longer active,I would like to change my form1 label's back colour to red.How can I change it?
How I write my program so far.
In form1 activate button click
1.form1 label's back colour = Colour.green // to let user know form1 is activate upon button click
2. form2 f2 = new form2();
f2.(form2 label's).BackColour = Colour.red
There is no change on form2.
when I add f2.Show() , once I click the activate button on form1 , form2 will pop-up with red label back colour.
Please remember that I am running both form1 and form2 on my Mainform.
Thanks In Advance.
Your forms are just regular c# classes, and the form controls are private members of these classes.
If you need to have public access to few of those controls, just create a public property for those controls.
public Button MyColorButton
{
get { return MyButton; }
}

Categories