Witam.Jak napisac program który definiuje zdarzenie programisty, w przypadku gdy wynik mnożenia dwóch liczb typu int wynosi 40. Te dwie liczby wprowadzać z klawiatury przez textBox’y. Zdarzenie powinno zostać obsłużone przez metodę wyprowadzającą komunikat „Wynikiem mnożenia jest 40.”.Z góry dzięki.
0
1
private void button1_Click(object sender, EventArgs e)
{
int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox2.Text);
int c = a * b;
if (c == 40)
MessageBox.Show("Wynik blablabal 40");
textBox3.Text = c.ToString();
}
?
1
Mi się wydaje, że chodziło raczej o stworzenie własnego zdarzenia.
public partial class Form1 : Form
{
public delegate void EventHandler(object sender, EventArgs e);
public event EventHandler Event;
private void OnEvent(EventArgs e)
{
if (Event != null)
Event(this, e);
}
public Form1()
{
InitializeComponent();
textBox1.TextChanged += new System.EventHandler(textBox_TextChanged);
textBox2.TextChanged += new System.EventHandler(textBox_TextChanged);
}
void textBox_TextChanged(object sender, EventArgs e)
{
try
{
int value1 = int.Parse(textBox1.Text);
int value2 = int.Parse(textBox2.Text);
if (value1 * value2 == 40) OnEvent(new EventArgs());
}
catch(Exception) { }
}
}
oraz
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Form1 form = new Form1();
form.Event += new Form1.EventHandler(form_Event);
Application.Run(form);
}
static void form_Event(object sender, EventArgs e)
{
MessageBox.Show("wynik mnożenia to 40");
}