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.