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

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

Related

Problem to add "," in my windows forms calculator (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 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)).

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

Import txt file data into datagridview 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
I am trying to import data into my datagridview. The code I have so far is:
private void loadButton_Click(object sender, EventArgs e)
{
if (File.Exists(DATA_FILE_NAME))
fileIn = File.OpenText(DATA_FILE_NAME);
else
{
MessageBox.Show(DATA_FILE_NAME + " does not exist", "Abort Execution", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
if (listCountTextBox.ReadInt(out index))
for (int i = 1; i <= index; i++)
idList.Add(Int32.Parse(fileIn.ReadLine())); <----- Error here
fileIn.Close();
DisplayIDList(displayDGV);
}
The format of the data in the text file looks like this: "000-0000" and there are 1,240,000 values. The error says: Input string was not in a correct format.
Any help would be much appreciated!
Thank You!
As the others responds is not posible to convert "000-0000" to int, thats the error.
Would work if you change:
idList.Add(Int32.Parse(fileIn.ReadLine().Replace("-","")));
//idList.Add(Int32.Parse(fileIn.ReadLine())); <----- Error here

Right Approach to Make cart in asp.net [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm making a shopping cart in asp.net with c# using DataTable stored in Session and all items stored in DataTable.
public DataTable dtCart = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["dtCart"] == null) { CreateCartTable(); }
}
}
protected void CreateCartTable()
{
dtCart.Rows.Clear();
dtCart.Columns.Clear();
dtCart.Columns.Add(new DataColumn("Prd_Id", Type.GetType("System.String")));
dtCart.Columns.Add(new DataColumn("Prd_Name", Type.GetType("System.String")));
dtCart.Columns.Add(new DataColumn("Prd_Img", Type.GetType("System.String")));
dtCart.Columns.Add(new DataColumn("Qty", Type.GetType("System.Int32")));
dtCart.Columns.Add(new DataColumn("Rate", Type.GetType("System.Decimal")));
dtCart.Columns.Add(new DataColumn("Amount", Type.GetType("System.Decimal")));
dtCart.Columns.Add(new DataColumn("TotalAmount", Type.GetType("System.Decimal")));
Session["dtCart"] = dtCart;
}
Is this right approach or any other way is more elegant and light on application?
FYI I'm using web forms.
You can make a class to cart:
public class Cart
{
public string Prd_Id{set;get;}
public string Prd_Name{set;get;}
//and so on...
}
then in page:
Cart obj=new Cart{Prd_Id="1",Prd_Name="John"};
Session["dtCart"]=obj;

How to add values per character in c#? [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 8 years ago.
Improve this question
I am new to c# and do not know how to explain this but I wanted to know if it was possible to add a charge for every character that is entered into a text box.
For example if the customer was to enter "HELLO HI" they should be charged £5 with a additional cost of £1 per letter or number this £1 charge should also apply for any spaces entered.
Sorry if I have not explained this properly but this is the best way I can.
Thank you
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int amount;
private void textBox1_TextChanged(object sender, EventArgs e)
{
amount = 5;
amount = amount + textBox1.Text.Length;
label1.Text = amount.ToString();
}
}
}
Well, it pretty much looks like this, I guess:
string str = "Happy Birthday";
int price = str.Length + 5;
Find the length of entered string in textbox using TextboxID.text.Length and add your fix charge that 5 , as ALEX explain you.
please see alex's answer.

Categories