Элемент "textbox1" не существует в данном контексте - C#
Формулировка задачи:
В чём ошибка, можете сказать?
using System.Drawing;
using System.Windows.Forms;
using System;
class SimpleWindowsForm : Form
{
ListBox listbox1;
public SimpleWindowsForm()
{
this.Size = new Size(400, 400);
PictureBox picturebox1 = new PictureBox();
picturebox1.SizeMode = PictureBoxSizeMode.StretchImage;
Bitmap image1 = new Bitmap("C://Users//Компьютер//Desktop//Автар//Deadpool-Marvel-фэндомы-Deathstroke-1197435.png");
picturebox1.ClientSize = new Size(this.Width, 150);
picturebox1.Image = (Image)image1;
this.Controls.Add(picturebox1);
this.Text = "Калькулятор";
// Окно
Button button1 = new System.Windows.Forms.Button();
button1.Text = "Unknown";
button1.Location = new Point(150, 160);
button1.Size = new Size(100, 30);
button1.Click += new System.EventHandler(button1_Click);
this.Controls.Add(button1);
// 1 кнопка
listbox1 = new System.Windows.Forms.ListBox();
listbox1.Location = new System.Drawing.Point(20, 200);
listbox1.Size = new Size(100, 100);
listbox1.Items.Add("Умножение");
listbox1.Items.Add("Деление");
listbox1.Items.Add("Сложение");
listbox1.Items.Add("Вычитание");
listbox1.SelectedIndex = 2;
this.Controls.Add(listbox1);
// Список
TextBox textbox1 = new TextBox();
textbox1.Multiline = true;
textbox1.Text = "";
textbox1.Location = new Point(250, 200);
textbox1.Width = 50; textbox1.Height = 20;
this.Controls.Add(textbox1);
// 1 число
TextBox textbox2 = new TextBox();
textbox2.Multiline = true;
textbox2.Text = "";
textbox2.Location = new Point(250, 220);
textbox2.Width = 50; textbox1.Height = 20;
this.Controls.Add(textbox2);
}
static void Main()
{
Application.Run(new SimpleWindowsForm());
// Запуск
}
void button1_Click(object a, EventArgs b)
{
string x;
string y;
int v;
int n;
x = textbox1.Text;
y = textbox2.Text;
int TheResultOfActionPlus = v + n;
switch(listbox1.SelectedItem.ToString())
{
case ("Сложение"): MessageBox.Show("Ваш ответ: " + TheResultOfActionPlus); break;
}
}
private void InitializeComponent()
{
this.SuspendLayout();
this.BackColor = System.Drawing.SystemColors.Control;
this.ClientSize = new System.Drawing.Size(293, 273);
this.Name = "123";
this.ResumeLayout(false);
}
}Решение задачи: «Элемент "textbox1" не существует в данном контексте»
textual
Листинг программы
class SimpleWindowsForm : Form
{
ListBox listbox1;
TextBox textbox1;
public SimpleWindowsForm()
{
...
// Список
textbox1 = new TextBox();
textbox1.Multiline = true;
textbox1.Text = "";
textbox1.Location = new Point(250, 200);
textbox1.Width = 50; textbox1.Height = 20;
this.Controls.Add(textbox1);
...