I am using Visual C++ 2010 to make my very first project - A Calculator(Following an online tutorial).
Now, the calculator is done and functioning, however, the only way to input the numbers is by pressing the buttons with a mouse.
I want to be able to do so with my keyboard. How can I do it?
double iFirstNumber;
double iSecondNumber;
double iResult;
String^ iOperator;
private: System::Void btnC_Click(System::Object^ sender, System::EventArgs^ e) {
//button C
txtDisplay->Text="0";
lblShowOp->Text ="";
}
private: System::Void btnCE_Click(System::Object^ sender, System::EventArgs^ e) {
//button CE
txtDisplay->Text="0";
}
private: System::Void button_click(System::Object^ sender, System::EventArgs^ e) {
//Numeric Buttons
Button ^ Numbers = safe_cast<Button^>(sender);
if(txtDisplay->Text =="0")
{
txtDisplay->Text = Numbers->Text ;
}
else
{
txtDisplay->Text = txtDisplay->Text + Numbers->Text;
}
}
private: System::Void button14_Click(System::Object^ sender, System::EventArgs^ e) {
//Equals
lblShowOp->Text = "";
iSecondNumber = Double::Parse(txtDisplay->Text);
if(iOperator == "+")
{
iResult = iFirstNumber + iSecondNumber;
txtDisplay->Text = System::Convert::ToString(iResult);
}
else if(iOperator == "-")
{
iResult = iFirstNumber - iSecondNumber;
txtDisplay->Text = System::Convert::ToString(iResult);
}
else if(iOperator == "x")
{
iResult = iFirstNumber * iSecondNumber;
txtDisplay->Text = System::Convert::ToString(iResult);
}
else if(iOperator == "÷")
{
iResult = iFirstNumber / iSecondNumber;
txtDisplay->Text = System::Convert::ToString(iResult);
}
if(iOperator == "x²")
{
iResult = iFirstNumber * iFirstNumber;
txtDisplay->Text = System::Convert::ToString(iResult);
lblShowOp->Text= System::Convert::ToString(iFirstNumber) + " " + "²";
}
}
private: System::Void btnBack_Click(System::Object^ sender, System::EventArgs^ e) {
if(txtDisplay->Text->Length>0)
{
txtDisplay->Text = txtDisplay->Text->Remove(txtDisplay->Text->Length -1,1);
}
}
private: System::Void txtDisplay_TextChanged(System::Object^ sender, System::EventArgs^ e) {
if(txtDisplay->Text == "")
{
txtDisplay->Text = "0";
}
}
private: System::Void btnDot_Click(System::Object^ sender, System::EventArgs^ e) {
if(!txtDisplay->Text->Contains("."))
{
txtDisplay->Text = txtDisplay->Text + ".";
}
}
private: System::Void btnPM_Click(System::Object^ sender, System::EventArgs^ e) {
if(txtDisplay->Text->Contains("-"))
{
txtDisplay->Text->Remove(0,1);
}
if(!txtDisplay->Text->Contains("-"))
{
txtDisplay->Text = "-" + txtDisplay->Text;
}
}
private: System::Void operators2(System::Object^ sender, System::EventArgs^ e) {
Button ^ op = safe_cast<Button^>(sender);
iFirstNumber = Double::Parse(txtDisplay->Text);
txtDisplay->Text ="";
iOperator = op->Text;
if(iOperator == "+" || iOperator == "-" || iOperator == "x" || iOperator == "÷")
{
lblShowOp->Text= System::Convert::ToString(iFirstNumber) + " " + iOperator;
}
else if(iOperator == "x²")
{
lblShowOp->Text = System::Convert::ToString(iFirstNumber) + " " + "²";
}
}
};
}
This is my code so far.
By the code that you post, it seems that you're using C++/CLI and WinForms, then System::Windows::Forms::Form has a KeyDown event. Take a look: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx (inside the page, change to C++ language)
I hope that it help you.