C # error in the dymamico event button click

1

When you click on the dynamic button of the click tiles event, it opens several sites loaded from the settings.ini file ... in case it should open one by one relative to the name and link but opens all at the same time.

Note: I'm using MetroTiles as buttons (MetroFramework Components).

My settings.ini file

  [bot]
  b00=Debian
  b01=Mineos
  b02=Utorrent
  b03=Debian Apache Web Server 
  b04=Debian Mysql Web Server
  b05=Ez Monitor
  b06=Netdata

  [web]
  h00=https://debian:10100/
  h01=https://debian:8443/
  h02=https://debian:8080/gui/
  h03=http://ccstudio.zapto.org/
  h04=http://debian/phpmyadmin/
  h05=http://debian/app/cc/linux/eZ/
  h06=http://debian:19999

My code

      //TILES ADD 

        for (int i = 0; i < 15; i++)
        {
            var a = new IniFile("Settings.ini");
            var b = new MetroTile();
            var c = new MetroLabel();

            b.Height = 50;
            b.Width = 100;

            b.Margin = new Padding(5, 5, 5, 5);       
            b.Click += new EventHandler(b_Click);
            b.MouseEnter += new EventHandler(b_Move);
            b.Text = a.Read("b0" + i . ToString(), "bot");
            //c.Text = a.Read("h0" + i . ToString(), "web");
            //c.Hide();
            b.UseCustomBackColor = true;
            b.BackColor = GetRandomColor();
            flowLayoutPanel1.Controls.Add(b);
           /// flowLayoutPanel1.Controls.Add(c);
        }

    }
    private void b_Click(object sender, EventArgs e)
    {
        var a = new IniFile("Settings.ini");
        for (int i = 1; i < 11; i++)
        {

            Process.Start(a.Read("h0" + i.ToString(), "web"));
        }
    }
    
asked by anonymous 02.08.2017 / 18:55

2 answers

0

Your code is doing exactly what it should, although not what you wants . Notice that in the event Click of Tiles, you go through the IniFile integer and open each of the links.

As far as I understand, you want every Tile to open a link associated with it. There are several ways to do this, but the simplest one for your case is this: instead of using the same event for all Tiles, associate a different event for each one by the time it was created.

See the code. I've removed the other parts to make it easier for you to view the change.

for (int i = 0; i < 15; i++)
{
    var a = new IniFile("Settings.ini");
    var b = new MetroTile();

    // ... //

    var link = a.Read("h0" + i.ToString(), "web");
    b.Click += (obj, args) => Process.Start(link);

    // ... //
}
    
02.08.2017 / 19:10
0

I'd assign the link in the Tag property of Tile and then pick up the sender that triggered the event:

{
    for (int i = 0; i < 15; i++)
    {
        var a = new IniFile("Settings.ini");
        var b = new MetroTile();
        var c = new MetroLabel();

        b.Height = 50;
        b.Width = 100;

        b.Margin = new Padding(5, 5, 5, 5);       
        b.Click += new EventHandler(b_Click);
        b.MouseEnter += new EventHandler(b_Move);
        b.Text = a.Read("b0" + i . ToString(), "bot");

        b.Tag = a.Read("h0" + i . ToString(), "web"); //atribui o link na tag do tile

        //c.Text = a.Read("h0" + i . ToString(), "web");
        //c.Hide();
        b.UseCustomBackColor = true;
        b.BackColor = GetRandomColor();
        flowLayoutPanel1.Controls.Add(b);
       /// flowLayoutPanel1.Controls.Add(c);
    }

}
private void b_Click(object sender, EventArgs e)
{
       Process.Start( ((MetroTile)sender).Tag.ToString() );
}
    
02.08.2017 / 19:22