Когда я создаю приложение и запускаю его, у меня не возникает никаких проблем или ошибок.
Я понимаю сообщение об ошибке исключения, что какой-то объект имеет значение null.
но я не могу найти, в чем проблема является. и я не могу использовать точки останова, потому что они не находятся в среде выполнения.
когда я пытаюсь просмотреть конструктор, я получаю эту ошибку исключения:
< img alt="нулевое исключение при попытке просмотра дизайнера." src="https://i.sstatic.net/Lh4PoLOd.jpg" />
Я знаю коды немного длинные, но не могу определить, в чем проблема. я пытался сузить коды здесь, но все они связаны друг с другом.
это произошло впервые, когда я добавил этот новый код файла класса модуля:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TestApp
{
public class CustomButton : Button
{
public enum ShapeType
{
Shape1,
Shape2,
Shape3,
Shape4
}
public ShapeType ButtonShape { get; set; }
public CustomButton()
{
this.Size = new Size(100, 100);
this.FlatStyle = FlatStyle.Flat;
this.FlatAppearance.BorderSize = 0;
this.BackColor = Color.White;
if (!this.DesignMode)
{
this.Paint += CustomButton_Paint;
}
}
private void CustomButton_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Black, 4); // הגדרת עיפרון שחור עבה
switch (ButtonShape)
{
case ShapeType.Shape1:
DrawShape1(g, pen);
break;
case ShapeType.Shape2:
DrawShape2(g, pen);
break;
case ShapeType.Shape3:
DrawShape3(g, pen);
break;
case ShapeType.Shape4:
DrawShape4(g, pen);
break;
}
}
private void DrawShape1(Graphics g, Pen pen)
{
Rectangle mainRect = new Rectangle(10, 10, 80, 40);
g.DrawRectangle(pen, mainRect);
int smallWidth = mainRect.Width / 2;
int smallHeight = mainRect.Height / 2;
Rectangle topRect = new Rectangle(mainRect.X, mainRect.Y, smallWidth, smallHeight);
Rectangle bottomRect = new Rectangle(mainRect.X + smallWidth, mainRect.Y, smallWidth, mainRect.Height);
g.DrawRectangle(pen, topRect);
g.DrawRectangle(pen, bottomRect);
}
private void DrawShape2(Graphics g, Pen pen)
{
Rectangle mainRect = new Rectangle(10, 10, 80, 40);
g.DrawRectangle(pen, mainRect);
Rectangle sideRect = new Rectangle(mainRect.X + mainRect.Width / 2, mainRect.Y, mainRect.Width / 2, mainRect.Height);
g.FillRectangle(Brushes.Black, sideRect); // מילוי חצי מהכפתור בצבע שחור
}
private void DrawShape3(Graphics g, Pen pen)
{
Rectangle mainRect = new Rectangle(10, 10, 80, 40);
g.DrawRectangle(pen, mainRect);
Rectangle leftRect = new Rectangle(mainRect.X, mainRect.Y + mainRect.Height / 2, mainRect.Width / 2, mainRect.Height / 2);
Rectangle rightRect = new Rectangle(mainRect.X + mainRect.Width / 2, mainRect.Y + mainRect.Height / 2, mainRect.Width / 2, mainRect.Height / 2);
g.DrawRectangle(pen, leftRect);
g.DrawRectangle(pen, rightRect);
}
private void DrawShape4(Graphics g, Pen pen)
{
Rectangle mainRect = new Rectangle(10, 10, 80, 40);
g.DrawRectangle(pen, mainRect);
Rectangle smallSquare = new Rectangle(mainRect.X + mainRect.Width / 2, mainRect.Y + mainRect.Height / 2, mainRect.Width / 2, mainRect.Height / 2);
g.FillRectangle(Brushes.Yellow, smallSquare); // מילוי ריבוע קטן בצבע צהוב
}
}
}
и код дизайнера:
using System.Drawing;
using System.Windows.Forms;
namespace TestApp
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
private Button btnSave;
private Panel panelCollage;
private Label lblTitle;
private Label lblInstructions;
private Button btnSelectImages;
// כפתורי הצורות
private CustomButton customButton1;
private CustomButton customButton2;
private CustomButton customButton3;
private CustomButton customButton4;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.btnSave = new System.Windows.Forms.Button();
this.panelCollage = new System.Windows.Forms.Panel();
this.lblTitle = new System.Windows.Forms.Label();
this.lblInstructions = new System.Windows.Forms.Label();
this.btnSelectImages = new System.Windows.Forms.Button();
// יצירת הכפתורים של הצורות
this.customButton1 = new CustomButton { ButtonShape = CustomButton.ShapeType.Shape1 };
this.customButton2 = new CustomButton { ButtonShape = CustomButton.ShapeType.Shape2 };
this.customButton3 = new CustomButton { ButtonShape = CustomButton.ShapeType.Shape3 };
this.customButton4 = new CustomButton { ButtonShape = CustomButton.ShapeType.Shape4 };
this.SuspendLayout();
//
// btnSave
//
this.btnSave.BackColor = System.Drawing.Color.Red;
this.btnSave.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnSave.Font = new System.Drawing.Font("Arial", 10F, System.Drawing.FontStyle.Bold);
this.btnSave.ForeColor = System.Drawing.Color.White;
this.btnSave.Location = new System.Drawing.Point(530, 523);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(130, 40);
this.btnSave.TabIndex = 3;
this.btnSave.Text = "Save Collage";
this.btnSave.UseVisualStyleBackColor = false;
this.btnSave.Click += new System.EventHandler(this.BtnSave_Click);
//
// panelCollage
//
this.panelCollage.AllowDrop = true;
this.panelCollage.BackColor = System.Drawing.Color.SteelBlue;
this.panelCollage.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panelCollage.Location = new System.Drawing.Point(20, 197);
this.panelCollage.Name = "panelCollage";
this.panelCollage.Size = new System.Drawing.Size(640, 320);
this.panelCollage.TabIndex = 2;
//
// lblTitle
//
this.lblTitle.Font = new System.Drawing.Font("Arial", 16F, System.Drawing.FontStyle.Bold);
this.lblTitle.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(50)))), ((int)(((byte)(50)))), ((int)(((byte)(50)))));
this.lblTitle.Location = new System.Drawing.Point(10, 10);
this.lblTitle.Name = "lblTitle";
this.lblTitle.Size = new System.Drawing.Size(660, 30);
this.lblTitle.TabIndex = 0;
this.lblTitle.Text = "Create Your Collage!";
this.lblTitle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// lblInstructions
//
this.lblInstructions.Font = new System.Drawing.Font("Arial", 10F, System.Drawing.FontStyle.Italic);
this.lblInstructions.ForeColor = System.Drawing.Color.Gray;
this.lblInstructions.Location = new System.Drawing.Point(17, 73);
this.lblInstructions.Name = "lblInstructions";
this.lblInstructions.Size = new System.Drawing.Size(643, 20);
this.lblInstructions.TabIndex = 1;
this.lblInstructions.Text = "Select images below to start creating your collage.";
this.lblInstructions.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// btnSelectImages
//
this.btnSelectImages.BackColor = System.Drawing.Color.LimeGreen;
this.btnSelectImages.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnSelectImages.Font = new System.Drawing.Font("Arial", 10F, System.Drawing.FontStyle.Bold);
this.btnSelectImages.ForeColor = System.Drawing.Color.White;
this.btnSelectImages.Location = new System.Drawing.Point(270, 115);
this.btnSelectImages.Name = "btnSelectImages";
this.btnSelectImages.Size = new System.Drawing.Size(130, 40);
this.btnSelectImages.TabIndex = 4;
this.btnSelectImages.Text = "Select Images";
this.btnSelectImages.UseVisualStyleBackColor = false;
this.btnSelectImages.Click += new System.EventHandler(this.btnSelectImages_Click);
//
// customButton1
//
this.customButton1.Location = new System.Drawing.Point(20, 600);
this.customButton1.Size = new System.Drawing.Size(50, 50);
//
// customButton2
//
this.customButton2.Location = new System.Drawing.Point(80, 600);
this.customButton2.Size = new System.Drawing.Size(50, 50);
//
// customButton3
//
this.customButton3.Location = new System.Drawing.Point(140, 600);
this.customButton3.Size = new System.Drawing.Size(50, 50);
//
// customButton4
//
this.customButton4.Location = new System.Drawing.Point(200, 600);
this.customButton4.Size = new System.Drawing.Size(50, 50);
//
// Form1
//
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(245)))), ((int)(((byte)(245)))), ((int)(((byte)(245)))));
this.ClientSize = new System.Drawing.Size(680, 700);
this.Controls.Add(this.btnSelectImages);
this.Controls.Add(this.lblTitle);
this.Controls.Add(this.lblInstructions);
this.Controls.Add(this.panelCollage);
this.Controls.Add(this.btnSave);
// הוספת הכפתורים לטופס
this.Controls.Add(this.customButton1);
this.Controls.Add(this.customButton2);
this.Controls.Add(this.customButton3);
this.Controls.Add(this.customButton4);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Test App";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.ResumeLayout(false);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... -null-exce