0

我正在尝试通过在 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%。

请问有人知道解决办法吗?:) 谢谢

4

1 回答 1

2

现在问题只出现在这一部分:我不允许将 g 添加到 this.Controls.Add(g); 它给了我错误参数 1: cannot convert from 'System.Drawing.Graphics' to 'System.Windows.Forms.Control

很明显,您不能添加Graphics为控件。此外,如果您以这种方式绘制,则在重新绘制图片框或表单时,您的绘图将消失。所以你应该在图片框的Paint事件内绘制。

双最大限制 = 0; int maxleftpos = 0; 私人无效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(10, beginningHeight + addingToHeight);
        maxleftpos = Math.Max(label.Left + label.Width, maxleftpos);
        this.Controls.Add(label);

        // getting the figures (max figures) from the db to show in a label
        double limiet = (double)dr.Field<double>("maximumBedrag");
        maxLimit = Math.Max(limiet, maxLimit);
        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 = 200;
        picturebox.Name = "picturebox" + i;
        picturebox.Height = 15;
        picturebox.Tag = limiet;
        picturebox.Location = new Point(100, (beginningHeight + 27) + addingToHeight);
        this.Controls.Add(picturebox);
        picturebox.BringToFront();
        //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++;

    }

    foreach (Control c in this.Controls)
    {
        if (c is PictureBox)
        {
            c.Location = new Point(maxleftpos, c.Top);
        }
    }
    if (this.Width<maxleftpos+150)
    {
        this.Width = maxleftpos + 50;
    }

    this.Refresh();

}

private void picturebox_Paint(object sender, PaintEventArgs e)
{

    PictureBox p = sender as PictureBox;
    Graphics gr = e.Graphics;
    gr.ResetTransform();
    //Graphics g = picturebox.CreateGraphics();
    int breedteGebruikt = Convert.ToInt32((double)p.Tag);
    int max = Convert.ToInt32(maxLimit);
    int grwidht = breedteGebruikt * p.Width / max;



    gr.DrawRectangle(new Pen(Color.Red), new Rectangle(10, 5, 50, 5));
    gr.FillRectangle(Brushes.Green, 0, 0, p.Width, p.Height);
    gr.FillRectangle(Brushes.Red, 0, 0, grwidht, p.Height);

}
于 2013-04-13T09:53:08.120 回答