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.
Related
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'm trying to get the value of radio button selection to my listbox, but the listbox always gets the same value even though selection was different. Please help, I am a newbie to coding, I couldn't find any answers on the net either..
Here are the codes I've written:
void rdbtnOne_CheckedChanged(object sender, EventArgs e)
{
if (rdbtnOne.Checked == true)
{
rdbtnOne.Text = "Men";
}
else
{
rdbtnOne.Text = "Women";
}
}
void btnOne_Click(object sender, EventArgs e)
{
lstOne.Items.Add(i + rdbtnOne.Text);
i++;
}
Ok, I have found the solution, FINALLY. The reason that my code did not work in the first place was because I tried to give value by equaling rdbtnOne.Text directly. Instead I created another value to equal it. All right here is how it worked for me:
string MenOrWomen;
void rdbtnTwo_CheckedChanged(object sender, EventArgs e)
{
if (rdbtnTwo.Checked.Equals(true))
{
MenOrWomen = "Women";
}
else
{
MenOrWomen = "Men";
}
}
void rdbtnOne_CheckedChanged(object sender, EventArgs e)
{
if (rdbtnOne.Checked.Equals(true))
{
MenOrWomen = "Men";
}
else
{
MenOrWomen = "Women";
}
}
int i = 1;
void btnOne_Click(object sender, EventArgs e)
{
lstOne.Items.Add(i + MenOrWomen);
i++;
}
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 got an error which i cant fix:
Error 1 'System.Windows.Forms.Label' does not contain a definition for 'Copy'
and no extension method 'Copy' accepting a first argument of
type'System.Windows.Forms.Label'
could be found (are you missing a using directive or an assembly reference?)
//path 156 22 FileWatcherEigen
That was my error. Can someone help me and explain to me what went wrong?
This is my code:
using System;
using System.IO;
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 Form1 : Form
{
private bool pause = false;
private bool cut1 = false;
private bool copy1 = false;
public Form1()
{
InitializeComponent();
}
// The lines with performed actions of a file
private void fileSystemWatcher1_Created(object sender,System.IO.FileSystemEventArgs e)
{
if (!pause)
{
listBox1.Items.Add("File Created> " + e.FullPath + " -Date:" + DateTime.Now);
}
}
private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
{
if (!pause)
{
listBox1.Items.Add("File Changed> " + e.FullPath + " -Date:" + DateTime.Now);
}
}
private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
{
if (!pause)
{
listBox1.Items.Add("File Deleted> " + e.FullPath + " -Date:" + DateTime.Now);
}
}
private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)
{
if (!pause)
{
listBox1.Items.Add("File Renamed> " + e.FullPath + " -Date:" + DateTime.Now);
}
}
private void fileSystemWatcher2_Changed(object sender, System.IO.FileSystemEventArgs e)
{
if (!pause)
{
listBox1.Items.Add("File Changed> " + e.FullPath + " -Date:" + DateTime.Now);
}
}
private void fileSystemWatcher2_Created(object sender, System.IO.FileSystemEventArgs e)
{
if (!pause)
{
listBox1.Items.Add("File Created> " + e.FullPath + " -Date:" + DateTime.Now);
}
}
private void fileSystemWatcher2_Deleted(object sender, System.IO.FileSystemEventArgs e)
{
if (!pause)
{
listBox1.Items.Add("File Deleted> " + e.FullPath + " -Date:" + DateTime.Now);
}
}
private void fileSystemWatcher2_Renamed(object sender, System.IO.RenamedEventArgs e)
{
if (!pause)
{
listBox1.Items.Add("File Renamed> " + e.FullPath + " -Date:" + DateTime.Now);
}
}
//1st directory
private void button2_Click(object sender, EventArgs e)
{
if (dlgOpenDir.ShowDialog() == DialogResult.OK)
{
fileSystemWatcher1.EnableRaisingEvents = false; // Stop watching
fileSystemWatcher1.IncludeSubdirectories = true;
fileSystemWatcher1.Path = dlgOpenDir.SelectedPath;
textBox1.Text = dlgOpenDir.SelectedPath; // Text of textBox2 = Path of fileSystemWatcher2
fileSystemWatcher1.EnableRaisingEvents = true; // Begin watching
}
}
//2nd directory
private void button3_Click(object sender, EventArgs e)
{
if (dlgOpenDir.ShowDialog() == DialogResult.OK)
{
fileSystemWatcher2.EnableRaisingEvents = false; // Stop watching
fileSystemWatcher2.IncludeSubdirectories = true;
fileSystemWatcher2.Path = dlgOpenDir.SelectedPath;
textBox2.Text = dlgOpenDir.SelectedPath; // Text of textBox2 = Path of fileSystemWatcher2
fileSystemWatcher2.EnableRaisingEvents = true; // Begin watching
}
}
//log
private void button1_Click(object sender, EventArgs e)
{
DialogResult resDialog = dlgSaveFile.ShowDialog();
if (resDialog.ToString() == "OK")
{
FileInfo fi = new FileInfo(dlgSaveFile.FileName);
StreamWriter sw = fi.CreateText();
foreach (string sItem in listBox1.Items)
{
sw.WriteLine(sItem);
}
sw.Close();
}
}
//pause watching
private void pause_button_Click(object sender, EventArgs e)
{
if (!pause)
{
pause = true;
pause_button.Text = "Unpause";
}
else
{
pause = false;
pause_button.Text = "Pause Watching";
}
}
//clear listbox
private void clear_button_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
}
private void Transfer_Click(object sender, EventArgs e)
{
if (copy1)
{
File.Copy(FileBrowseBox.Text, Path.Combine(DestinationBox.Text, Path.ChangeExtension(FileNameBox.Text, Path.GetExtension(FileBrowseBox.Text))));
}
}
private void Browse_file_Click(object sender, EventArgs e)
{
DialogResult resDialog = openFileDialog1.ShowDialog();
if (resDialog == DialogResult.OK)
{
FileBrowseBox.Text = openFileDialog1.FileName;
}
}
private void Browse_destination_Click(object sender, EventArgs e)
{
DialogResult resDialog = folderBrowserDialog1.ShowDialog();
if (resDialog == DialogResult.OK)
{
DestinationBox.Text = folderBrowserDialog1.SelectedPath;
}
}
private void CopyButton_CheckedChanged(object sender, EventArgs e)
{
copy1 = true;
}
}
}
It says the problem is within this part:
File.Copy(FileBrowseBox.Text, Path.Combine(DestinationBox.Text, Path.ChangeExtension(FileNameBox.Text, Path.GetExtension(FileBrowseBox.Text))));
I have tried to find it on this forum but i couldn't really find the answer or solution
It does work with this code:
using System;
using System.IO;
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 Form1 : Form
{
private bool cut = false;
private bool copy = false;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
File.Copy(FileBrowseBox.Text, Path.Combine(DestinationBox.Text, Path.ChangeExtension(FileBox.Text, Path.GetExtension(FileBrowseBox.Text))));
label2.Text = "File Transfer Succeeded";
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult resDialog = openFileDialog1.ShowDialog();
if (resDialog == DialogResult.OK)
{
FileBrowseBox.Text = openFileDialog1.FileName;
label2.Text = "";
}
}
private void button3_Click(object sender, EventArgs e)
{
DialogResult resDialog = folderBrowserDialog1.ShowDialog();
if (resDialog == DialogResult.OK)
{
DestinationBox.Text = folderBrowserDialog1.SelectedPath;
label2.Text = "";
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
copy = true;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
cut = true;
}
}
}
You're getting this error because you have a label named File on your form that's being referenced rather than System.IO.File. You can either rename the Label, which I'd recommend, or you can use the fully qualified path of System.IO.File.Copy instead.
Apparently you have a Label named File. It shadows the System.IO.File class and causes the error. Specifying full class name should eliminate the problem:
System.IO.File.Copy(...
Ok I got this annoying error today and “a” DID contain a definition for “b”.
I got into this mess after opening the solution in VS2012 and then opening it VS2010.
Turns out by deleting the reference DLL in the affected project, building the reference DLL project, then re-referencing allowed VS to see the definition.
Just to note. I have got similar error while debugging, after I have renamed property in the class. Checked everything, even searched for old property name witch ctrl+shift+f5 in all solution. Nothing found...
After a while I noticed existing breakpoint with 'When hit...' condition to output value of the old property.
There is one other simple case where this will happen. Lets say you add a tool such as a "Button" to your form. Once you double click the tool,(in Visual Studio) the code private void button1_Click(object sender, EventArgs e) { } will be created in your Form1.cs. The code this.button1.Click += new System.EventHandler(this.button1_Click); will also be created in your Form1.Designer.cs. If you deleted the first set of code(for what ever reason) then you will also need to delete the second bit of code or else you will get this error. This may be a specific/simple situation but this is a error that newer programmers might run into.
AHHHHH ok this is driving me nuts.
Why when does my decimal point in the wrong place e.g.
if i have the string 567 in the textbox and click the decimal button i would expect (or i want) the textbox to change to 567. but instead i get .567
It only goes into the correct place when i add another number e.g. if i had the number 4 then straight after doing the above I'd get 567.4
Edit:
Heres my whole 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;
using System.IO;
namespace Calculator
{
public partial class frmCurrencyCalc : Form
{
public frmCurrencyCalc()
{
InitializeComponent();
}
private void cmdZero_Click(object sender, EventArgs e)
{
if (txtScreen.Text == "0")
{
txtScreen.Text = "0";
}
else
{
txtScreen.AppendText("0");
}
}
private void cmd1_Click(object sender, EventArgs e)
{
if (txtScreen.Text == "0")
{
txtScreen.Text = "1";
}
else
{
txtScreen.AppendText("1");
}
}
private void cmdTwo_Click(object sender, EventArgs e)
{
if (txtScreen.Text == "0")
{
txtScreen.Text = "2";
}
else
{
txtScreen.AppendText("2");
}
}
private void cmdThree_Click(object sender, EventArgs e)
{
if (txtScreen.Text == "0")
{
txtScreen.Text = "3";
}
else
{
txtScreen.AppendText("3");
}
}
private void cmdFour_Click(object sender, EventArgs e)
{
if (txtScreen.Text == "0")
{
txtScreen.Text = "4";
}
else
{
txtScreen.AppendText("4");
}
}
private void cmdFive_Click(object sender, EventArgs e)
{
if (txtScreen.Text == "0")
{
txtScreen.Text = "5";
}
else
{
txtScreen.AppendText("5");
}
}
private void cmdSix_Click(object sender, EventArgs e)
{
if (txtScreen.Text == "0")
{
txtScreen.Text = "6";
}
else
{
txtScreen.AppendText("6");
}
}
private void cmdSeven_Click(object sender, EventArgs e)
{
if (txtScreen.Text == "0")
{
txtScreen.Text = "7";
}
else
{
txtScreen.AppendText("7");
}
}
private void cmdEight_Click(object sender, EventArgs e)
{
if (txtScreen.Text == "0")
{
txtScreen.Text = "8";
}
else
{
txtScreen.AppendText("8");
}
}
private void cmdNine_Click(object sender, EventArgs e)
{
if (txtScreen.Text == "0")
{
txtScreen.Text = "9";
}
else
{
txtScreen.AppendText("9");
}
}
private void cmdDecimal_Click(object sender, EventArgs e)
{
txtScreen.AppendText(".");
cmdDecimal.Enabled = false;
}
private void cmdCancel_Click(object sender, EventArgs e)
{
txtScreen.Text = "0";
cmdDecimal.Enabled = true;
}
}
}
The RightToLeft looks to be your problem.
As described in MSDN,
The RightToLeft property is used for
international applications where the
language is written from right to
left, such as Hebrew or Arabic. When
this property is set to
RightToLeft..::.Yes, control elements
that include text are displayed from
right to left.
As one ofthe previous answers suggested, this should be set to false, but with TextAlign set to Right to mimic the appearance of a real calculator.
My advice is -- define a business layer. In your case -- a double variable. Upon button clicks, update the variable first. Then format the value.
My advice is to set TextAlign to Right, but leave RightToLeft set to No.
Edit: Having said that, this issue may be unrelated to these settings.
I remember a friend having this a bug similar to this back in early 2009 in Visual Studio 2008 on Windows Vista. Strangely enough, the same problem did not occur on the same version of Visual Studio on Windows XP.
If you haven't updated Visual Studio / .NET 3.5 to Service Pack 1, I suggest doing that and seeing if it fixes the problem.
Perhaps try a different method:
private void AddDecimal()
{
txtScreen.SelectionLength = txtScreen.TextLength;
txtScreen.SelectedText += ".";
}
(Also is your text box, text aligment, right aligned... if not that may contribute to your problem.)
I think you have a few things here.
By the looks of it, you've set:
txtScreen.right to left = true;
If you append just the decimal point, you get the result you describe. Try using something like:
txtScreen.AppendText(".00");
This will give you the result you are describing.
You could be opening a can of worms. When you start formatting the textbox, you are changing it from holding a value to presentation. Eg:
decimal value = 567;
txtScreen.Text = value.ToString("0.00");
Then you will have to start writing crazy validation rules to avoid values like 567.00.1 etc.
Just to let you all know who are interested.
I managed to fix it somehow. All I did was delete the right to left thing in the design code and then realigned it (using the GUI) to right and its worked...odd as I did nothing different to last time...
Oh well
Thank you all for your help
Much aprreciated
x
You can try this one, it works for me:
private void btndot_Click(object sender, EventArgs e)
{
if (txtbox.Text == "0" && txtbox.Text != null)
{
txtbox.Text = ".";
}
else
{
txtbox.Text = txtbox.Text + ".";
}
}