Hi im trying to create this program where the txtbox1 will multiply on user's input and automatically shows the total in decimal point same as in other txtboxes, but i'm having a trouble to sum up all the subtotals of every result to overall txtbox total
here is my code in every subtotals
private void txtbox11_TextChanged_1(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtbox12.Text))
txtTotal11.Text =
(Convert.ToInt32(txtbox11.Text)*Convert.ToDecimal(112.61)).ToString();
}
private void txtbox12_TextChanged_1(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtbox12.Text))
txtTotal12.Text =
(Convert.ToInt32(txtbox12.Text) * Convert.ToDecimal(32.10)).ToString();
}
How do i automatically get the sum of txtTotal11 & txtTotal12 to display on txtTotal13?
or should i put an event like "textChanged" on every txtTotal# too?
thanks guys i'm really having a headache on this.
It will be something like:
private void txtboxSubTotal1_TextChanged_1(object sender, EventArgs e)
{
CalcGrandTotal();
}
private void txtboxSubTotal2_TextChanged_1(object sender, EventArgs e)
{
CalcGrandTotal();
}
private void CalcGrandTotal()
{
decimal grandTotal = 0;
decimal parseValue= 0;
if (!string.IsNullOrEmpty(txtboxSubTotal1.Text) && decimal.TryParse(txtboxSubTotal1.Text, parseValue))
grandTotal += parseValue;
if (!string.IsNullOrEmpty(txtboxSubTotal2.Text) && decimal.TryParse(txtboxSubTotal2.Text, parseValue))
grandTotal += parseValue;
txtboxGrandTotal.Text = grandTotal.ToString();
}
Try this: (in every TextChanged event)
decimal total13;
private void txtbox11_TextChanged_1(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtbox12.Text))
var thisTotal = (Convert.ToInt32(txtbox11.Text)*Convert.ToDecimal(112.61)).ToString();
txtTotal11.Text = thisTotal
total13 += thisTotal
txtTotal13.Text = thisTotal.ToString();
}
Related
I have to make a quantile configuration and I have to set a condition so that the second value is higher than the first, the third value is higher than the second and so on. I am using numericupdowns and the value is set by the user. I tried to implement this code but it always shows a message box error even if the values are correct. This is my code so far (using Visual studio and C#):
decimal min = 1;
//when the first numeric is changed:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
numericUpDown1.Minimum = min;
min = numericUpDown1.Value;
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
min++;
numericUpDown2.Minimum = min;
min = numericUpDown2.Value;
}
private void numericUpDown3_ValueChanged(object sender, EventArgs e)
{
min++;
numericUpDown3.Minimum = min;
min = numericUpDown3.Value;
}
private void numericUpDown4_ValueChanged(object sender, EventArgs e)
{
min++;
numericUpDown4.Minimum = min;
numericUpDown4.Maximum = 99;
}
private void button_OK_Click(object sender, EventArgs e)
{
if (
numericUpDown1.Value > numericUpDown2.Value ||
numericUpDown2.Value > numericUpDown3.Value ||
numericUpDown3.Value > numericUpDown4.Value )
{
MessageBox.Show(
"Quantiles are not filled correctly",
"The quantiles aren't filled in correctly", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBoxName.Select();
DialogResult = DialogResult.None;
return;
}
}
You need to keep all the NumericUpDown value inside a List or an array where you can manipulate them like a single entity trhough a loop.
Of course changing the minimum value of one of the numeric elements should be linked to a check for the current value inside that control because you can't have a current value lesser than the new minimum value.
So the first thing to do is to create a global variable inside your form class where you keep the references to all the numeric controls that you want to synchronize
public class Form1
{
private List<NumericUpDown> numbers = new List<NumericUpDown>();
public Form1 : Form
{
InitializeComponent();
numbers.AddRange(new [] {n1, n2,n3,n4,n5});
}
......
}
Now you can write a method like this one that adjust the minimum on all the numeric included in the list
private void UpdateMinimum()
{
for (int x = 0; x < numbers.Count-1; x++)
{
if(numbers[x].Value > numbers[x+1].Value)
numbers[x+1].Value = numbers[x].Value;
numbers[x+1].Minimum = numbers[x].Value;
}
}
finally you have all your NumericUpDown event ValueChanged call the same method
void numerics_ValueChanged(object sender, EventArgs e)
{
UpdateMinimum();
}
If you want to set a condition so that the second value is higher than the first, the third value is higher than the second and so on, you can refer to the following code:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
numericUpDown1.Minimum = 1;
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
numericUpDown2.Minimum = numericUpDown1.Value + 1;
}
private void numericUpDown3_ValueChanged(object sender, EventArgs e)
{
numericUpDown3.Minimum = numericUpDown2.Value + 1;
}
private void numericUpDown4_ValueChanged(object sender, EventArgs e)
{
numericUpDown4.Minimum = numericUpDown3.Value + 1;
numericUpDown4.Maximum = 99;
}
private void button_OK_Click(object sender, EventArgs e)
{
if (
numericUpDown1.Value > numericUpDown2.Value ||
numericUpDown2.Value > numericUpDown3.Value ||
numericUpDown3.Value > numericUpDown4.Value)
{
MessageBox.Show(
"Quantiles are not filled correctly",
"The quantiles aren't filled in correctly", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBoxName.Select();
DialogResult = DialogResult.None;
return;
}
}
Here is the test result:
I need help with program, where I have a listbox with names and I need to check if there is more than 3 same names in the listbox when I press the buttonControl, the MessegeBox is displayed and it says "There are too many of the same names in the list as allowed". The code is like this for now:
private void buttonVlozjmeno_Click(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex;
string add_name = textBoxName.Text;
if (index >= 0)
listBox1.Items.Insert(index, add_name);
else
listBox1.Items.Add(add_name);
textBoxJmeno.Text = null;
}
private void buttonADD_Click(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(index);
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("Adam");
listBox1.Items.Add("Adam");
listBox1.Items.Add("Adam");
listBox1.Items.Add("John");
listBox1.Items.Add("John");
listBox1.Items.Add("Eva");
listBox1.Items.Add("John");
listBox1.Items.Add("John");
}
private void buttonControl_Click(object sender, EventArgs e)
{
}
You can try querying the box with a help of Linq, e.g. for WinForms it can be
using System.Linq;
...
bool tooManySameNames = myBox
.Items
.OfType<String>()
.GroupBy(name => name)
.Any(group => group.Count() > 3);
I am creating a C# project which takes two user inputs of complex numbers and completes a mathematical operation on them. The problem I am running into is how to parse i as the square root of -1.
double operandA = 0;
double operandB = 0;
double result = 0;
string textA, textB;
string error = "The value you entered is invalid, try again";
private void plus_Btn_Click(object sender, EventArgs e)
{
result = operandA + operandB;
}
private void Subtract_btn_Click(object sender, EventArgs e)
{
result = operandA - operandB;
}
private void mult_Btn_Click(object sender, EventArgs e)
{
result = operandA * operandB;
}
private void divide_Btn_Click(object sender, EventArgs e)
{
result = operandA / operandB;
}
private void textBox2_Leave(object sender, EventArgs e)
{
textB = textBox2.Text;
if (double.TryParse(textB, out operandB))
operandB = double.Parse(textB);
else
{
MessageBox.Show(error);
textBox2.Select();
}
}
private void textBox1_Leave(object sender, EventArgs e)
{
textA = textBox1.Text;
if (double.TryParse(textA, out operandA))
operandA = double.Parse(textA);
else
{
MessageBox.Show(error);
textBox1.Select();
}
}
It works fine on regular numbers, decimals, and negative numbers, but I can't figure out how to do the value of i that I need. Can anyone help? It has been suggested that I use System.Numeric.Complex, but whenever I try to do this using "Using System.Numerics.Complex" or simply "Using System.Numerics", it says this type/namespace does not exist inside 'System'.
If you want to support operations on complex numbers, you should consider using System.Numerics.Complex
Complex c = Complex.Sqrt(-1);
Console.WriteLine(c + 1);
For documentation on this type see here
You can use Complex in System.Numerics
https://msdn.microsoft.com/en-us/library/system.numerics.complex(v=vs.110).aspx
my problem is that i'm trying to have a textbox display the number of times that I have clicked on the screen. I'm having the issue of not being able to take the textbox's text and convert it to a int. Thanks!
{
public partial class Form1 : Form
{
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
button1.Location = new Point(rand.Next(0, 750), rand.Next(0, 750));
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right)
{
int mouseclick = 0;
textBox1.Text = Int32.Parse(mouseclick);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Try the following changes:
{
public partial class Form1 : Form
{
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
button1.Location = new Point(rand.Next(0, 750), rand.Next(0, 750));
}
int mouseclick = 0;
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right)
{
mouseclick++;
}
textBox1.Text = mouseclick.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
I have moved mouseclick out of the event so that it can maintain its value and i am only incrementing mouseclick if the user clicks with the left mouse button.
The textbox datatype is a string, but its assignment is a number being parsed as a number. Perhaps the following example would be helpful.
string mouseclick = "0";
int intMouseClick = Int32.Parse(mouseclick);
textBox1.Text = intMouseClick.ToString();
You tried to convert an int into string bei using Int32.Parse(...).
But this function does the reverse conversion. It converts a string to an int.
First off, you are telling the program to set mouseclicks to 0 every single time it is clicked. You might instead want the code to read:
if (e.Button != MouseButtons.Right)
{
int mouseclick = mouseclick + 1;
textBox1.Text = Int32.Parse(mouseclick);
}
or move the int out of the if statement and just do mouseclick++;
On top of that, if you are trying to set the text boxes text, you can do it much easier by just doing this:
textBox1.Text = mouseclick.ToString();
I want to have the abilitiy to add numeric fields using an event as i have over 50 i dont fancy adding event to each one is there a better way of doing the following. Also I cant seem to get sumEarnings to be reconized by the compiler does anybody have any suggestions.
protected void Page_Load(object sender, EventArgs e)
{
wagesnET.ValueChanged += sumEarnings();
}
protected void btnSave_Click(object sender, EventArgs e)
{
StringDictionary KeyValue = new StringDictionary();
KeyValue.Add("", "");
foreach (string key in Request.Form)
{
if (!key.StartsWith("checkbox")) continue;
}
}
private void sumEarnings(object sender, EventArgs e)
{
int total = Convert.ToInt16(wagesnET.Text) + Convert.ToInt16(partnerearningscic.Text);
return total;
}
sumEarnings is a method without return type, change it as
private int sumEarnings()
{
return Convert.ToInt16(wagesnET.Text) + Convert.ToInt16(partnerearningscic.Text);
}
To attach it to multiple controls from the code you can use enumerator. For example:
foreach (Control c in this.Controls)
{
//if (c is TextBox)
c.ValueChanged += sumEarnings();
}
You need to remove the () from the event handler assignment:
wagesnET.ValueChanged += sumEarnings;
After you calculate the new total value udpate the appropriate control:
private void sumEarnings(object sender, EventArgs e)
{
int total = Convert.ToInt16(wagesnET.Text) + Convert.ToInt16(partnerearningscic.Text);
// todo: update the total value...
}
It's not possible to have an event handler that return a value at the same time.