我正在尝试通过在 foreach 中制作控件来制作动态控件(标签、图片框和按钮)。foreach 由数据行控制,这些数据行是从我调用的 SQL 函数创建的。
问题是我的图形似乎不能像现在这样在我的图片框上工作。
到目前为止,我已经将其作为代码:全局变量:
private int i = 0, beginningHeight = 70, addingToHeight = 55;
PictureBox picturebox = new PictureBox();
功能:
private void tonenAlleCategorieen()
{
foreach (DataRow dr in blCategorie.getAlleCategorieenMetLimieten())
{
//making labels dyanmic and fill them with the correct text (from database)
string categorie = (string)dr.Field<string>("Omschrijving");
Label label = new Label();
label.BackColor = Color.Transparent;
label.ForeColor = Color.FromArgb(97, 97, 97);
label.Font = new Font("Myriam Pro", 10, FontStyle.Bold);
label.Width = 200;
label.Name = categorie;
label.Text = categorie;
label.BackColor = Color.Transparent;
label.Location = new Point(30, beginningHeight + addingToHeight);
this.Controls.Add(label);
// getting the figures (max figures) from the db to show in a label
double limiet = (double)dr.Field<double>("maximumBedrag");
Label labeltest = new Label();
labeltest.BackColor = Color.Transparent;
labeltest.ForeColor = Color.FromArgb(97, 97, 97);
labeltest.Font = new Font("Myriam Pro", 8, FontStyle.Bold);
labeltest.Width = 200;
labeltest.Name = Convert.ToString(limiet);
labeltest.Text = "Limiet: " + Convert.ToString(limiet) + "€";
labeltest.BackColor = Color.Transparent;
labeltest.Location = new Point(30, (beginningHeight + 27) + addingToHeight);
this.Controls.Add(labeltest);
//making pictureboxes for every single row in the db
PictureBox picturebox = new PictureBox();
picturebox.Width = 400;
picturebox.Name = "picturebox" + i;
picturebox.Height = 15;
picturebox.Location = new Point(30, (beginningHeight + 27) + addingToHeight);
this.Controls.Add(picturebox);
//calling the paint event for drawing inside the pictureboxes
picturebox.Paint += new PaintEventHandler(picturebox_Paint);
//adjusting height (55px extra per new row)
beginningHeight += 55;
i++;
}
}
private void picturebox_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
//draw here
//Graphics g = picturebox.CreateGraphics();
int x = 30;
int y = (beginningHeight + 27) + addingToHeight;
int breedteGebruikt = 200;
int breedteNietGebruikt = picturebox.Width - breedteGebruikt;
int hoogteBalk = picturebox.Height;
g.DrawRectangle(new Pen(Color.Red), new Rectangle(10, 5, 50, 5));
g.FillRectangle(Brushes.Green, x, y, breedteNietGebruikt, hoogteBalk);
g.FillRectangle(Brushes.Red, x, y, breedteGebruikt, hoogteBalk);
picturebox.Refresh();
}
任何人都可以在这里帮助我并告诉我如何将图形添加到我的图片框中,以便我可以看到我的图片框应该填充多少百分比?这是一个图片示例,可以很好地了解它:
正如您在上图中看到的那样,它当前不起作用,并且我已将名为“Boodschappen”的第一条记录的数据放入数据库中,在此示例中,现在应该由我的图形填充 30%。
请问有人知道解决办法吗?:) 谢谢