question

Filip-2434 avatar image
0 Votes"
Filip-2434 asked Filip-2434 commented

Powershell windows form how to run Form1_Load void after loading Form?

Hello everybody.

This is my powershell code of windows form application in C#.

 Add-Type -TypeDefinition @"
 using System;
 using System.ComponentModel;
 using System.Drawing;
 using System.Windows.Forms;
 using System.Timers;
    
 namespace Test
 {
     public class Form1 : Form
     {
         public Button button1;
     System.Timers.Timer t;
         public Form1()
         {
             button1 = new Button();
             button1.Size = new Size(40, 40);
             button1.Location = new Point(30, 30);
             button1.Text = "Click me";
             this.Controls.Add(button1);
             button1.Click += new EventHandler(button1_Click);
                
         }
        
         private void Form1_Load(object sender, EventArgs e)
         {
             t = new System.Timers.Timer();
             t.Interval = 10;
             t.Elapsed += OnTimeEvent;
             t.Start();
         }
    
    
         private void button1_Click(object sender, EventArgs e)
         {
             MessageBox.Show("Hello World");
         }
    
       private void OnTimeEvent(object sender, ElapsedEventArgs e)
         {
             Invoke(new Action(() =>
             {
         Console.WriteLine("Hello world");
         }));
     }
    
    
         [STAThread]
         public static void Main()
         {
             Application.EnableVisualStyles();
             Application.Run(new Form1());
         }
     }
 }
 "@ -ReferencedAssemblies System.Windows.Forms, System.Drawing
    
    
 iex "[Test.Form1]::Main()"

The proble is that the Form1_Load(object sender, EventArgs e) void, not run after loading the application.
But when i run it in Visual Studio it work fine.

I did one sulotion that I change it and run like normal void in Form1:


         public Form1()
         {
          Form1_Load();        
         }
        
         private void Form1_Load()
         {
             t = new System.Timers.Timer();
             t.Interval = 10;
             t.Elapsed += OnTimeEvent;
             t.Start();
         }


And it work's.
But I don't understand why it don't run like Form1_Load(object sender, EventArgs e)?
Is there reason or some sulotion to run it like Form1_Load(object sender, EventArgs e)?

Thanks for answare.





windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

OlafHelper-2800 avatar image
1 Vote"
OlafHelper-2800 answered Filip-2434 commented

button1.Click += new EventHandler(button1_Click);

You define the event for the button click, but not for Form load. Add

 this.Load += new System.EventHandler(this.Form1_Load);
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @OlafHelper-2800
Thanks so much, this work.

0 Votes 0 ·