在foreach内部绘制一个picturebox(控件)

时间:2013-04-13 09:33:20

标签: c# graphics drawing picturebox

我正在尝试通过在foreach中进行控制来制作动态控件(标签,图片框和按钮)。 foreach由datarows控制,datarows是从我调用的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%。

有人知道解决方案吗? :) 感谢

1 个答案:

答案 0 :(得分:2)

  

现在这个问题只会出现问题:我不允许添加g   to this.Controls.Add(g);它给了我错误参数1:不能   从'System.Drawing.Graphics'转换为   “System.Windows.Forms.Control的

很明显,您无法添加Graphics作为控件。此外,如果您以这种方式绘制,当图片框或表格重新绘制时,您的绘图将消失。所以你应该在图片框的Paint事件中画画。

double maxLimit = 0;     int maxleftpos = 0;     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(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);

}