简体   繁体   中英

c# winform .net create button click event for user control

I have a problem with c# winform (.net framework) user control.

  • I have a 'notifications' user control what contains a button, and some information.
  • and i have a 'todaylist' form, what dinamycal 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.

Pics:

The UC (I want to get 'label2' text)

UC(我想得到'label2'文本)

Generated UCs

生成的 UC

I tried this way but nothing:

EDITED:

UC

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);
        }

You can "forward" the event of the user control to the button by defining custom add/remove accessors:

// UC:
public event EventHandler ButtonClick
{
    add => btn.Click += value;
    remove => btn.Click -= value;
}

And in your main form you subscribe notifications.Click (which seems to be the generated user control of multiple children). But actually you should subscribe the ButtonClick of a child control:

((UC)asd.Controls[0]).ButtonClick += Child0Click;

You do not need

public event EventHandler btnClick;

Try removing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM