简体   繁体   中英

Drawing inside a picturebox (control) inside foreach

I'm trying to making dynamic controls (labels, pictureboxes and buttons) by making controls in a foreach. The foreach is controlled by datarows, which are created from an SQL function that I call for.

The problem is that my graphics don't seem to work on my pictureboxes as it is now.

So far I've got this as code: Global variables:

private int i = 0, beginningHeight = 70, addingToHeight = 55;
PictureBox picturebox = new PictureBox();

The functions:

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();
        }

Can anybody help me out here and tell me how I can add the graphics into my pictureboxes so I can see how much percentage of my picturebox should be filled? Here is a picture example to have a good look onto it:

As you see in the above image it currently doesn't work, and I've put data in the database for the first record named "Boodschappen" which should now be filled in by my graphics for 30% in this example.

Does anybody know a solution please? :) Thanks

Now the troubles only rise on this part: I'm not allowed to add the g to this.Controls.Add(g); it gives me the error Argument 1: cannot convert from 'System.Drawing.Graphics' to 'System.Windows.Forms.Control

It's clear you can't add Graphics as a control. Also if you draw that way your drawing will disappear when picturebox or form repainted. So you should draw inside the Paint event of picturebox.

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);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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