How to add values per character in c#? [closed] - c#

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.

Related

For cycle always give same output [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 4 months ago.
Improve this question
1. Summarize the problem
The following for cycle keeps on running as i want, but always give me the same clicked button that is "0".
It does not give me an error. But by playing the game i can see that it's always the same number.
2. Describe what you've tried
I've tried searching around the internet for people like me. but sadly i couldn't find anything.
3. Show some code
Code that i'm talking about.
int ButtonNum;
public void Start()
{
for (int i = 0; i < ButtonsPage.Length; i++)
{
ButtonsPage[i].GetComponent<Button>().onClick.AddListener(delegate { ButtonClicked(ButtonNum); });
}
}
public void ButtonClicked(int i)
{
Debug.Log("Clicked" + i);
if (WhichType == "Nose")
{
NoseColor.sprite = NosesColor[i];
NoseOutline.sprite = NosesOutline[i];
}
//ButtonNum will be used to say which one is clicked. Still haven't add it though cause i wanted to fix this problem before
}
You are not modifying ButtonNum in any way, I assume the goal is to use i as button number, try changing your code to:
public void Start()
{
for (int i = 0; i < ButtonsPage.Length; i++)
{
var temp = i;
ButtonsPage[i].GetComponent<Button>().onClick.AddListener(delegate { ButtonClicked(temp); });
}
}
Temporary variable is required due to how closures work in C#.

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# Global Object [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 building a program for my course in which i need global objects as i intend to have the object accessible from many forms and edited also. Before saying just use global variables i cant the specs state for use of OOP.
my latest attempt to fix this is using a public class but this gave a protection error problem
Code:
Form1.cs (forgot to rename and not re doing all the code and design)
public class ObjectsGlobal
{
Bays bay1 = new Bays();
Bays bay10 = new Bays();
}
frmInput.cs
private void btnAdd_Click(object sender, EventArgs e)
{
if ( 1 == Convert.ToInt32(nudBayNum))
{
ObjectsGlobal.bay1.CarMake = txtMake.Text;
}
}
any ideas are welcome at this point
Change it to:
public static class ObjectsGlobal
{
public static Bays bay1 = new Bays();
public static bay10 = new Bays();
}
Also, as recommended in a comment I have now read, take a look at the Singleton Pattern.

How to create thread according to integer value? [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 7 years ago.
Improve this question
I'm trying to create thread according to integer value. For example if the variable is '5', program should create 5 threads or variable is '2', program should create 2 threads, etc. But I can't understand which path I must follow.
It's just a matter of creating the Thread and start it. But I wouldn't suggest you to handle the thread explicitly, but to use Tasks or ThreadPool in order to execute multithreading work.
using System;
using System.Threading;
public class Program
{
public static void Main()
{
int numberOfRequestedThreads = 3;
for (int i = 0; i < numberOfRequestedThreads; i++)
{
var tempThread = new Thread(new ThreadStart(DoWork));
tempThread.Name = i.ToString();
tempThread.Start();
}
}
public static void DoWork()
{
Console.WriteLine("Thread#{0} is now working!", Thread.CurrentThread.Name);
}
}

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

Categories