简体   繁体   English

如何为动态创建的元素设置不同的事件

[英]How to set different events for dynamically created element

Im trying to create event with different index for dynamically created GroupBox. 我正在尝试为动态创建的GroupBox创建具有不同索引的事件。 With my actual code event for every groupbox is that same. 对于每个分组框,我的实际代码事件是相同的。 How can i make event with different index for every groupbox? 如何为每个组框创建具有不同索引的事件? My Code: 我的代码:

public void LoadGry()
        {

           // GroupBox groupbox = new GroupBox();
            Label nazwagry = new Label();
            for(int i = 0; i < myCollection.Count; i++)
            {
                GroupBox groupbox = new GroupBox();
                groupbox.Text = myCollection[i];
                groupbox.Size = new Size(290, 131);
                groupbox.Location = new Point(6, 150 * (myCollection.Count - i - 1));
                groupbox.ForeColor = Color.White;
                Label label1 = new Label();
                label1.Text = groupbox.Text;
                label1.AutoSize = true;
                label1.Location = new Point(groupbox.Location.X + 80, groupbox.Location.Y + 20);
                groupbox.Controls.Add(label1);
                Gry.Controls.Add(label1);

                PictureBox picturebox = new PictureBox();
                picturebox.Location = new Point(groupbox.Location.X + 5, groupbox.Location.Y + 20);
                picturebox.Size = new Size(75, 75);
                picturebox.SizeMode = PictureBoxSizeMode.StretchImage;
                picturebox.LoadAsync(myCollection3[i]);
                groupbox.Click += new EventHandler(delegate {groupboxclick(groupbox, picturebox, i);});
                Label label2 = new Label();
                label2.Text = "Status: " + "Aktualny";
                label2.ForeColor = Color.Green;
                label2.AutoSize = true;
                label2.Location = new Point(label1.Location.X, label1.Location.Y + 20);
                Gry.Controls.Add(label2);
                Label zapiszopis = new Label();
                zapiszopis.Text = myCollection4[i];
                zapiszopis.Visible = false;

                Gry.Controls.Add((Control)groupbox);    

                //MessageBox.Show("pokaz mi wysokosc");
                }
            }
private void groupboxclick(GroupBox groupbox, PictureBox picturebox, int itest)
        {


            groupbox.ForeColor = Color.Aqua;
            this.pictureBox1.BackgroundImage = picturebox.BackgroundImage;
            opishacka.Text = myCollection4[itest];
        }

The problem is that the event setup is using the variable K value. 问题在于事件设置使用变量K值。 For use the number instead you probably needs to create an expression manually to use the current value in each case. 为了使用数字,您可能需要手动创建一个表达式以在每种情况下使用当前值。

BUT You can easily do what you want using the following properties to attach values to controls. 但是,您可以使用以下属性轻松地执行所需的操作,以将值附加到控件。

1-) Tag in WinForms & WPF: 1-)WinForms和WPF中的标签:

// Setup
pictureBox.Tag = i;

// Event
int i = (int) pictureBox.Tag;

2-) ViewState in WebForms 2-)WebForms中的ViewState

// Setup
ViewState[pictureBox.UniqueID] = i;

// Event
int i = (int) ViewState[pictureBox.UniqueID];

You can use many other techniques. 您可以使用许多其他技术。 I only post one for each popular framework. 对于每个流行的框架,我只会发布一个。 I guest that you are in a WinFors project. 我邀请您参加WinFors项目。

Hope this help! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM