Take screenshot of WinForm after it has loaded - c#

I have a screenshot application where a user can pass command line arguments, and based on those arguments will determine the behaviour of the form.
I am trying to take a screenshot after the form has loaded, however I have the functionality to do this after InitializeComponent();. Like so-
if (counts > 0)
{
generateScreenshotButton_Click(null, null);
button2_Click(null, null);
}
My problem being these functions are firing before the form has loaded. Therefore the screen shot is blank. How can I resolve this?

Add Load event in your Form.cs file.
There is a difference between InitializeComponent and Form Load. See What does InitializeComponent() do, and how does it work in WPF? It is for WPF but is same in Windows Forms.
Try this code:
public Form1()
{
InitializeComponent(); //this is the InitializeComponent method. this This method locates a URI to the XAML for the Window/UserControl that is loading, and passes it to the System.Windows.Application.LoadComponent() static method.
this.Load += new EventHandler(Form1_Load); //this create Load Form event
}
void Form1_Load(object sender, EventArgs e) //after your form is completely loaded, your program will run the code from here...
{
//your code goes here
}
Hope that I helped you.

Have you tried using MainForm Loaded event?
And in that event handler do your stuff? :)
Or maybe using Shown event?

Related

How to call a function inside a form from a user control (C# Winforms)

So I have user control I want to hide/send to back and I want to call the public function in my form where the user control is, from the control itself.
I have a button in the user control with the following code:
mainboard MAIN = new mainboard(); // mainboard is a form to call to.
MAIN.PastLockScreen(); // PastLockScreen is a public void inside mainboard
When I click the button the public function in mainboard does not get called. There are no errors, what am I doing wrong, and how can I call a function in a form from a user control?
void inside mainboard
public void PastLockScreen()
{
lockscreen1.SendToBack(); // lockscreen1 is the usercontrol that this function gets called from
}
The void is being referenced but not called?
Edit: I have done some investigating and turns out that my timers I have in any form or control, also dont work. But buttons on the actual form itself do work. (and yes I did do timerName.Start(); when the form/control loads.)
Solved the issue above, my timers needed to display time, which the time string I defined inside the class instead of inside the timer.tick
From within the UserControl, just cast ParentForm to type mainboard:
// .. form within the UserControl that is CONTAINED by form mainboard ...
private void button1_Click(object sender, EventArgs e)
{
mainboard MAIN = this.ParentForm as mainboard;
if (MAIN != null)
{
MAIN.PastLockScreen();
}
}
Note that this is a TIGHTLY COUPLED approach that limits your use of the UserControl to only mainboard.
A better approach would be to make the UserControl raise some kind of custom EVENT that the mainboard form then subscribes to. When the event is received then the form itself would run the appropriate method. This would mean you could potentially utilize the UserControl in a different form without changing any code within it. This latter approach would be LOOSELY COUPLED.
Try This
In Form1
to Show the Form2
Form2 frm2 = new Form2();
frm2.Show();
To call the method do you want
Form2 cmd = new Form2();
cmd.PastLockScreen();
In Form2
public void PastLockScreen()
{
this.SendToBack();
}

Detect CloseEvent through different forms

PROBLEM SOLVED
SHORT STORY
I want to detect "FormClosing()" event through different forms, ie, when form1 is closed that is instantiated within form2, can form2 detect when user presses exit in form1?
LONG STORY
My team and I are working on a windows form application. Project has two forms: one is the main form page and the other is accessed via this main form. Main form looks like this:
And the second one looks like this:
If you press "Ekle/Sil" buttons within the main form, you are directed to form 2 where you can edit database entries. When you press "Sayfayı Yenile" button in the main form, the content of the text areas are refreshed by re-fetching entries from the database.
My problem is, I want to automatically refresh the main form when the user closes the second form. My research suggests I should use an "FormClosing()" event to detect a closing form. However, I want to detect this from the main form. Instantiating main form in second form's source code doesn't seem to be a reliable solution. Anyone can tell me how to do this?
EDIT
I solved the problem:
1) Created a public method within the main form that refreshes the page.
2) Send "this" property from the main form when creating the second form.
3) Added an "FormClosed()" handler within the second form that invokes this public method.
Still, I'm looking for a better solution.
EDIT 2
Better solution InBetween's answer
Simply use the Form.Closed event of the new child windows form. Everything is handled from the main form:
void EkleSil_Clicked(object sender, EventArgs e) //or whatever method is called when button is clicked
{
var newChildForm = new ChildForm();
newChildForm.Closed += childFormClosed;
newChildForm.Show();
}
void childFormClosed(object sender, EventArgs e)
{
((Form)sender).Closed -=childFormClosed;
updateShownData();
}
You can create a event in the second form and raise it when the form is closing .
Handle the event in the main form and refresh the main form when the event is raised
Another option would be to pass Form1 as an argument to Form2. Then use the Form.Closing event in Form2 and use the Form1 reference to trigger something.
Form1 form1Ref;
public Form2(Form1 mainform)
{
form1Ref = mainform;
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
form1Ref.SomeMethod();
}

Creating a load event for text label in windows application c#

I am new to C# programming language. I am trying to create a windows form application in c# .net using VS 2012. I have created a text label and I want to display a text when the application is loaded. I see only click event rather than load event. would anyone please suggest what to do? (I am expecting something like "lblText_Load")
The label does not have an event for loaded. You can either use the Form's Load event, or the OnShown event.
I suggest using the OnShown event because this will be triggered after the form is loaded and displayed. The OnLoad event for the form occurs when it begins to load not after.
You can use the form's Load event. See MSDN for additional information.
private void Form1_Load(object sender, System.EventArgs e)
{
lblText.Text = "w00t \o/";
}
On the load event of the form you can do what you want.
private void Form1_Load(object sender, System.EventArgs e) {
lblText.Text = "Evaluation Tool";
}
Forms have lots of events you can use (Load, Activated, Shown , etc.)

MainForm_Load doesn't work c#

The MainForm_Load doesn't work, the form is showing as I designed, but nothing in the load method happens. When I put a breakpoint it just skip on this method, I tried to delete the method and recreate it but it still not working.
I'm using Visual Studio 2010, everithing works fine with other projects I did.
Even the most basic function not working in it.
Here is an example:
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
MessageBox.Show("Test");
}
Any ideas??
Tnx
Did you type this by hand?
You need to assign the event, if you do this on the designer it will auto generate the code. If you want to do by hand you have to manually assign the event.
public FormMain()
{
InitializeComponent();
this.Load += FormMain_Load;
}
Everything looks fine here.Please check this.
Go to Properties window of your form(here it is FormMain.cs[Design] ). Click on event section. check in Load event your
FormMain_Load
method is defined or not ?
give the breakpoint and check it is calling or not.
Give breakpoint on the form constructor .
And you need to check, From where you are calling this form ?
if this form is the first form in your application, then go to Program.cs file. and check there this is available or not inside Main Function.
Application.Run(new FormMain());
In FormMain.Designer.cs page check
this.Load += new System.EventHandler(this.FormMain_Load);
is available inside
private void InitializeComponent()
{
}
or not?

Registering a KeyPress Event Method in C# "DialPad.Designer.CS" page

Im designing a dialerPad form using Windows form, there is a textbox which should only take numbers and not text, my problem is that when i add the code
private void txtDialedNumber_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
//Blaah Blaah Code;
}
but its not getting registered in the other DialPad.Designer.CS page. For example the fallowing code registers TextChangedEvent
this.txtDailedNumber.TextChanged += new System.EventHandler(this.txtDailedNumber_TextChanged);
Can anybody help me on this?
You should never change *.designer.cs files manually.
What you should be doing is opening the design view of your form, selecting the object, and then setting the event handler in the objects properties:
Alternatively, if you want to register event handler manually (instead of using the designer), just put it under the InitializeComponent() call in the constructor for your form.
I would try to simplify Greg's answer.
Select the text box > go to properties > events tab> KeyPress Event > select the method this.txtDailedNumber.KeyPress
You are done.

Categories

Resources