C#控件创建的控件 [英] C# control created controls

查看:90
本文介绍了C#控件创建的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

this is my code

public string filepath = "";

    public int addpicnumber = 0;
    private void DragAndDropArea_Load(object sender, EventArgs e)
    {
     #region Areoload
        //load the areo into the form
        this.BackColor = Color.Black;
        MARGINS m = new MARGINS();
        m.cxLeftWidth = -1;
        m.cxRightWidth = -1;
        m.cyBottomHeight = -1;
        m.cyTopHeight = -1;
        //set all value -1 to apply glass effect to the all of visible window
        DwmExtendFrameIntoClientArea(this.Handle, m);
        //end load areo code
        #endregion

        if (this.Name == "Dock1")
        {
            for (addpicnumber = 0; addpicnumber <= Properties.Settings.Default.Dock1.Count; addpicnumber++)
            {
                PictureBox newpic = new PictureBox();

                newpic.Tag = Properties.Settings.Default.Dock1[addpicnumber];
                this.toolTip1.SetToolTip(newpic, newpic.Tag.ToString());

                newpic.Cursor = System.Windows.Forms.Cursors.Hand;
                newpic.Size = new Size(80, 70);
                newpic.BackColor = Color.Red;
                newpic.Name = "pic" + addpicnumber.ToString();
                newpic.Location = new Point(0, newpic.Location.Y + (addpicnumber * 90));
                flowLayoutPanel1.Controls.Add(newpic);
            }
        }

        #region allowthedrop
        // Enable drag and drop for this form
        // (this can also be applied to any controls)
        this.AllowDrop = true;
        #endregion
    }




基本上它创建了picureboxes可以说

Properties.Settings.Default.Dock1.Count

设置为10,它将产生10个picurebox,但请记住,10可能是更高或更低的数字,例如1-100.我想知道如何访问它们,例如,当我单击图片框时,我希望它显示一个消息框,告诉我图片框标签的名称.




basically it creates picureboxes lets say the

Properties.Settings.Default.Dock1.Count

is set to 10, it will make ten picureboxes but still keep in minds that that 10 could be a higher or lower number like 1-100. I was wondering how i can access them, for example, when I click the picurebox, i want it to show a message box telling me the pictureboxs tag name. and it will do this from the picurebox that you have clicked, i was wondering if i can get some help?

推荐答案

有一个ControlCollection您的代码已访问通过flowLayoutPanel1.Controls.Add(newpic);.您可以遍历此集合,并按其名称查找单个PictureBox(已将其设置为"pic" + addpicnumber).

对于单击问题,您必须在初始化期间为每个PictureBox的OnClick事件订阅一个事件处理程序.您可以根据需要为一个PictureBox使用一个事件处理程序.
There is a ControlCollection your code already accesses via flowLayoutPanel1.Controls.Add(newpic);. You can traverse this collection looking for an individual PictureBox by its name (you have set it to "pic" + addpicnumber).

For the click issue, you would have to subscribe an event handler to each PictureBox''s OnClick event during initialization. You can use one event handler for as many PictureBoxes as you need.
private void DragAndDropArea_Load(object sender, EventArgs e)
{

...

newpic.Name = "pic" + addpicnumber.ToString();
newpic.Location = new Point(0, newpic.Location.Y + (addpicnumber * 90));
newpic.Click += new EventHandler(PictureBoxClicked);

...
}


private void PictureBoxClicked(object sender, EventArgs e)
{
    PictureBox clickOrigin = sender as PictureBox;
    if( clickOrigin != null)
    {
        Console.Writeline("Clicked on " + clickOrigin.Name + ".");
    }
}


这篇关于C#控件创建的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆