简体   繁体   English

循环添加控件(Picturebox)

[英]add controls (Picturebox) in loop

What i have: Big PictureBox (lets call it Pic_Map) on the form. 我所拥有的:窗体上的Big PictureBox(称为Pic_Map)。 A class Ore.cs , A List<Ore> ores; 一个Ore.cs类,一个List<Ore> ores; and a database that pulls the data and places it into the ores list. 还有一个数据库,用于提取数据并将其放入ores列表。

Functionality: So, The functionality of this is i have a TextBox/Combobox and a Button. 功能性:因此,此功能是我有一个TextBox / Combobox和一个Button。 When i press the Button, it will loop through the ores list and Dynamically add PictureBoxes ontop of the Pic_Map based on TexBox/ComboBox being equal to the data (in this case Ore_Name). 当我按下按钮时,它将循环浏览ores列表,并根据TexBox / ComboBox等于数据(在此情况下为Ore_Name)在Pic_Map的顶部动态添加PictureBoxes。

Problem: This all works fine, but the problem is that when i add the PictureBoxes Dynamically, it only seems to add the last value on the ores list (Red circles on Pic_Map). 问题:一切正常,但是问题是,当我动态添加PictureBoxes时,似乎只在ores列表中添加了最后一个值(Pic_Map上的红色圆圈)。 so, it ends up showing only 1 PictureBox instead of lets say 3, since i have 3 value Names that match with the TextBox/ComboBox. 因此,由于我有3个与TextBox / ComboBox匹配的值名称,因此最终只显示1个PictureBox而不是3个。

Question: How to get it to work like when i write/choose "Flame Stone" it looks on all the data that has "Flame Stone" in its name and add it (Instead of it adding only the last value from the list). 问题:如何使它像我编写/选择“ Flame Stone”时那样工作,它查找名称中具有“ Flame Stone”的所有数据并将其添加(而不是仅添加列表中的最后一个值)。

Code: 码:

private void PopulateComboBoxByName()
    {
        PictureBox ore_Area = new PictureBox(); 

        db.GetOre(); //Getting data and putting it into "ores" list

        foreach (Ore ore in db.ores)
        {
            if (CBOX_Filter.SelectedItem.ToString() == ore.Ore_Name)
            {
                int area_Width = Convert.ToInt32(ore.Area_Width);
                int area_Height = Convert.ToInt32(ore.Area_Height);

                int ore_Width = Convert.ToInt32(ore.Ore_Width);
                int ore_Height = Convert.ToInt32(ore.Ore_Height) - area_Height / 2;
                ore_Area.Name = "ore_Area";

                ore_Area.ImageLocation = @"Data\Images\Circle.png";
                ore_Area.SizeMode = PictureBoxSizeMode.StretchImage;

                ore_Area.Size = new Size(Convert.ToInt32(area_Width), Convert.ToInt32(area_Height));
                ore_Area.Location = new Point(Convert.ToInt32(ore_Width), Convert.ToInt32(ore_Height));
                ore_Area.BackColor = Color.Transparent;

                this.Controls.Add(ore_Area);
            }
        }

        ore_Area.Parent = PIC_Map;
    }

Images: 图片:

Data: 数据: 数据

Map: 地图: 动态图片框(红色圆圈)

Thanks to LarsTech, it has been fixed. 感谢LarsTech,它已得到修复。

Making the picturebox is moved inside the loop, this.controls.add is changed to PIC_Map.controls.add and Ore_Area.Parent = PIC_Map; 使PIC_Map.controls.add在循环内移动, this.controls.add更改为PIC_Map.controls.addOre_Area.Parent = PIC_Map; is deleted. 被删除。

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

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