M
M
Maxim K2022-02-11 22:23:39
C++ / C#
Maxim K, 2022-02-11 22:23:39

Why doesn't the code in C# work?

Hello.
There is a code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {

        Bitmap gBitmap;
        Pen MyPen0;
        Pen MyPen;
        Graphics gScreen;
        Bitmap bitmap;
        TElement[] Element;

        public Form1()
        {
            InitializeComponent();

        }

        public struct TElement
        {
            public int x;
            public int y;
            public int inf;
            public Color color;
        }

        void Sort()
        {
            int L = Element.Length;
            for (int i = 1; i <= L - 1; i++)
                for (int j = i; j <= L - 1; j++)
                {
                    TElement tmp;
                    if (Element[j - 1].inf > Element[j].inf)
                    {
                        tmp = Element[j];
                        Element[j] = Element[j - 1];
                        Element[j - 1] = tmp;
                    }
                }
        }

        void SetRandom()
        {
            int L = Element.Length;
            Random rnd = new Random();
            for (int i = 0; i <= L - 1; i++)
            {
                Element[i].x = 100;
                Element[i].y = 20 + i * 40;
                Element[i].color = Color.Black;
                Element[i].inf = rnd.Next(100);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SetRandom();
            Drawing(-1, -1);
        }

        void Drawing(int n, int m)
        {
            const int d = 15;
            int L = Element.Length;
            string s;
            SizeF size;
            gBitmap.Clear(Color.White);
            for (int i = 0; i <= L - 1; i++)
            {
                MyPen.Color = Element[i].color;
                gBitmap.DrawEllipse(MyPen, Element[i].x - d, Element[i].y - d, 2 * d, 2 * d);
                s = Convert.ToString(Element[i].inf);
                size = gBitmap.MeasureString(s, Font);
                gBitmap.DrawString(s, Font, Brushes.Black,  Element[i].x - size.Width / 2,
                Element[i].y - size.Height / 2);
            }
            if (n != -1)
            {
                MyPen0.Color = Color.Black;
                gBitmap.DrawLine(MyPen0, 120, Element[n].y, 140, Element[n].y);
                s = "I = " + Convert.ToString(n);
                size = gBitmap.MeasureString(s, Font);
                gBitmap.DrawString(s, Font, Brushes.Black, 150, Element[n].y - size.Height / 2);
            }
            if (m != -1)
            {
                MyPen0.Color = Color.Red;
                gBitmap.DrawLine(MyPen0, 120, Element[m].y, 140,
                Element[m].y);
                s = "J = " + Convert.ToString(m);
                size = gBitmap.MeasureString(s, Font);
                gBitmap.DrawString(s, Font, Brushes.Black, 150, Element[m].y - size.Height / 2);
            }
            gScreen.DrawImage(bitmap, ClientRectangle);
        }


        void Change(int n1, int n2, int n, int m)
        {
            Element[n1].color = Color.Red;
            Element[n2].color = Color.Red;
            int x1 = Element[n1].x;
            int y1 = Element[n1].y;
            int x2 = Element[n2].x;
            int y2 = Element[n2].y;
            double x;
            for (int t = 1; t <= 15; t++)
            {
                x = (y2 - y1) * t / 15;
                Element[n1].y = y1 + (int)(x);
                switch (t)
                {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                        x = 40 * t / 4;
                        Element[n2].x = x1 - (int)(x);
                        break;
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                    case 10:
                    case 11:
                        x = (y1 - y2) * (t - 4) / 7;
                        Element[n2].y = y2 + (int)(x);
                        break;
                    case 12:
                    case 13:
                    case 14:
                    case 15:
                        x = 40 * (t - 11) / 4;
                        Element[n2].x = x1 - 40 + x;
                        break;
                }
                Drawing(n, m);
                Thread.Sleep(100);
            }
            Element[n1].color = Color.Black;
            Element[n2].color = Color.Black;
            Drawing(n, m);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            gScreen = CreateGraphics();
        }
    }
}


The compiler complains about:
gBitmap.Clear(Color.White); - does not see the clear method, does not see the DrawEllipse, MeasureString method. Because of which ??? Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
twobomb, 2022-02-11
@twobomb

Well, because the bitmap does not have such methods. These are the methods of Graphics. Use

Graphics g = Graphics.FromImage(gBitmap);
g.Clear(Color.White);

etc.

@
@insighter, 2022-02-11
_

Why should you see? Are you from Delphi?

var g = Graphics.FromImage(gBitmap);
g.DrawEllipse(...)
g.MeasureString(...)

public struct TElement
it is not customary to write type names with the letter T here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question