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

IIS Web Application can't be accessed remotely
re-number a value by 1 if same value
Fix error with route url in Asp.Net MVC 5
Calling AWS Lambda Fucntion from MVC controller
From C#, how do I call Release on a returned object that implements IStorage?
How to rename folder in network shared folder?
GhostScript has problems to open files with space in their name
Convert some local time to UTC in C#
DirectoryEntry.MoveTo() committing changes without calling CommitChanges()
How to use configuration settings in a Universal Windows App
Displaying an item that meets 2 conditions in a SQL database in a Webform label
How does MVC know which controller to call, when they are not annotated? [closed]
Allow Time as Hours and Minutes in a Datagridview column
DacFx: Error loading Microsoft.Data.Tools.Schema.Sql.resources
How to have a fixed line spacing in a textbox?
C# - Is it better to use is and cast or to use as and check for null? [duplicate]

Categories

HOME
autohotkey
macros
netty
ebay-api
lithium
crash
sms
sugarcrm
glympse
psexec
hibernate-mapping
install
jquery-select2
browserstack
yacc
rtc
oxid
cakephp-2.0
dat.gui
jsfiddle
web-parts
single-page-application
cherrypy
lotusscript
ckeditor4.x
libxml2
angular-schema-form
ios9
tikz
kubectl
storefront
postgresql-9.6
epub
geocomplete
intersection
ansible-inventory
campaign-monitor
oracle-bpm-suite
jibx
grails-2.0
ack
spring-webflow-2
serverless-architecture
lucee
dotnet-cli
jcifs
photoshop-script
kendo-scheduler
printer-control-language
codesniffer
rmq
was
robotc
therubyracer
optional-parameters
firebase-polymer
easy68k
x12
qtruby
jde
window.location
firefox-os
dday
github-for-mac
kramdown
producer-consumer
play-json
vanilla-forums
hexagonal-tiles
dpkt
eaaccessory
gmaps4jsf
iphone-6
drools-guvnor
novell
yosemite
agent-based-modeling
msdropdown
rssi
xps
new-operator
mongoid4
node-amqp
nop
facebook-likebox
grunt-wiredep
sygic
ie-developer-tools
raddatepicker
forward-declaration
mandelbrot
wpd
windows-scheduler
nx
teamsystem
reflexil
optimistic-concurrency
visual-leak-detector
clgeocoder
jexcelapi
quazip
mediastreamsource
web-search
zend-acl
disk-based
cross-cutting-concerns
high-load

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