Autor Beitrag
marvinjust
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Di 29.03.11 16:23 
Hallo,

bin neu hier und brauche dringend Eure Hilfe.
Ich möchte eine ganz einfache Anwendung schreiben, die wenn ich auf einen Button klicke, ein neues Rectangle zeichnet. Und wenn ich nochmal klicke, dann soll er es nochmal zeichen usw (natürlich Zufallszahlen für die Koordinaten). Ich habe noch einen Zähler programmiert, der zählt, wie viele Rectangle ich zeichne. Dieser funktioniert auch einwandfrei, aber die Anwendung will einfach keine Rectangle zeichnen.

Mein Quelltext sieht folgendermaßen aus:

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Übung
{
    public partial class Form1 : Form
        public Form1()
        {
            InitializeComponent();
        }

        private Point m_ptLocation;

        public Point Location
        {
            get { return m_ptLocation; }
            set { m_ptLocation = value; }
        }

        List<Rectangle> m_lstRects = new List<Rectangle>();

        private void picOutput_Paint(object sender, PaintEventArgs e)
        {
            foreach (Rectangle r in m_lstRects)
            {
                new Rectangle(Location.X, Location.Y, 5050);
            }
            lblStatus.Text = m_lstRects.Count.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Graphics g = picOutput.CreateGraphics();
            Pen p = new Pen(Brushes.Blue);
            g.DrawRectangle(p, 1001005050);

            Rectangle r = new Rectangle();
            Random rand = new Random();
            r.Location = new Point(rand.Next(100300), rand.Next(100300));
            m_lstRects.Add(r);

            picOutput.Invalidate();
        }
    }
}


Ich bitte um Eure Hilfe.

MfG
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19338
Erhaltene Danke: 1752

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Di 29.03.11 16:50 
Hallo und :welcome: im Forum!

Du hast da etwas verwechselt. In dem Ereignis Paint zeichnest du nichts, sondern erstellst nur neue Rechtecke, die du aber sofort wieder verwirfst.

Und in dem Click des Buttons zeichnest du, löst danach aber ein sofortiges Neuzeichnen aus, so dass alles wieder weg ist. :nut:

In deinem Button Click Event musst du die Daten füllen, dann das Neuzeichnen auslösen und im Paint Event die Daten zeichnen.