I'm just trying to figure out how I can enter a number into a TextBox and click a button and make the number go into another TextBox. I know I need to make a double value like price and make it equal to zero. I'm just wondering how I would make the button control the first TextBox.
private void DepositTextBox_TextChanged(object sender, EventArgs e)
{
string deposits = Console.ReadLine();
double deposit = double.Parse(deposits);
deposit += balance;
}
private void WithdrawTextBox_TextChanged(object sender, EventArgs e)
{
string withdraws = Console.ReadLine();
double withdraw = double.Parse(withdraws);
withdraw += balance;
}
This is my code but when I run it as soon as I put a number or letter in the TextBox, it says value cannot be null, parameter name: value.
Why do you speak of a Button Click but your code sample shows a TextChanged event?
Sounds like you should probably create a form-level property to the store the total balance and manipulate that.
In your ButtonClick events, Convert the textbox.Text to a numeric type and then perform the appropriate mathmatical operation on the total balance.
Then simply just display that balance property in the other textbox.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private double balance;
private void btnDeposit_Click(object sender, EventArgs e)
{
double value = Convert.ToDouble(txtDeposit.Text);
balance += value;
txtBalance.Text = balance.ToString();
}
private void btnWithdraw_Click(object sender, EventArgs e)
{
double value = Convert.ToDouble(txtWithdraw.Text);
balance -= value;
txtBalance.Text = balance.ToString();
}
}
Related
I'm trying to have a button click event add an int value to a textbox and then have another button click event subtract from the same textbox that I just added the int value to. The problem I'm having is that the subtract button enters a negative(-1) value to the textbox and if I click on the add button again it goes right back to the int value I had before clicking the subtract button. I just want the add button to add and the subtract button to subtract the in value. I'm very new to c# and I've tried a everything I've found online and I'm losing faith.
I've tried if statements, and some other methods I found online and nothing works.
This is the code I have now. The add button works fine, but the subtract button doesn't do what I want.
private int a = 0;
private void btnAdd_Click(object sender, EventArgs e)
{
a++;
txtSummary.Text = a.ToString();
}
private void comboBoxValues_SelectedIndexChanged(object sender, EventArgs e)
{
}
private int b = -0;
private void btnSubtract_Click(object sender, EventArgs e)
{
b--;
txtSummary.Text = b.ToString();
}
This will convert whatever is in your TextBox to an Integer, then add or subtract 1 to that value:
private void Form1_Load(object sender, EventArgs e)
{
txtSummary.Text = "0";
}
private void btnAdd_Click(object sender, EventArgs e)
{
AddToTextBox(1);
}
private void btnSubtract_Click(object sender, EventArgs e)
{
AddToTextBox(-1);
}
private void AddToTextBox(int changeBy)
{
int value;
if(int.TryParse(txtSummary.Text, out value))
{
value = value + changeBy;
txtSummary.Text = value.ToString();
}
else
{
MessageBox.Show("Invalid Integer in TextBox!");
}
}
Below is some of my code that isn't working. I wanted to see it could count the number of spaces in the text box so I'm having it display the number in a message box and it currently just throws a blank one. The problem is either in the float to string conversion or in the counter.
private static string _globalVar = "n";
public static string GlobalVar
{
get { return _globalVar; }
set { _globalVar = value; }
}
public Form1()
{
InitializeComponent();
button1.Text = "Enter";
}
string LNum { get; set; }
public void button1_Click_1(object sender, EventArgs e)
{
MessageBox.Show(LNum);
}
public void richTextBox1_TextChanged(object sender, System.Windows.Forms.KeyEventArgs e)
{
float n = 0;
if (Control.ModifierKeys == Keys.Space)
{
n = n + 1; ;
}
string.Format("{0:N1}", n);
string LNum = Convert.ToString(n);
}
What you are doing is an excellent way to learn how and when the events are raised and how they can be leveraged to customize an applications behavior and is something similar to what many of us have done over the years.
Based on what you say you are wanting to do, there are several issues. If you take a look at this code, it will do what you want.
public void button1_Click(object sender, EventArgs e)
{
int n = 0;
for (int counter = 0; counter < richTextBox1.TextLength; counter++)
{
if (richTextBox1.Text[counter] == ' ')
{
n++;
}
}
MessageBox.Show(n.ToString("N1"));
}
The key difference is that I am only looking at the entered text when the button is clicked. (TextChanged is run every time there is a change in the text displayed). I chose not to use a float variable to store the count of spaces as the count will always be an integer.
In addition, the parameter to TextChanged System.Windows.Forms.KeyEventArgs e is incorrect and will never compile if correctly bound to the TextChanged event.
The KeyEventArgs parameter is used by the KeyUp and KeyDown events. If you use these events, you will be looking to count every time the space bar is pressed and not the number of spaces in the text box. And like their name suggests, the events are raised every time a Key on the keyboard goes Up (Pressed) and Down (Released).
The title was properly written wrong but i will explain currently i have 2 sets of three buttons linked to a label. when i press a button it will place a number in the label as a result what i want to do is after the first button is pressed and then a second is clicked i want the two scores to add together to make a total result in the label eg if i press "three of a kind" which equals 3 points then press "four of a kind" which equals 6 points i want a result of 9 in the label and then if i press "five of a kind" which equals 12 point the label would then read 18 ect can i get any help please there isnt any code to put in due to not finding anything i will put in how my buttons are linked to my labels.
Result
public class Result
{
}
private void button2_Click(object sender, EventArgs e)
{
String add = (Convert.ToInt32(3)).ToString();
label6.Text = Convert.ToString(add);
}
private void button5_Click(object sender, EventArgs e)
{
String add = (Convert.ToInt32(3)).ToString();
label7.Text = Convert.ToString(add);
}
private void button3_Click(object sender, EventArgs e)
{
String add = (Convert.ToInt32(5)).ToString();
label6.Text = Convert.ToString(add);
}
private void button6_Click(object sender, EventArgs e)
{
String add = (Convert.ToInt32(5)).ToString();
label7.Text = Convert.ToString(add);
}
private void button4_Click(object sender, EventArgs e)
{
String add = (Convert.ToInt32(12)).ToString();
label6.Text = Convert.ToString(add);
}
private void button7_Click(object sender, EventArgs e)
{
String add = (Convert.ToInt32(12)).ToString();
label7.Text = Convert.ToString(add);
}
I didn't quite understand the question, but I would suggest you to read about WPF and the MVVM mechanism to sync the data between the UI and the data.
What you're doing is writing a lot of code-behind code that is duplicated over and over and is doing a lot of unnecessary work (you convert a number to Int, then convert the result to string and then convert it again to string)
This function can be used to do the work property and efficiently once:
private void UpdateLabelText(Label label, int number) {
label.Text = number.ToString();
}
And use this method whenever you need to update the text in your label.
You have to cast the value of label.Text to int then add the value and then recast to string:
private void button6_Click(object sender, EventArgs e)
{
AddToLabel(label7, 12);
}
void AddToLabel(Label label, int value)
{
var n = int.Parse(label.Text); // convert the actual value of label.Text to int
var add = n + value; // add the increment
label.Text = add.ToString(); // assign to label.Text
}
How can i make auto refresh textbox while typing value like this?
i tried to do the same but it did not work. i always to hit ENTER to refresh or click on up/down arrows to refresh the value
here is the code
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
try
{
double a = double.Parse(s1.Text); //textbox 1
double b = double.Parse(s2.Text); //textbox 2
double s = a * b;
resultSpeed.Text = "" + s; //s is the result
}
catch
{
MessageBox.Show("Please input the number");
}
}
Just use event KeyUp. It will trigger every time you put a symbol.
ValueChanged isn't working because it only triggers when you are done with editing - you press enter or change focus.
So basically change your event from ValueChanged to KeyUp.
I'm not posting any code because the only change will be subcribing to other event. Your function is fine, however you should change its name :)
Put your code into textbox's TextChanged Event.
Like this
private void textBox1_TextChanged(object sender, EventArgs e)
{
calculate();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
calculate();
}
private void calculate()
{
double a = 0, b = 0, demo;
if (double.TryParse(textBox1.Text, out demo))
a = double.Parse(textBox1.Text); //textbox 1
if (double.TryParse(textBox2.Text, out demo))
b = double.Parse(textBox2.Text); //textbox 2
double s = a * b;
textBox3.Text = s.ToString(); //s is the result
}
I am studying C# now, so i am making some kind of exercises to get used to C# syntax and learn better. I decided to make a calculator looking alike the normal windows calculator.
I created only one button "1" and one textbox.
I want to make this button write 1 in textbox when I press it and also make an int variable equal to the number in the textbook, to make the calculation later. So I can't either change "int a"'s value or change text in the textbox, it always shows 01, because a always equals to 0.
How can I make the program, both show the correct numbers and change the value of a correctly?
For example how can i make the program show eleven in the textbox when i press button twice and also change "int a"'s value to 11?
public partial class Form1 : Form
{
int a;
string Sa;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Sa = a.ToString() + "1";
textBox1.Text = Sa;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += "1";
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
a = Int32.Parse(textBox1.Text);
}
just it.. Change text on textBox every button click, and change variable a every textBox changed.
The value could then be set using
a = int.Parse(Sa);
textBox1.Text = Sa.TrimStart('0');
Although if you'd like to be more efficient about it,
a = a * 10 + 1;
not have Sa at all,
textBox1.Text = a.ToString();
If you run into integer overflows, you should use BigInteger.
You have several options:
Make int a nullable int. That way you can check if int is already set
int? a;
if ( a.HasValue )
{
}
else
{
}
Check if the Text property of textBox1 is empty (which means you don't have to append a to it)
if ( textBox1.Text == string.Empty)
{
}
else
{
}
public void btnOne_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnOne.Text;
}
private void btnTwo_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnTwo.Text;
}
// etc
for any button you want to append text to the text box set the click property to btn_Click then pt this code inside the method
private void btn_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
// This will assign btn with the properties of the button clicked
txt_display.Text = txt_display.Text + btn.Text;
// this will append to the textbox with whatever text value the button holds
}