It is necessary to create a new event for the object created by the program. Click event. How to do it? Where to begin?
- oneWhat kind of object? You have written a class and want to write an event for it? or what? - Specter
- oneI programmatically created Button and I need to write an event for it. - Svyatoslav
|
1 answer
first version (traditional):
YourButton.Click += new System.EventHandler(YourButtonClick);//подписываемся на событие //обрабатываем событие private void YourButtonClick(object sender, EventArgs e) { MessageBox.Show("He's Alive"); } second option (lambda syntax):
YourButton.Click += (s,e)=>MessageBox.Show("He's Alive"); - 2Thank. I like the first one better. - Svyatoslav
- 3Jeffrey Richter says that if a method takes more than 4 logical lines of code, it must be named, but for "small" handlers and anonymous methods are suitable - Specter
- And I like the second option more - it is more universal. The first option is suitable for developing Desktop applications, I also write Windows RT applications (under Windows 8) and it works differently there. - Egor Lyah
- 2Are you sure that you can use lambdas under WinRT-target, but not methods? (Hint: this can not be, since lambda is compiled into a method (possibly of an auxiliary class).) - VladD
- Well, here is my version of the working code:
Button enterData = new Button(); enterData.HorizontalAlignment = HorizontalAlignment.Center; enterData.Click += async (s, e1) => { if (service.Text != "" && login.Text != "" && password.Text != "") { OutputData.Text += service.Text + " " + site.Text + " " + login.Text + " " + password.Text + "\n"; popup.IsOpen = false; } else { MessageDialog dlg = new MessageDialog("Повторите ввод данных, заполнив все поля отмеченные звездочной.", "Ошибка!"); await dlg.ShowAsync(); } };Button enterData = new Button(); enterData.HorizontalAlignment = HorizontalAlignment.Center; enterData.Click += async (s, e1) => { if (service.Text != "" && login.Text != "" && password.Text != "") { OutputData.Text += service.Text + " " + site.Text + " " + login.Text + " " + password.Text + "\n"; popup.IsOpen = false; } else { MessageDialog dlg = new MessageDialog("Повторите ввод данных, заполнив все поля отмеченные звездочной.", "Ошибка!"); await dlg.ShowAsync(); } };- Egor Lyah
|