0

I'm currently working on an UML project. Right now, I have to figure out, how to move a class with all it's attributes by clicking and dragging. As of right now, the class is constructed with 2 rectangles and a draw string for the attributes and class name.

I probably have to put all of this into a list and then assign a mousedown event, however, I do not know how.

  private void button1_Click(object sender, EventArgs e)
    {
        Properties fcm = new Properties();
        DialogResult dr = fcm.ShowDialog();

        foreach (var r in rects)
        {
            if (dr == DialogResult.OK)
            {
                Graphics graph = pictureBox1.CreateGraphics();
                graph.Clear(Color.WhiteSmoke);

                Graphics g = pictureBox1.CreateGraphics();
                string nazev = fcm.GetNazev();

                gr = pictureBox1.CreateGraphics();
                Vykreslování vykres = new Vykreslování();
                vykres.třída(40, 40, 200, 170, gr, nazev);

                gr = pictureBox1.CreateGraphics();
                Vykreslování vykres1 = new Vykreslování();
                vykres1.třída(40, 40, 200, 25, gr, nazev);

                using (Font myFont = new Font("Arial", 14))
                {

                    g.DrawString(nazev, myFont, Brushes.Black, new Point(Convert.ToInt32((vykres1.Width - vykres1._TextWidth) / 2 + vykres1.Xpos), Convert.ToInt32(vykres1.Ypos + 5)));

This is what I use for creating the class itself. As you can see, I've tried putting it into a list via foreach, but it does not seem to be working.

Chernarussian
  • 45
  • 3
  • 7
  • Am I right in thinking that you have a single `PictureBox` for the entire drawing area, and that `rects` is a list of the individual classes to be drawn? If so, then a `MouseDown` event would need to work out from the mouse coordinates which of the `rects` was under the mouse. – Trevor Mar 12 '18 at 23:34
  • Why are you calling `CreateGraphics` so many times against the `PictureBox`? Call it once before the `for` loop and use that same `Graphics` object for the rest of the function. – Trevor Mar 12 '18 at 23:35
  • @Trevor Indeed, I do use one picturebox as a drawing area and rects is indeed a list for the classes. As for the CreateGraphics, I'm still learning and I do know the code is awfull but well, it works. – Chernarussian Mar 13 '18 at 13:11

0 Answers0