Привет всем.
сегодня хотел сделать нажатие кнопки по нажатию клавиши на клавиатуре, но столкнулся с проблемой которую я не знаю как решать. Надеюсь подскажите как её решить. заранее спасибо
код:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
class MyButtonClass : Form
{
private Button One, Two, Three, Four, Fife, Six, Seven, Eight, Nine, Zero, Plus, Minus, multiply, division, Equally, dot, Delete, clear, copy, past;
private TextBox FirstText, SecondText, ThirdText;
private void InitializeComponent()
{
this.SuspendLayout();
//
// MyButtonClass
//
this.ClientSize = new System.Drawing.Size(280, 525);
this.Name = "MyButtonClass";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.ResumeLayout(false);
}
double n, k, res;
int v, b;
public MyButtonClass()
{
v = 0;
b = 0;
Plus = new Button();
Plus.Text = "+";
Plus.Top = 150;
Plus.Left = 210;
Plus.Height = 100;
Plus.Width = 70;
Plus.Click += new System.EventHandler(PlusButton);
this.Controls.Add(Plus);
//Declare, properties and call a button
FirstText = new TextBox();
FirstText.Text = $"0";
FirstText.Top = 10;
FirstText.Left = 0;
FirstText.Height = 50;
FirstText.Width = 210;
this.Controls.Add(FirstText);
//Declare, properties and call a TextBox
SecondText = new TextBox();
SecondText.Text = $"";
SecondText.Top = 50;
SecondText.Left = 0;
SecondText.Height = 50;
SecondText.Width = 210;
this.Controls.Add(SecondText);
//Declare, properties and call a TextBox
ThirdText = new TextBox();
ThirdText.Text = $"0";
ThirdText.Top = 100;
ThirdText.Left = 0;
ThirdText.Height = 50;
ThirdText.Width = 210;
this.Controls.Add(ThirdText);
//Declare, properties and call a TextBox
InitializeComponent();
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(-5, 0);
}
[STAThread]
static void Main()
{
Application.Run(new MyButtonClass());
//starting objects of class MyButtonClass
}
void PlusButton(object sender, EventArgs e)
{
if (v == 0)
{
SecondText.Text = "+";
v = 1;
}
else if (v == 1)
{
n = double.Parse(FirstText.Text);
k = double.Parse(ThirdText.Text);
if (SecondText.Text == "+")
{
res = n + k;
}
else if (SecondText.Text == "-")
{
res = n - k;
}
else if (SecondText.Text == "x")
{
res = n * k;
}
else if (SecondText.Text == "/")
{
res = n / k;
}
string r = Convert.ToString(res);
FirstText.Text = r;
SecondText.Text = "+";
ThirdText.Text = "0";
v = 1;
}
}
public class FilteredTextBox : TextBox
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (e.KeyChar == '+')
{
Plus.PerformClick();
// здесь ошибка
}
base.OnKeyPress(e);
}
}
}