0

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)

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

Generated user controls:

Generated UCs

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);
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • [How to know in a Form the name of a Button clicked in a UserControl](https://stackoverflow.com/a/67256385/7444103) – Jimi Jun 01 '21 at 22:08
  • And how to subscribe the button click in the main form, if i generating dynamical? – lakatvagyok Jun 02 '21 at 08:44
  • I don't know what i am doing wrong, but not really work. I make the same, what in the link and what you writed. But when i click the button, doesn't do anything. – lakatvagyok Jun 02 '21 at 09:41
  • I updated the question with the new code. – lakatvagyok Jun 02 '21 at 10:02
  • I edited the question with the generating code, and i move subscription there. I guess i need to add the "ActionTask_Clicked" method, when i generate a new UC. But still not working. – lakatvagyok Jun 02 '21 at 10:22
  • I don't know what `listItems` is or what conditions you're testing there. Do the usual thing: build a new Project that just has a Form that contains a Button and your UC. When the Button in the Form is clicked, add a new UC to the Form and subscribe to the event. The UC appears. Click the Button of your UC... Does it work now? Of course it does, this is very standard stuff. -- Name the Button in your UC in a meaningful way (e.g., `ActionButton`) and make sure you have subscribed to its `Click` Event. Set a breakpoint in the `Click` handler, both in the UC and the Form. – Jimi Jun 02 '21 at 10:37
  • I following this video: youtube.com/watch?v=u71RJZm7Gdc&t=0s But i make it with Database. There is the answer what is "listItems". Can you please watch the code? I put the whole in the question – lakatvagyok Jun 02 '21 at 12:43
  • Can be possible, if i have a login panel what contains a try-catch (if thy mysql server not runnning, show a message.) And when i click the button, i get the login panel error message? – lakatvagyok Jun 02 '21 at 13:25
  • 1
    I don't know what did i do wrong but it works now. Thank you very much! – lakatvagyok Jun 02 '21 at 14:11

1 Answers1

0

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;
György Kőszeg
  • 17,093
  • 6
  • 37
  • 65
  • I'm not sure i understand this, but i see you Hungarian too. Can you help me somewhere? – lakatvagyok Jun 01 '21 at 16:02
  • I updated my answer with further missing parts – György Kőszeg Jun 01 '21 at 16:11
  • It's dropped me this error: 'System.InvalidCastException: 'Az objektum nem konvertálható „System.Windows.Forms.Panel” típusról „Retask.forms.notifications” típusra.'' Where should i put your second code? – lakatvagyok Jun 01 '21 at 16:23
  • Of course I don't see from your code how deep your controls are embedded into each other. But I hope the intention was clear. A more elegant solution would be to create a `GetChild(int index)` method on your `notifications` control that returns the `UC` at the specified index. – György Kőszeg Jun 01 '21 at 16:35