Problem to add "," in my windows forms calculator (c#) [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am just trying to create a calculator with windows forms on visula studio (c#) everythings works great but when I tried to add the comma. I don't have the correct result.
for example if i try to make: 7.5 -4.5 = I've got the answer 30.
Can you help me please i am new in c# I really want to learn this language.
this is the part:
private void button3_Click(object sender, EventArgs e)
{
double fn;
double sn;
double r=0;
//----------------------------------------------------------------
double.TryParse(label_firstNun.Text, out fn);
double.TryParse(label1.Text, out sn);
//------------------------------------
if (label_Operator.Text=="+")
{
r = fn + sn;
}
//-----------------------------------
if (label_Operator.Text == "-")
{
r = fn - sn;
}
//-----------------------------------
if (label_Operator.Text == "*")
{
r = fn * sn;
}
//-----------------------------------
if (label_Operator.Text == "%")
{
r = fn / sn;
}
//-----------------------------------
label1.Text = r.ToString();
label_Operator.Text = "";
label_firstNun.ResetText();
}
private void button1_Click(object sender, EventArgs e)
{
this.label1.Text = this.label1.Text + ".";
}
}
thank you very much for your help.

It sounds like your locale doesn't use . as a decimal separator. Either use the correct decimal separator for your culture, or use another culture for parsing the numbers (e.g. double.TryParse(label_firstNun.Text, NumberStyles.Number, CultureInfo.InvariantCulture, out fn)).

Related

CS8321: The local function 'TextChange' is declared but never used [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
please help me
public Text changingText;
int Cash = '0';
int CashMulti = '1';
int CashPerClick = '1';
public void OnMouseDown()
{
Cash = Cash + CashPerClick * CashMulti;
void TextChange()
{
changingText.text = "Cash";
}
}
The function should be declared outside the other function and then you can call it whenever you want.
public void OnMouseDown()
{
Cash = Cash + CashPerClick * CashMulti;
TextChange();
}
void TextChange()
{
changingText.text = "Cash";
}

Reading a text file with c# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
So far this is my code. The problem I am encountering is that the file is not being found.
namespace Assignment_Forms_Aplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Define Variable
string[] words = new string[10];
// Read the text from the text file, and insert it into the array
StreamReader SR = new StreamReader(#"Library.txt");
//
for (int i = 0; i < 10; i++)
{
words[i] = SR.ReadLine();
}
// Close the text file, so other applications/processes can use it
SR.Close();
}
}
}
Hi Gailen use the following method:
File.ReadAllLines(#"location");
If location is correct then this will work
Assign to variable, for example

C# Using for loops to process multiple elements in the WinForm Environment [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am pretty new to C#, I have been searching for an hour or so now for what I need and can't find it. I am trying to look at the contents of 32 different text box's using for loops.
The code I have at the moment is:
private void btnCalculate_Click(object sender, EventArgs e)
{
string ElementString;
Control ElementControl;
double Num;
Boolean errorMsg = false;
for (int x = 1; x <= 4; x++)
for (int y = 1; y <= 4; y++)
{
ElementString = "txtA" + x.ToString() + y.ToString();
ElementControl = this.Controls[ElementString];
ElementString = ElementControl.Text.Trim();
if (!double.TryParse(ElementString, out Num))
{
errorMsg = true;
break;
}
}
if (errorMsg)
MessageBox.Show("Error Processing Input Matricies, invalid entries");
}
Ok changing this part, sorry for not putting more information, but hopefully this will help.
The program crashes when the button is clicked. It crashes when running the line:
ElementString = ElementControl.Text.Trim();
And gives the error message:
Object reference not set to an instance of an object.
Thank you
The first thing you need to do is check if ElementControl is null. Do that right before you call the line that is throwing the exception.
Another way to do this would be to search for all of the TextBox controls in your container and check them that way. You can use the following to do that:
foreach(Control c in this.Controls)
{
if (c is TextBox)
{
// Do whatever you want to do with your textbox.
}
}
That is much more dynamic than trying to call retrieve the controls by name.
put an if statement around the part where it crashes:
if(ElementControl != null)
{
ElementString = ElementControl.Text.Trim();
}
else
{
//Handle error if element control is null
}

C# read and calculate multiple values from textbox [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a question about creating calculator in C# Windows Form Application.
I want it to be possible write with form buttons an expression in textbox so for example 2+3+7= and after pressing "=" button program will read all digits and signs and perform calculation... I don't know from where to start and how could do it in such a way. Any help to any reference or smth to look at how to start doing such a expressions?
Main thing is how to read, seperate and after calculate values from textbox.
Thanks.
With the Split method you could solve this rather easy.
Try this:
private void button1_Click(object sender, EventArgs e)
{
string[] parts = textBox1.Text.Split('+');
int intSum = 0;
foreach (string item in parts)
{
intSum = intSum + Convert.ToInt32(item);
}
textBox2.Text = intSum.ToString();
}
If you would like to have a more generic calculation, you should look at this post:
In C# is there an eval function?
Where this code snippet would do the thing:
public static double Evaluate(string expression)
{
System.Data.DataTable table = new System.Data.DataTable();
table.Columns.Add("expression", string.Empty.GetType(), expression);
System.Data.DataRow row = table.NewRow();
table.Rows.Add(row);
return double.Parse((string)row["expression"]);
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = Evaluate(textBox1.Text).ToString();
}

Textbox don't allow typing " \ " 2 times after each other [duplicate]

This question already has an answer here:
Don't allow a specific use of characters
(1 answer)
Closed 9 years ago.
I bumped into a problem, i hope someone can help me out :)
i got a textbox, and i want to limit users so that it isn't allowed to have two \ after each other.
i'm using it for folders. for example: C\temp\test\
now i want to make it not possible to type C\temp\test\\
i've tried searching some around for this problem but i couldn't find anyting like this. so i hope it's possible :)
heres a code of my textbox how it is now
private void textBox1_TextChanged(object sender, EventArgs e)
{
try
{
Regex regex = new Regex(#"[^C^D^A^E^H^S^T^]");
MatchCollection matches = regex.Matches(textBox1.Text);
if (matches.Count > 0)
{
MessageBox.Show("Character niet toegestaan!");
textBox1.Text = "";
}
clsOpslagMedium objOpslag; // definieert type object
objOpslag = new clsOpslagMedium(); // creert opject in memory
objOpslag.DriveLetterString = textBox1.Text;
}
catch (Exception variableEx1)
{
MessageBox.Show("Foutmelding: " + variableEx1.Message);
}
}
I hope someone can give some examples and that I provided enough information :)
An easy way would be to simply replace the string.
private void textBox1_TextChanged(object sender, EventArgs e)
{
//get the current cursor position so we can reset it
int start = textBox1.SelectionStart;
textBox1.Text = Regex.Replace(textBox1.Text, #"\\\\+", #"\");
//make sure the cursor does reset to the beginning
textBox1.Select(start, 0);
}
The extra code surrounding the replace ensures the cursor doesn't reset to the start of the textbox (which happens when you set the Text property).
a textreplace isn't working in my case. i need an error shown up when a user leaves the box when he types more then one \
If this is what you really want you need to use a ErrorProvider. Add one to your form then add the following code to the texbox's Validating event and be sure that CausesValidation is true for the textbox
private void textBox1_Validating(object sender, CancelEventArgs e)
{
if(Regex.IsMatch(textBox1.Text, #"\\\\+"))
{
e.Cancel = true;
errorProvider1.SetError(textbox1, #"\\ Is not allowed");
}
else
{
errorProvider1.SetError(textbox1, null);
}
}
This will make a ! show up next to the text box if they type wrong and force them to correct it when they attempt to leave the text box.
You need to find all \-sequences (\\,\\\,\\\\, ...) and replace that on \. You can use regex for search sequences
Sample:
string test=#"c:\\\adas\\dasda\\\\\\\ergreg\\gwege";
Regex regex = new Regex(#"\\*");
MatchCollection matches = regex.Matches(test);
foreach (Match match in matches)
{
if (match.Value!=string.Empty)
test = ReplaceFirst(test, match.Value, #"\");
}

Categories