MainForm_Load doesn't work c# - 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?

Related

How do I add a Form Load event using the form designer in C# 2015? (like double clicking)?

I am new to C# in Visual Studio 2015. I just realized that the old classical way of adding event handlers by double-clicking on the item no longer works for some items like the form (or the Window).
After some Google searches I still can't figure out a way to add the Load event of the form using the designer.
Do I have to manually write code for that unlike in Visual Basic in Visual Studio 2005?
You can set the Load event of a Control from its properties window here. You create the
private void Form1_Load(object sender, EventArgs e)
{
// my code
}
event in your form class, and fill in its name (Form1_Load) where the arrow points in the picture.
Doing it manually would be something like:
Form1.Load += new System.EventHandler(this.Form1_Load);
considering that you created the Form1_Load event.
But on top of all that, double clicking should work.
You may have an issue with your instillation. However, select the form and go to the actions window and select load. This should give the desired result

C# Form Background Parameter

I have created a simple C# application to automatically login into a hotspot. I have a notify icon with a contextmenustrip with some functions like Connect, Disconnect, etc. I want to be able to run the form in the background showing only the notify icon to automatically login into the hotspot if the form is hidden.
I followed the instructions from VBNight in this post:
Hide form at launch
The application is running in the background, the notify icon showing but the Form_Load function is not working until I press on the notify icon.
I guess that's because Form_Load Occurs before a form is DISPLAYED for the first time.
Try moving your code from Form_Load to the constructor after InitializeComponent(); or so.
EDIT:
To answer your question, I suggest you extract code #1 from...
private void YOUR_BUTTON_Click(object sender, EventArgs e) {
// move this code #1 to...
}
and move the code to a brandnew method.
private void NewButtonClicked() {
// move code #1 here (in case)
}
Then, go back and call the method you just created.
private void YOUR_BUTTON_Click(object sender, EventArgs e) {
// You can leave code #1 but to remove duplicate,
NewButtonClicked();
}
Finally, replace YOUR_BUTTON.PerformClick(); with NewButtonClicked(); wherever you need. I assume you don't need any interaction with form Controls since the form is hidden.
I fixed it changing the form opacity to 0 and the ShowInTaskbar property to false. The form is hidden and the code is working.

Calling a click event containing a delegate stops the application from starting

I have a refresh button on my user control that works just fine every time I click on it. This is the code to its click event:
private void btnRefresh_Click(object sender, EventArgs e)
{
if (!RefreshFolder())
{
Notify(new string[] { "Refresh failed." });
}
else
{
Notify(new string[] { "Refreshed." });
}
}
Notify function happens to be a delegate call from my main form. This function works well when called normally. What I want to do is to raise the above event when my user control is loaded. But when I call btnRefresh.PerformClick() on the load event the application does not start and quits automatically after a short while. But when instead of the Notify function I put a MessageBox.Show("blah") the function works properly.
This is the delegate I use:
public delegate void NotifyMe(string[] messages);
And on my main form I have the following line in its constructor so that my user control can use the Notify function of the main control:
userctrlSelectManualCampaignFile.Notify += new NotifyMe(Notify);
The function works perfectly well as I said unless when I try to raise the click event. Any ideas?
Update:
Main form constructor:
public MainForm()
{
InitializeComponent();
logger = new Logger();
connectionHelper = new ConnectionHelper();
datetimeHelper = new DateTimeHelper();
userctrlSettingsGeneral.Notify += new NotifyMe(Notify);
userctrlSelectManualCampaignFile.Notify += new NotifyMe(Notify);
}
My user control constructor:
public ManualCampaignFiles()
{
InitializeComponent();
}
You're probably causing a "silent" exception to be thrown, causing your application to quit.
Such exceptions can be "swallowed" by the framework, for example within event handlers.
Try setting your debug environment to break on thrown exceptions. For this, go to the Debug > Exceptions... menu, then in the dialog that shows up, make sure to check the "Thrown" checkbox corresponding to Common Language Runtime Exceptions.
This way, when you lauch your program, it will break at the moment an exception is thrown, allowing you to figure out what is going on.
Cheers
I found the answer in another forum:
Before event raising you should check if event is not null. This is
common pattern for event raising.
Answer provided by Vitaliy Liptchinsky.
But Hans Passant's answer was also very close to truth.

Take screenshot of WinForm after it has loaded

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?

WindowLoaded() not getting called

I have a breakpoint in a form's OnLoaded event (which dynamically creates some controls), yet when I instantiate the form, it simply sits there looking blanched, and Window_Loaded() is never called.
I instantiate the form with a custom constructor:
NoUseForAName nufan = new NoUseForAName(iListMsgTypes, dtFrom, dtTo);
nufan.Show();
And have added the Loaded() event, which I expect to get called directly after I call .Show() on the form:
private void Window_Loaded(object sender, RoutedEventArgs e)
Why is [Window_]Loaded() not getting reached?
You should either add
InitializeComponent();
as the first line of your custom constructor, or call the default constructor like this:
public NoUseForAName(...)
: this()
{
...
}
Did you hook up the event anywhere? e.g. on the new instance
nufan.Loaded += Window_Loaded;
(Also loaded is not called "directly" after show, loading may take a while)

Categories

Resources