简体   繁体   English

C# windows 形式,线不会画! canvas 未定义

[英]C# windows form, Line wont Draw! canvas is not defined

What is needed to define 'canvas' on line 27?在第 27 行定义“画布”需要什么? I've looked everywhere but it only gives respoenses of why an object might not be defined, and not about why the specific object canvas is not defined.我到处都看过,但它只给出了为什么 object 可能没有被定义的原因,而不是为什么特定的 object canvas 没有被定义。 Can someone tell me what i'm Missing?有人能告诉我我想念什么吗?


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

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void canvas_Paint(object sender, PaintEventArgs e)
        {
            Graphics gObject = canvas.CreateGraphics();

            Brush red = new SolidBrush(Color.Red);
            Pen redPen = new Pen(red, 8);

            gObject.DrawLine(redPen, 10, 10, 35, 500);
        }
    }
}

That canvas must be a control in the form.那个canvas肯定是窗体中的一个控件。 You got to add it in the designer and give it the canvas name.你必须在设计器中添加它并给它命名canvas What kind?哪一种? Hmm… I'd say use a PictureBox.嗯……我会说使用 PictureBox。

You would also need to link the Paint event to canvas_Paint from the properties panel (events tab).您还需要从属性面板(事件选项卡)将Paint事件链接到canvas_Paint And yes, e.Graphics is preferred over using CreateGraphics .是的, e.Graphics优于使用CreateGraphics

Where did you get that code from?你从哪里得到的代码?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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