I have a calciulator. it works fine example enter 10-5 in textbox1 and result shown on textbox2. but i want to calculate more. example 8-5*3-1 like this. or 7-2+3 and so on.
how it will be ?
here is my code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
float ricxvi1, pasuxi;
int datvla;
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Clear();
textBox1.Text = textBox1.Text + 7;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox1.Text = textBox1.Text + 0;
textBox2.Clear();
textBox2.Text = textBox2.Text + 0;
datvla = 0;
}
private void button6_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Clear();
textBox1.Text = textBox1.Text + 4;
}
private void button17_Click(object sender, EventArgs e)
{
ricxvi1 = ricxvi1 = float.Parse(textBox1.Text);
datvla = 1;
textBox1.Text += "-";
}
private void button10_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Clear();
textBox1.Text = textBox1.Text + 1;
}
private void button11_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Clear();
textBox1.Text = textBox1.Text + 2;
}
private void button12_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Clear();
textBox1.Text = textBox1.Text + 3;
}
private void mimateba_Click_1(object sender, EventArgs e)
{
ricxvi1 = float.Parse(textBox1.Text);
datvla = 2;
textBox1.Text += "+";
}
private void button7_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Clear();
textBox1.Text = textBox1.Text + 5;
}
private void button8_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Clear();
textBox1.Text = textBox1.Text + 6;
}
private void button5_Click(object sender, EventArgs e)
{
ricxvi1 = float.Parse(textBox1.Text);
datvla = 3;
textBox1.Text += "*";
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Clear();
textBox1.Text = textBox1.Text + 8;
}
private void button4_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Clear();
textBox1.Text = textBox1.Text + 9;
}
private void button9_Click(object sender, EventArgs e)
{
ricxvi1 = float.Parse(textBox1.Text);
datvla = 4;
textBox1.Text += "/";
}
private void button14_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 0;
}
private void button16_Click_1(object sender, EventArgs e)
{
switch (datvla)
{
case 1:
pasuxi = ricxvi1 - float.Parse(textBox1.Text.Substring(textBox1.Text.Length - 1));
textBox2.Text = pasuxi.ToString();
break;
case 2:
pasuxi = ricxvi1 + float.Parse(textBox1.Text.Substring(textBox1.Text.Length - 1));
textBox2.Text = pasuxi.ToString();
break;
case 3:
pasuxi = ricxvi1 * float.Parse(textBox1.Text.Substring(textBox1.Text.Length - 1));
textBox2.Text = pasuxi.ToString();
break;
case 4:
pasuxi = ricxvi1 / float.Parse(textBox1.Text.Substring(textBox1.Text.Length - 1));
textBox2.Text = pasuxi.ToString();
break;
default:
break;
}
}
private void button15_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Clear();
textBox1.Text = textBox1.Text + ".";
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
You have to build a parser to understand the calculations done. Regex or manually parsing is very hard, probably harder than using a parser.
A good parser is ANTLR, which also has a sample grammar for a calculator.
You'll most likely want to create a method which takes in as many decimal values as are entered. Such as my example below, with this you can pass in the entered digits to the method. Then inside the method call calculation methods based on the make up. My point is that you don't want business or data logic handled in the UI and as you have a common task of checking, clearing and assigning to the textbox go for a method that'll handle that for you.Multiple input method
Related
'
public partial class ComPortForm : Form
{
//Port Numaralarını ports isimli diziye atıyoruz.
string[] ports = SerialPort.GetPortNames();
public ComPortForm(SerialPort SP)
{
InitializeComponent();
}
private void ComPortForm_Load(object sender, EventArgs e)
{
foreach (string port in ports)
{
// Port isimlerini combobox1'de gösteriyoruz.
comboBox1.Items.Add(port);
comboBox1.SelectedIndex = 0;
}
// Baudrate'leri kendimiz combobox2'ye giriyoruz.
comboBox2.Items.Add("2400");
comboBox2.Items.Add("4800");
comboBox2.Items.Add("9600");
comboBox2.Items.Add("19200");
comboBox2.Items.Add("115200");
comboBox2.SelectedIndex = 2;
//Bu esnada bağlantı yok.
label1.Text = "Bağlantı Kapalı";
}
private void ComPortForm_Load(object sender, FormClosingEventArgs e)
{
// Form kapandığında Seri Port Kapatılmış Olacak.
if (Program.serial.IsOpen == true)
{
Program.serial.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
if (Program.serial.IsOpen == false)
{
if (comboBox1.Text == "")
return;
// combobox1'e zaten port isimlerini aktarmıştık.
Program.serial.PortName = comboBox1.Text;
//Seri Haberleşme baudrate'i combobox2 'de seçilene göre belirliyoruz.
Program.serial.BaudRate = Convert.ToInt16(comboBox2.Text);
try
{
//Haberleşme için port açılıyor
Program.serial.Open();
label1.ForeColor = Color.Green;
label1.Text = "Bağlantı Açık.";
}
catch (Exception hata)
{
MessageBox.Show("Hata:" + hata.Message);
}
}
else
{
label1.Text = "Bağlantı Zaten Açık!";
}
}
private void button2_Click(object sender, EventArgs e)
{
//BAĞLANTIYI KES BUTONU
timer1.Stop();
if (Program.serial.IsOpen == true)
{
Program.serial.Close();
label1.BackColor = Color.Transparent;
label1.ForeColor = Color.Red;
label1.Text = "Bağlantı Kapalı.";
}
}
private void button3_Click(object sender, EventArgs e)
{
//kaydet butonuyla Form2'yi kapatıyoruz.
this.Close();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
//resim kutusuna dokununca url ye gidecek.
System.Diagnostics.Process.Start("explorer.exe", #"https://hosseven.com.tr/");
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
'
My application for which I selected the Serial Port settings is currently running, but I want to save these settings and use them when my application starts again.I want to keep my chosen settings between application starts? Do I need to save to json file? please edit my code and help.I'm new at this job.
I'm new in c#, I want to make a small translator with some words.
private void button1_Click(object sender, EventArgs e)
{
string i;
i = textBox1.Text;
if (textBox1.Text == bonjour) ;
{
label1.Text = "Hello";
}
if (textBox1.Text == Hello) ;
{
label1.Text = "bonjour";
}
}
But label always "bonjour". Where did I go wrong?
This works with some changes.
string i;
i = textBox1.Text;
if (textBox1.Text == "bonjour") //Remove the ";" and put quotes around string
{
label1.Text = "Hello";
}
if (textBox1.Text == "Hello")
{
label1.Text = "bonjour";
}
I would also suggest, if case does not matter, the following:
string i;
i = textBox1.Text;
if (textBox1.Text.ToLower() == "bonjour")
{
label1.Text = "Hello";
}
if (textBox1.Text.ToLower() == "hello")
{
label1.Text = "bonjour";
}
private void button1_Click(object sender, EventArgs e)
{
string i;
i = textBox1.Text;
if (textBox1.Text == "bonjour")
{
label1.Text = "Hello";
}
if (textBox1.Text == "Hello")
{
label1.Text = "bonjour";
}
}
You don't want semicolons at the end of tests.
Also, you need double quotes "" around the strings you're testing for.
With the way you've set this up, you could also do this:
private void button1_Click(object sender, EventArgs e)
{
string i;
i = textBox1.Text;
if (i == "bonjour")
{
label1.Text = "Hello";
}
if (i == "Hello")
{
label1.Text = "bonjour";
}
}
Furthermore, you have no way of testing case, so use .ToLower(), as suggested by Matt Cullinan.
private void button1_Click(object sender, EventArgs e)
{
string i;
i = textBox1.Text;
if(textBox1.Text == "bonjour");
{
label1.Text = "Hello";
}
else if(textBox1.Text == "Hello");
{
label1.Text = "bonjour";
}
}
My goal is when the checkbox is unchecked to have label1 return to it's value before the checkbox was checked.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
{
if
(checkBox1.Checked)
label1.Text = textBox1.Text + label1.Text;
else
label1.Text = label1.Text; //return to previous value?
}
}
The way mentioned above, label1 'sticks' to the value when the checkbox is unchecked.
I tried using the label1.Refresh but it didn't work.
Is there a more effective way to get the result I'm looking for?
Save the value before updating it. Then revert when unchecked.
private string _lastValue = string.Empty;
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
{
if
(checkBox1.Checked)
{
_lastValue = label1.Text;
label1.Text = textBox1.Text + label1.Text;
}
else
label1.Text = _lastValue;
}
}
Simply use a variable to store the previous value of the label.
string DefaultLabelValue="";
private void Form1_Load(object sender, EventArgs e)
{
DefaultLabelValue=lable1.Text;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
label1.Text = textBox1.Text + label1.Text;
else
label1.Text = DefautLabelValue;
}
I am new to c#. I built a simple calculator. But it does not have keyboard input. How to enable it?
I want to get 5 in the text box when number 5 s pressed from keyboard.
Also i want to hide mouse cursor in text field. Now when the application stars, mouse cursor appears in the text field.
This is my code:
namespace MyCalculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//textBox1.Text = "0";
}
double num1=0, num2, result;
string op;
private void button_plus_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = String.Empty;
op = "+";
}
private void button_minus_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = String.Empty;
op = "-";
}
private void button_mul_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = String.Empty;
op = "*";
}
private void button_div_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = String.Empty;
op = "/";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "1";
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "2";
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "3";
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "4";
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "5";
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "6";
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "7";
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "8";
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "9";
}
private void button0_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "0";
}
private void button00_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "00";
}
private void button_point_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + ".";
}
private void button_clear_Click(object sender, EventArgs e)
{
textBox1.Text = String.Empty;
}
private void button_result_Click(object sender, EventArgs e)
{
calculate(op);
}
public void calculate( string op)
{
num2 = Convert.ToDouble(textBox1.Text);
switch(op)
{
case "+" : result=num1+num2;
textBox1.Text = result.ToString(); break;
case "-": result = num1 - num2;
textBox1.Text = result.ToString(); break;
case "*": result = num1 * num2;
textBox1.Text = result.ToString(); break;
case "/": result = num1 / num2;
textBox1.Text = result.ToString(); break;
}
num1 = 0; num2 = 0;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Font = new Font("Arial",12, FontStyle.Bold);
textBox1.Cursor = Cursors.Arrow;
}
}
}
To capture keyboard KeyDown event you have to first enable KeyPreview of that form. Select the form and go to properties and set KeyPreview = true.
Use KeyDown event to capture keyboard events,
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.Equals(Keys.NumPad5))
{
//Assuming button5 will set the value 5
button5.PerformClick();
}
}
Hiding Cursor
I assume you are expecting the same behaviour as Windows calculator. Rather than hiding the cursor simply you can disable the textbox. Set the textbox Enabled = false.
I am having a very annoying issue that I have been looking everywhere to find but none of them make sense to me. I have only recently started C# so if its a silly mistake, well sorry.
Ive built a calculator and I can successfully make it but I want it to show the operations as the user clicks them. For example when the user clicks on the 6 button of course it shows 6 in the textfield then when he presses the plus(+) button, it should display [6 + ] and then he presses 5 for example and it looks like this in the textfield [6 + 5].
Now here's my error. I can make all the above work but when i click the equals(=) button, I get an error. It says
"Input string was not in correct format."
It says the error is on this line of code:
decimal total = Convert.ToDecimal(LCD.Tag) +
Convert.ToDecimal(LCD.Text);
Heres my code:
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 WindowsFormsApplication1
{
public partial class Window : Form
{
bool pluss = false;
bool minuss = false;
bool multiplyy = false;
bool dividee = false;
public Window()
{
InitializeComponent();
}
private void clear_Click(object sender, EventArgs e)
{
LCD.Text = "";
}
private void dec_Click(object sender, EventArgs e)
{
if (LCD.Text.Contains("."))
{
return;
}
else {
LCD.Text = LCD.Text + ".";
}
}
private void zero_Click(object sender, EventArgs e)
{
LCD.Text = LCD.Text + "0";
}
private void one_Click(object sender, EventArgs e)
{
LCD.Text = LCD.Text + "1";
}
private void two_Click(object sender, EventArgs e)
{
LCD.Text = LCD.Text + "2";
}
private void three_Click(object sender, EventArgs e)
{
LCD.Text = LCD.Text + "3";
}
private void four_Click(object sender, EventArgs e)
{
LCD.Text = LCD.Text + "4";
}
private void five_Click(object sender, EventArgs e)
{
LCD.Text = LCD.Text + "5";
}
private void six_Click(object sender, EventArgs e)
{
LCD.Text = LCD.Text + "6";
}
private void seven_Click(object sender, EventArgs e)
{
LCD.Text = LCD.Text + "7";
}
private void eight_Click(object sender, EventArgs e)
{
LCD.Text = LCD.Text + "8";
}
private void nine_Click(object sender, EventArgs e)
{
LCD.Text = LCD.Text + "9";
}
private void plus_Click(object sender, EventArgs e)
{
if (LCD.Text == "")
{
return;
}else{
pluss = true;
LCD.Tag = LCD.Text;
LCD.Text = LCD.Text + " + ";
}
}
private void equal_Click(object sender, EventArgs e)
{
decimal total = Convert.ToDecimal(LCD.Tag) + Convert.ToDecimal(LCD.Text);
LCD.Text = total.ToString();
}
}
}
I am awaiting someone's response and I'll be so greatful if I get a fix.
Thanks.
When your program crashes:
Break into the debugger.
Inspect the value of LCD.Tag and LCD.Text.
Viola, you will surely notice something awry in the format.
I realize this is a practice project, but this kind of string manipulation back and forth is not the best way to build a calculator. Better to separate the display from the data structures used to contain expression trees and values (i.e., the data structures used to do the actual calculation).
You're trying to convert a string that contains the "+" symbol to decimal format.
Place some breakpoints in your application and inspect where it crashes, the value will probably not be what you expect it to be.
Take a look at what you're giving to Convert.ToDecimal to convert (LCD.Text). It expects a number in string format but you're passing it something like "1 + 2" which is not. You must evaluate the expression yourself.