Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a form load event (currently not working)

Tags:

I have a Windows Forms form where I am trying to show a user control when the form loads. Unfortunately, it is not showing anything. What am I doing wrong? Please see the code below:

AdministrationView wel = new AdministrationView(); public ProgramViwer() {     InitializeComponent(); }   private void ProgramViwer_Load(object sender, System.EventArgs e) {     formPanel.Controls.Clear();     formPanel.Controls.Add(wel); } 

Please note I added the load event based on what I read in this article:

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.load.aspx

like image 797
user710502 Avatar asked Mar 23 '12 22:03

user710502


People also ask

Why the form1_load () event is used?

It "Occurs before a form is displayed for the first time." and "You can use this event to perform tasks such as allocating resources used by the form." You can consider it as "initialization". For example, if you want to determine whether a label “__ , Sir” should use "Morning, Afternoon" .

How do you call a load event in C#?

You have to call Form_load. Form_Load(this, null);


2 Answers

Three ways you can do this - from the form designer, select the form, and where you normally see the list of properties, just above it there should be a little lightning symbol - this shows you all the events of the form. Find the form load event in the list, and you should be able to pick ProgramViwer_Load from the dropdown.

A second way to do it is programmatically - somewhere (constructor maybe) you'd need to add it, something like: ProgramViwer.Load += new EventHandler(ProgramViwer_Load);

A third way using the designer (probably the quickest) - when you create a new form, double click on the middle of it on it in design mode. It'll create a Form load event for you, hook it in, and take you to the event handler code. Then you can just add your two lines and you're good to go!

like image 177
Bridge Avatar answered Sep 28 '22 08:09

Bridge


You got half of the answer! Now that you created the event handler, you need to hook it to the form so that it actually gets called when the form is loading. You can achieve that by doing the following:

 public class ProgramViwer : Form{   public ProgramViwer()   {        InitializeComponent();        Load += new EventHandler(ProgramViwer_Load);   }   private void ProgramViwer_Load(object sender, System.EventArgs e)   {        formPanel.Controls.Clear();        formPanel.Controls.Add(wel);   } } 
like image 23
GETah Avatar answered Sep 28 '22 08:09

GETah