-1

First, a great thank you to those who asked/responded to questions. You were able to get me this far. I wanted to help a young Belgian entrepreneur by taking on a challenge, build a Media managing software to display various media types (Images, Videos, links, text) on huge LED screens. I have limited coding experience as I work in EDI. My issue is that I create playlists dynamically based on the number of playlists in the DB (see screenshot), but I cannot trigger the playing of the right playlist when pressing the play button. Warning, my code is noob code. PlayList ScreenShot

 Label playListLbl = new Label();
 GroupBox playListGrp = new GroupBox();

 public GroupBox addplayListGrp(int i, int start, int end)
    {

        GroupBox playListGrp = new GroupBox();
        playListGrp.Name = "playListGrp"+ Convert.ToString(1 + i);
        playListGrp.Text = "Play list " + Convert.ToString(1 + i);
        playListGrp.Font = new Font("Century Gothic", 12F, 
        FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
        playListGrp.Width = 425;
        playListGrp.Height = 525;
        playListGrp.Margin = new Padding(1);
        playListGrp.Location = new Point(start, end);
        return playListGrp;
        }
   Button addPlayBtn(int i)
        {
        Button PlayBtn = new Button();
        PlayBtn.Font = new Font("Century Gothic", 9.75F, 
        System.Drawing.FontStyle.Regular, 
        System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        PlayBtn.ForeColor = Color.Black;
        PlayBtn.Location = new Point(10, 467);
        PlayBtn.Name = "playBtn" + Convert.ToString(1 + i);
        PlayBtn.Size = new Size(100, 30);
        PlayBtn.TabIndex = 6;
        PlayBtn.Text = "Play";
        PlayBtn.UseVisualStyleBackColor = true;
        PlayBtn.Click += new EventHandler(playBtn1_Click);
        return PlayBtn;
        }

  public BMS_main()
        {
        int startPos = 5;
        int endPos = 5;

        for (int i = 1; i <= playlistCountInc; i++)
        {
            playListGrp = addplayListGrp(i, startPos, endPos);
            playListLbl = addLabel(i);
            Label playListLblTime = addLabelTime(i);
            Button PlayBtn = addPlayBtn(i);
        }
        playListGrp.Controls.Add(playListLbl);
        playListGrp.Controls.Add(playListLblTime);
        playListGrp.Controls.Add(playlistView);
        playListGrp.Controls.Add(PlayBtn);
        }
 private void playBtn1_Click(object sender, EventArgs e)
       {
           if (ScreenStatus)
        {
            Playing = true;
            DisplayTimer.Stop();
            DisplayTimer.Enabled = false;
            InitialScreenTimer.Stop();
            InitialScreenTimer.Enabled = false;
            PlayListTimer.Enabled = true;
            PlayListTimer.Start();
        }
        else
        {
            message = "Veuillez alimenter les panneaux";
            result = MessageBox.Show(message, caption, buttons);
        }
  public void PlayListTimer_Tick(object sender, EventArgs e)
        {
            Label lblAcessorio4 = 
            (Label)playListLbl.Controls.Find("playLbl4", 
            true).FirstOrDefault();
        if (lblAcessorio4 != null)
        {
            lblAcessorio4.Text = "Test lblAcessorio4";
        }
        else
        {
            message = "Label is null";
            result = MessageBox.Show(message, caption, buttons);
        }
  • Happy to help, give me a question to start with? IE what part do you need help with? Enumerating the .controls collection? – Trey May 16 '18 at 20:11
  • 1
    Use the `Tag` property to store an identifier and then check it in the `playBtn1_Click` event (`sender` is the button that raised the event, cast it to `Button`). – Gusman May 16 '18 at 20:13
  • Perhaps you should put your _playListGrd.Add_ inside the loop that creates the controls. Otherwise you add just the last one set of controls – Steve May 16 '18 at 20:15
  • "How do I call a control that was created dynamically in c#" Asuming this is windows forms, the same way you call designer created Controlls. All the Designer really does, is work on a seperate part of the class. All the code it writes is executed when you call "InitializeComponents()" in the contructor. It can do nothing you can not do. – Christopher May 16 '18 at 20:28
  • @Trey, good point, so if I cleck on Button[i] generated automatically, how doI change the text of the corresponding Label. Ex: I Generated buttons dynamically and I press on Botton 2; how do I change the text of Label 2 – Michael Lambert May 16 '18 at 20:29
  • @steve, controls are created succesfully, it's using them the issue. Thanks :) – Michael Lambert May 16 '18 at 20:32
  • @Christopher, thought of that, but VS says the Label doesn't exist in this context. Thanks – Michael Lambert May 16 '18 at 20:33
  • @Gusman, can you elaborate, not custom to the tag property? – Michael Lambert May 16 '18 at 20:34
  • just do a foreach on the controls of the parent(container), and check the control name. Something like foreach (var control in mycontainer.controls) – Trey May 16 '18 at 20:37
  • There are two ways: 1. Create a variable. Assigned the created instance to the variable. Now you can use it under that name. You propably want a array, List or similar collection to store all those instancs. 2. You assign some unique identifier (like a Primary Key) as a tag to each button, lable, etc. in the group. In events you can then cast the sender to the appropirate type and access the tag proeprty. | Itterating over the controls is also a option, but not one I usually take, – Christopher May 16 '18 at 20:42

1 Answers1

0

Set the Tag property of your button with something which will help you decide later on which song to play:

playListGrp = addplayListGrp(i, startPos, endPos);
playListLbl = addLabel(i);
Label playListLblTime = addLabelTime(i);
Button PlayBtn = addPlayBtn(i);

// You can do this
PlayBtn.Tag = playListGrp; // or anything else

Then in the button click handler, get the value of the Tag and make a decision based on that. Just keep in mind that whatever you set the Tag to, you will need to cast it back to that type. For example, in the above I set it GroupBox so I will cast it to a GroupBox:

private void playBtn1_Click(object sender, EventArgs e)
{
    GroupBox gb = ((Button)(sender)).Tag as GroupBox;

    // Now make the decision
    if(gb.Name == "whatever you need to put here"){ // do whatever }
}

I would put the lisbox and then get the selected item and play that.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64