c#


Calculator in C# input from keyboard using “&” in text property


I'm developing a calculator in visual studio 2017. Everything is working fine, but input from keyboard isn't working properly.
I use "&" in text property of a button, and it works, but problem is that it's printing on the screen like "&1 + &2". I attach a code and images so you guys can see what's happening.
1 - result picture
2 - usage of "&" symbol
Thanks in advance,
Best regards,
Ram
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 Calculator
{
public partial class Form1 : Form
{
Double resultado_value = 0; // result is zero in the beginning
String operationPerformed = "";
bool is_pressed = false;
public Form1()
{
InitializeComponent();
}
private void button_click(object sender, EventArgs e)
{
if ((textBox_Result.Text == "0") || (is_pressed))
textBox_Result.Clear();
is_pressed = false;
Button button = (Button)sender;
if (button.Text == ".") //to avoid repetitive dots
{
if(!textBox_Result.Text.Contains("."))
textBox_Result.Text = textBox_Result.Text + button.Text;
}else
textBox_Result.Text = textBox_Result.Text + button.Text;
}
private void operator_click(object sender, EventArgs e)
{
Button button = (Button)sender;
if (resultado_value != 0) //if result value not equal to zero
{
button15.PerformClick();
operationPerformed = button.Text;
labelCurrentOperation.Text = resultado_value + " " + operationPerformed;
is_pressed = true;
}
else
{
operationPerformed = button.Text;
resultado_value = Double.Parse(textBox_Result.Text);
labelCurrentOperation.Text = resultado_value + " " + operationPerformed;
is_pressed = true;
}
}
//Clear entry
private void button4_Click(object sender, EventArgs e)
{
textBox_Result.Text = "0";
}
//button Clear
private void button5_Click(object sender, EventArgs e)
{
// this.BackColor = System.Drawing.Color.White;//can't find color "control"
textBox_Result.Text = "0";
resultado_value = 0;
}
// equal button
private void button15_Click(object sender, EventArgs e)
{
switch (operationPerformed)
{
case "+":
// this.BackColor = System.Drawing.Color.Red;//form change color to red
textBox_Result.Text = (resultado_value + Double.Parse(textBox_Result.Text)).ToString();
break;
case "-":
// this.BackColor = System.Drawing.Color.Aqua;
textBox_Result.Text = (resultado_value - Double.Parse(textBox_Result.Text)).ToString();
break;
case "X":
// this.BackColor = System.Drawing.Color.AliceBlue;
textBox_Result.Text = (resultado_value * Double.Parse(textBox_Result.Text)).ToString();
break;
case "÷":
// this.BackColor = System.Drawing.Color.BlueViolet;
textBox_Result.Text = (resultado_value / Double.Parse(textBox_Result.Text)).ToString();
break;
default:
break;
}
resultado_value = Double.Parse(textBox_Result.Text);
labelCurrentOperation.Text = "";
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void labelCurrentOperation_Click(object sender, EventArgs e)
{
}
}
}
If I understand what you are trying to do correctly then what you want to do is catch keypresses at the Form level. If this is what you want you should set the KeyPreview Property of your Form to true and override the OnKeyPress Method of the Form or add an Event Handler of KeyPressed and assign this to the Forms KeyPressed Event and do your thing there.
If you want me to provide an example let me know.
Ram Pawar I have written a quick example for you.
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Formkeypress
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
KeyPreview = true;
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
if (e.KeyChar == 'r') BackColor = Color.Red;
if (e.KeyChar == 'b') BackColor = Color.Blue;
if (e.KeyChar == 'g') BackColor = Color.Green;
}
}
}
Basicly if you type 'r' here the Form will change its Background color to Red. Typing 'b' will change it to Blue, typing 'g' will change it to Green.
Please note that you have to set KeyPreview to true in the constructor for this to work.
I override the OnKeyPress event here as this is the prefered way to add logic to an event when deriving from a Control or Form. You can however just attach a KeyPress event handler to the Form if you wish with the same code block as the OnKeyPress method.
Also rermove the '&'s from your Text Properties.
Hope this help
Danny

Related Links

cannot convert system.data.datarow to dataset.row
How to generate installation requirements for my program?
How do I link gui components to Unity web player?
Submit POST request from codebehind in ASP.NET [duplicate]
The Microsoft Office Access database engine could not find an object
Search and replace specific cell value in a datagridview
Return multiple values from Stored Procedure in C#
Capture current user custom field and save in database
System.Data.OleDb.OleDbException: Data type mismatch in criteria expression
ODP.NET Managed driver throws an exception while connecting to the Oracle Database
Parallel.Foreach giving error “ Index was outside the bounds of the array ”
how to build flat API with indexer against dictionary backing
How to edit Virtual Hard Drives(vhd) with C#?
Have some troubles when I'm trying to draw 3d function with IlNumerics
ASP.NET MVC4 #Html.ListBoxFor Multiple to DB using EF
Parsing Dynamic Data

Categories

HOME
azure-documentdb
stata
compression
postgresql-9.3
windows-server-2008
android-ndk
gaussian
data-modeling
datetimepicker
artifactory
symfony-sonata
grid
angularjs-directive
country-codes
cdn
pip
phpexcel
normals
oms
aws-sdk-cpp
point-of-sale
guidewire
expo
eclipse-orion
loss
novacode-docx
aiml
aws-devicefarm
android-wifi
git-branch
opengraph
email-client
prepros
pumping-lemma
uipath
waterfall
pydub
angular2-changedetection
mplab
visual-prolog
snapkit
jsonapi-resources
voyager
zimbra
http-proxy
tunnel
ansible-inventory
lighting
motion
mockserver
latency
compiled-query
electron-packager
compiler-warnings
paperclip
nshttpurlresponse
ocpjp
line-endings
runtime-permissions
sharpdevelop
intermediate-language
fckeditor
senti-wordnet
spring-social-twitter
turbolinks
altova
foreground
system-integration
django-celery
database-administration
alchemy
database-project
redquerybuilder
edgar
identifying
litedb
eggplant
autopep8
bungeecord
dockerpy
drools-guvnor
uiblureffect
diffmerge
breeze-sharp
redmine-plugins
pytz
ios-enterprise
rpt
smartxls
winrun4j
opencobol
image-preloader
usersettings
pudb
junction-table
pageload
rbm
idtabs
towerjs
fileoutputstream
clgeocoder
openwrap
xpolog
lightopenid
service-layer
object-tag
iphone-maps
for-xml
pixel-bender
high-load
office-2003
tracd

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App