【问题标题】:Drawing inside a picturebox (control) inside foreach在 foreach 内的图片框(控件)内绘图
【发布时间】:2013-04-05 20:46:02
【问题描述】:

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

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

【问题讨论】:

    标签: c# graphics drawing picturebox


    【解决方案1】:

    现在麻烦只出现在这部分:我不允许添加 g 到 this.Controls.Add(g);它给了我错误参数1:不能 从“System.Drawing.Graphics”转换为 'System.Windows.Forms.Control

    很明显,您不能将Graphics 添加为控件。此外,如果您以这种方式绘制,则在重新绘制图片框或表单时,您的绘图将消失。所以你应该在picturebox的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);
    
    }
    

    【讨论】:

    • 嗯,谢谢,这有助于我进入好方法!现在我的问题是,我如何告诉我的 picturebox_paint 方法他在另一个函数中的哪一行?以及如何让变量在我的 picturebox_paint 函数中可访问?我仍然有点困惑,到目前为止我做了这个:i.imgur.com/GNYxIAw.png
    • 您可以添加关于绘制内容的全局变量。并在它们更改时调用 picturebox.Refresh()。
    • 好的,我创建了这些全局变量:private int i = 0, beginhoogte = 70, verhogenMet = 55;图片框图片框=新图片框();所以现在我有了这个(见图片),但它似乎仍然没有在我的表单中绘制图形? i.imgur.com/WbNftfK.png i.imgur.com/dtMUYKQ.png 编辑:我是否也应该再次参加pictureboxpaint 事件?就像我在其他功能中所做的那样? foreach(blCategorie.getAlleCategorieenMetLimieten() 中的 DataRow dr)
    • 您能否更新您的问题以显示当前代码。翻译 cmets 也很有用。
    • 更新,改写了一些英文 cmets + 英文变量,希望现在好一点!
    猜你喜欢
    • 2012-12-08
    • 2015-07-11
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多