C# error in dymamico click event

Asked

Viewed 78 times

1

By clicking on the dynamico button of the event click Tiles , it opens several sites loaded from the file Settings.ini ... in case it should open one by one relative to the name and link but open all at the same time.

Note: I am using Metrotiles as buttons (Metroframework Components).

My file Settings.ini

  [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"));
        }
    }
  • What error appears ?

  • opens all links by clicking on the 1st button (in this case) and not a link to each button

2 answers

0


Your code is doing exactly what it should, although it’s not what you whether. Notice that at the event Click of the Tiles, you walk the IniFile whole and open each of the links.

From what I understand, you want each Tile open a link associated with it. There are several ways to do this, but the simplest for your case is the following: instead of using the same event for all Tiles, associate a different event for each, already at the time of creation.

See the code. I removed the other parts so that it is 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);

    // ... //
}
  • Vitor thank you for the answer ,but the error remains even worse now that I was once lost obj,args in the visual C# asked to create a private Eventhandler obj; was created but says args soh as statments

  • The parentheses were missing from the code. I already edited the answer if you want to use it.

  • your is correct too ! was what I need Thank you

  • a Purpose as it would be (int i = 0; i < 15; i++) for reading the number of names in aqruivo ....

  • I don’t know which class IniFile you are using. Take a look at her documentation or post another question.

  • my aqruivo ini used is from this page https://stackoverflow.com/questions/217902/reading-writing-an-ini-file The Tiny class

Show 1 more comment

0

I would assign the link in the property Tag from Tile and then pick up by Sender who fired 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() );
}
  • Thanks this Solved now works by boot !

  • @carloscoelho that good, do not forget to mark as reply! thanks

  • 1

    only one remark, you can state var a = new IniFile("Settings.ini"); out of the loop as it will be the same in all iterations, avoid this "waste" .

  • by the way how would you read the amount of names in aqruivo ini and use it here for (int i = 0; i < 15; i++) instead of 15 would put the value he read of the automatically added names

  • would have to see its class that is reading the ini, and certainly would have a Lis<> with each item of the ini, opens a question with this situation, to better verify

Browser other questions tagged

You are not signed in. Login or sign up in order to post.