Динамическое создание компонентов и подписка их на события 2 - C#
Формулировка задачи:
Добрый день!
В процессе работы возник ещё один вопрос.
Программа у меня запускается, но при нажатии на panel мне выдаёт такую ошибку: "В экземпляре объекта не задана ссылка на объект."
Ошибка возникает в этом участке кода:
Вот весь код целиком:
Подскажите, пожалуйста, в чём ошибка.
panel[i].Click += new EventHandler((s, args) => { (s as Panel).BackColor = Color.Black; for (int j = 0; j < panel.Length; j++) { if ((panel[j].Left == ((s as Panel).Left - 5) && panel[j].Top == ((s as Panel).Top - 30) && panel[j].BackColor == Color.Black) || (panel[j].Left == ((s as Panel).Left - 35) && panel[j].Top == (s as Panel).Top && panel[j].BackColor == Color.Black) || (panel[j].Left == ((s as Panel).Left - 5) && panel[j].Top == ((s as Panel).Top + 5) && panel[j].BackColor == Color.Black)) panel[i - 1].BackColor = Color.Black; if ((panel[j].Left == ((s as Panel).Left + 30) && panel[j].Top == ((s as Panel).Top - 30) && panel[j].BackColor == Color.Black) || (panel[j].Left == ((s as Panel).Left + 35) && panel[j].Top == (s as Panel).Top && panel[j].BackColor == Color.Black) || (panel[j].Left == ((s as Panel).Left + 30) && panel[j].Top == ((s as Panel).Top + 5) && panel[j].BackColor == Color.Black)) panel[i + 1].BackColor = Color.Black; } });
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 sticks { public partial class Form1 : Form { Panel[] panel = new Panel[341]; public Form1() { InitializeComponent(); int i = 0, hpos = 0, vpos = 0, width = 0, height = 0; for (int row = 0; row < 21; row++) { hpos = 0; if ((row & 1) == 0) { height = 5; vpos += 30; for (int col = 0; col < 21; col++) { if ((col & 1) == 0) { width = 5; hpos += 30; } else { width = 30; hpos += 5; } panel[i] = new Panel(); panel[i].Name = "panel" + i.ToString(); panel[i].TabIndex = 0; panel[i].Size = new Size(width, height); panel[i].Location = new Point(hpos, vpos); panel[i].BackColor = SystemColors.ControlLight; if ((col & 1) != 0) { panel[i].MouseEnter += new EventHandler((s, args) => { if ((s as Panel).BackColor != Color.Black) (s as Panel).BackColor = SystemColors.ActiveBorder; }); panel[i].MouseLeave += new EventHandler((s, args) => { if ((s as Panel).BackColor != Color.Black) (s as Panel).BackColor = SystemColors.ControlLight; }); panel[i].Click += new EventHandler((s, args) => { (s as Panel).BackColor = Color.Black; for (int j = 0; j < panel.Length; j++) { if ((panel[j].Left == ((s as Panel).Left - 5) && panel[j].Top == ((s as Panel).Top - 30) && panel[j].BackColor == Color.Black) || (panel[j].Left == ((s as Panel).Left - 35) && panel[j].Top == (s as Panel).Top && panel[j].BackColor == Color.Black) || (panel[j].Left == ((s as Panel).Left - 5) && panel[j].Top == ((s as Panel).Top + 5) && panel[j].BackColor == Color.Black)) panel[i - 1].BackColor = Color.Black; if ((panel[j].Left == ((s as Panel).Left + 30) && panel[j].Top == ((s as Panel).Top - 30) && panel[j].BackColor == Color.Black) || (panel[j].Left == ((s as Panel).Left + 35) && panel[j].Top == (s as Panel).Top && panel[j].BackColor == Color.Black) || (panel[j].Left == ((s as Panel).Left + 30) && panel[j].Top == ((s as Panel).Top + 5) && panel[j].BackColor == Color.Black)) panel[i + 1].BackColor = Color.Black; } }); } Controls.Add(panel[i]); if (i < panel.Length - 1) i++; else break; } } else { height = 30; vpos += 5; for (int col = 0; col < 11; col++) { width = 5; hpos += 35; panel[i] = new Panel(); panel[i].Name = "panel" + i.ToString(); panel[i].TabIndex = 0; panel[i].Size = new Size(width, height); panel[i].Location = new Point(hpos - 5, vpos); panel[i].BackColor = SystemColors.ControlLight; panel[i].MouseEnter += new EventHandler((s, args) => { if ((s as Panel).BackColor != Color.Black) (s as Panel).BackColor = SystemColors.ActiveBorder; }); panel[i].MouseLeave += new EventHandler((s, args) => { if ((s as Panel).BackColor != Color.Black) (s as Panel).BackColor = SystemColors.ControlLight; }); panel[i].Click += new EventHandler((s, args) => { (s as Panel).BackColor = Color.Black; for (int j = 0; j < panel.Length; j++) { if ((panel[j].Left == (s as Panel).Left && panel[j].Top == ((s as Panel).Top - 35) && panel[j].BackColor == Color.Black) || (panel[j].Left == ((s as Panel).Left - 30) && panel[j].Top == ((s as Panel).Top - 5) && panel[j].BackColor == Color.Black) || (panel[j].Left == ((s as Panel).Left + 35) && panel[j].Top == ((s as Panel).Top - 5) && panel[j].BackColor == Color.Black)) panel[i - 1].BackColor = Color.Black; if ((panel[j].Left == ((s as Panel).Left - 30) && panel[j].Top == ((s as Panel).Top + 30) && panel[j].BackColor == Color.Black) || (panel[j].Left == ((s as Panel).Left + 35) && panel[j].Top == ((s as Panel).Top + 30) && panel[j].BackColor == Color.Black) || (panel[j].Left == (s as Panel).Left && panel[j].Top == ((s as Panel).Top + 35) && panel[j].BackColor == Color.Black)) panel[i + 1].BackColor = Color.Black; } }); Controls.Add(panel[i]); } } } } } }
Решение задачи: «Динамическое создание компонентов и подписка их на события 2»
textual
Листинг программы
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 sticks { public partial class Form1 : Form { Panel[] panel = new Panel[341]; void connect(object s) { if ((s as Panel).Height == 5) { foreach (Control control in Controls) { Panel p = control as Panel; if (p != null) { if ((p.Left == ((s as Panel).Left - 5) && p.Top == ((s as Panel).Top - 30) && p.BackColor == Color.Black) || (p.Left == ((s as Panel).Left - 35) && p.Top == (s as Panel).Top && p.BackColor == Color.Black) || (p.Left == ((s as Panel).Left - 5) && p.Top == ((s as Panel).Top + 5) && p.BackColor == Color.Black)) { panel[(s as Panel).TabIndex - 1].BackColor = Color.Black; } if ((p.Left == ((s as Panel).Left + 30) && p.Top == ((s as Panel).Top - 30) && p.BackColor == Color.Black) || (p.Left == ((s as Panel).Left + 35) && p.Top == (s as Panel).Top && p.BackColor == Color.Black) || (p.Left == ((s as Panel).Left + 30) && p.Top == ((s as Panel).Top + 5) && p.BackColor == Color.Black)) { panel[(s as Panel).TabIndex + 1].BackColor = Color.Black; } } } } else { int p1 = 0, p2 = 0; foreach (Control control in Controls) { Panel p = control as Panel; if (p != null) { if (p.Left == (s as Panel).Left && p.Top == ((s as Panel).Top - 5)) { p1 = p.TabIndex; } if (p.Left == (s as Panel).Left && p.Top == ((s as Panel).Top + 30)) { p2 = p.TabIndex; } } } foreach (Control control in Controls) { Panel p = control as Panel; if (p != null) { if ((p.Left == (s as Panel).Left && p.Top == ((s as Panel).Top - 35) && p.BackColor == Color.Black) || (p.Left == ((s as Panel).Left - 30) && p.Top == ((s as Panel).Top - 5) && p.BackColor == Color.Black) || (p.Left == ((s as Panel).Left + 5) && p.Top == ((s as Panel).Top - 5) && p.BackColor == Color.Black)) { panel[p1].BackColor = Color.Black; } if ((p.Left == ((s as Panel).Left - 30) && p.Top == ((s as Panel).Top + 30) && p.BackColor == Color.Black) || (p.Left == ((s as Panel).Left + 5) && p.Top == ((s as Panel).Top + 30) && p.BackColor == Color.Black) || (p.Left == (s as Panel).Left && p.Top == ((s as Panel).Top + 35) && p.BackColor == Color.Black)) { panel[p2].BackColor = Color.Black; } } } } } public Form1() { InitializeComponent(); int i = 0, hpos = 0, vpos = 0, width = 0, height = 0; for (int row = 0; row < 21; row++) { hpos = 0; if ((row & 1) == 0) { height = 5; vpos += 30; for (int col = 0; col < 21; col++) { if ((col & 1) == 0) { width = 5; hpos += 30; } else { width = 30; hpos += 5; } panel[i] = new Panel(); panel[i].Name = "panel" + i.ToString(); panel[i].TabIndex = i; panel[i].Size = new Size(width, height); panel[i].Location = new Point(hpos, vpos); panel[i].BackColor = SystemColors.ControlLight; if ((col & 1) != 0) { panel[i].MouseEnter += new EventHandler((s, args) => { if ((s as Panel).BackColor != Color.Black) (s as Panel).BackColor = SystemColors.ActiveBorder; }); panel[i].MouseLeave += new EventHandler((s, args) => { if ((s as Panel).BackColor != Color.Black) (s as Panel).BackColor = SystemColors.ControlLight; }); panel[i].Click += new EventHandler((s, args) => { (s as Panel).BackColor = Color.Black; connect(s); }); } Controls.Add(panel[i]); if (i < panel.Length - 1) i++; else break; } } else { height = 30; vpos += 5; for (int col = 0; col < 11; col++) { width = 5; hpos += 35; panel[i] = new Panel(); panel[i].Name = "panel" + i.ToString(); panel[i].TabIndex = i; panel[i].Size = new Size(width, height); panel[i].Location = new Point(hpos - 5, vpos); panel[i].BackColor = SystemColors.ControlLight; panel[i].MouseEnter += new EventHandler((s, args) => { if ((s as Panel).BackColor != Color.Black) (s as Panel).BackColor = SystemColors.ActiveBorder; }); panel[i].MouseLeave += new EventHandler((s, args) => { if ((s as Panel).BackColor != Color.Black) (s as Panel).BackColor = SystemColors.ControlLight; }); panel[i].Click += new EventHandler((s, args) => { (s as Panel).BackColor = Color.Black; connect(s); }); Controls.Add(panel[i]); i++; } } } } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д