c# Revert on checkbox unchecked - c#

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;
}

Related

I have a calculator but need to modify it

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

How to delete selected text from textbox in c#(SWF)

I try to do a notepad in c#,
I have some problems at delete function,
I want to delete selected text...
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
int a;
a = textBox1.SelectionLength;
textBox1.Text.Remove(textBox1.SelectionStart,a);
}
what is wrong?
Remove will return the truncated string, so you just need to reassign to the TextBox:
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
int a = textBox1.SelectionLength;
textBox1.Text = textBox1.Text.Remove(textBox1.SelectionStart,a);
}
Use SelectedText like this :
textbox1.SelectedText = "";

Want to add Keyboard input to my calculator. Also hide the mouse cursors?

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.

If statements involving Radiobuttons

I'm currently creating a calculator type form on C#. I have four radiobuttons (Addition, subtraction, multi, and div) and a label in between two textboxes. The label changes according to the selected radiobutton, (for example if I selected the Addition radiobutton the label would read "+"). The problem I'm experiencing with this code:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
label3.Text = ("+");
}
else if (radioButton2.Checked == true)
{
label3.Text = ("-");
}
else if (radioButton3.Checked == true)
{
label3.Text = ("x");
}
else if (radioButton4.Checked == true)
{
label3.Text = ("/");
}
}
is when I select the division button the label does not change unless I go through all the buttons and THEN other radio buttons (such as subtraction), when selected, do not change the label until multiple tries. I tried changing the last line to an "else label3.text=("/");" but it doesn't really change anything other than the order of errors.
Any help would be appreciated! Thanks :)
I think you need to check if the radio button is checked in each individual radioButtonX_CheckedChanged method like so:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
label3.Text = ("+");
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (radioButton2.Checked)
{
label3.Text = ("-");
}
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
if (radioButton3.Checked)
{
label3.Text = ("x");
}
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
if (radioButton4.Checked)
{
label3.Text = ("/");
}
}
Let me know if that helps, and if you are still having the issue.
You may want to change how you check for the Checked button. MrB's solution works, but if you'd like to keep your selection code in a single block (as you have), make sure all your radio buttons have their CheckedChanged event subscribed to something similar to the following:
private void RadioButtonCheckedChanged(object sender, EventArgs e)
{
var radioButton = (RadioButton)sender;
if (radioButton.Checked)
{
switch (radioButton.Text)
{
case "Add":
label3.Text = "+";
break;
case "Subtract":
label3.Text = "-";
break;
case "Divison":
label3.Text = "/";
break;
}
}
}
You can also switch on another property, such as the RadioButton.Tag field, whatever may be meaningful to you.
As far as the actual reason your code is failing, it's hard to understand without ensuring which RadioButton's have their events set properly, and seeing the incorrect results.
protected void Page_Load(object sender, EventArgs e)
{
agm.Visible = RadioButtonList1.SelectedValue == "1" ? true : false;
}

Incorrect input string format in Calculator

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.

Categories

Resources