I have a problem with C# Winforms (.NET framework) user control.
- I have a 'notifications' user control what contains a button, and some information.
- and I have a 'todaylist' form, what dynamically generate UC with database.
I don't know how to create an event for the button. When I click one button, i want to get one information (label) from the UC.
This is the user control (I want to get 'label2' text)
Generated user controls:
I tried this way but nothing:
EDITED:
User control:
public event EventHandler<ActionTaskEventArgs> ActionTaskClicked;
private void button1_Click(object sender, EventArgs e)
{
string cardid = label2.Text;
var args = new ActionTaskEventArgs(cardid);
ActionTaskClicked?.Invoke(this, args);
}
public class ActionTaskEventArgs : EventArgs
{
public ActionTaskEventArgs(string taskId)
{
TaskId = taskId;
}
public string TaskId { get; }
}
Form:
//UC generating:
if (statusz[i] == "Kiadott")
{
listItems[i] = new notifications();
listItems[i].Title = title[i];
listItems[i].Details = details[i];
listItems[i].Id = id[i].ToString();
listItems[i].Finish = "Ma";
listItems[i].BtnTxt = "Kezdés";
listItems[i].BtnVisible = true;
listItems[i].color = Color.FromArgb(254, 95, 85);
flowLayoutPanel1.Controls.Add(listItems[i]);
listItems[i].ActionTaskClicked += this.ActionTask_Clicked;
}
//
private void ActionTask_Clicked(object sender, notifications.ActionTaskEventArgs e)
{
MessageBox.Show(e.TaskId);
}
Can somebody help me please?
Now I get an error for this:
void UC_Click(object sender, EventArgs e)
{
//I gave an error for the below row! Can't convert button type to notifications type i guess.
notifications obj = (notifications)sender;
MessageBox.Show(obj.Id);
}