Question Subscribe and Catch child control click event

WiiLF

New member
Joined
Nov 7, 2021
Messages
3
Location
Canada
Programming Experience
10+
Hey Community! Happy to be here.

I have a form that contains a custom list control. In this list control, is a flexlayout control called flpListBox with rows of results.

I am having problems catching the click event of each item. I can MessageBox the sender object key (the song name in this case), and that is correct per row clicked, but I want to show the clicked result song name on the parent main form label lblNowPlaying.Text

In ListControl, I have this

C#:
public void Add(string Song, string Artist, string Album)
{
    var c = new ListControlItem();
    c.Name = "item" + (flpListBox.Controls.Count + 1);
    c.Margin = new Padding(0);
    c.Song = Song;
    c.Artist = Artist;
    c.Album = Album;
    c.SelectionChanged += SelectionChanged;
    c.Click += ItemClicked;
    flpListBox.Controls.Add(c);
    SetupAnchors();
}

public event ItemClickEventHandler ItemClick;
public delegate void ItemClickEventHandler(object sender, int Index);

private void ItemClicked(object sender, EventArgs e)
{
   ListControlItem c = (ListControlItem)sender;
   //MessageBox.Show(c.Song);

   ItemClick?.Invoke(this, flpListBox.Controls.IndexOfKey(c.Song));
   //ItemClick?.Invoke(this, flpListBox.Controls.IndexOfKey((sender as ListControlItem).Song));
}

And in parent form Form1 I have

C#:
InitializeComponent();
ListControl1.ItemClick += new ListControl.ItemClickEventHandler(ListControl1_ItemClick);
C#:
private void ListControl1_ItemClick(object sender, int Index)
{
   MessageBox.Show("event caught");
   lblNowPlaying.Text = "Now Playing: item index " + Index;
}

The reference for ListControl1.ItemClick correctly refers back, so I don't understand why I can not catch the Song name from the child control, and send to parent form label.

snap.jpg


Maybe someone can point me in the right direction, much appreciated
 
Just like in your previous line #20, why not get the list control item once more so that you have access to the other data?
 
I seem to have the string over I wanted written to the label on the main form, It would be really nice to get all of those fields (image especially) over to picturebox on main form, the string is fine but its limited

Looks like I can do something like this

C#:
public delegate void ItemClickEventHandler(object sender, string Name, Image Cover);

ItemClick?.Invoke(this, c.Song, c.Image);
 
Last edited:

Latest posts

Back
Top Bottom