How to sum a two number from two different textbox without clicking the button the sum will automatically displayed in the label
protected void button1(object sender, EventArgs e)
{
double a, b, product;
a = double.Parse(txtt1.Text);
b = 15;
product = a * 15;
lbl_.Text = Convert.ToString(product);
CodePudding user response:
Use the events that are designed for such purposes. If you even want to set default values and to show a sum of them, you can do this in the show event of your form like this:
private void Form1_Shown(Object sender, EventArgs e)
{
int test1 = 1;
int test2 = 2;
checkBox1.Text = test1.ToString();
checkBox2.Text = test2.ToString();
checkBox3.Text = (test1 test2).ToString();
}
Of course, this code is not really what you will do, it's just to demonstrate how to do such things. When one of the values within the other checkboxes has changed, you can rebuild the sum and display it using the text changed event. Something like this:
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
int valueToAdd = Convert.ToInt32(textBox2.Text);
int newValue = Convert.ToInt32(textBoxToChange.Text) valueToAdd;
textBoxToChange.Text = newValue.ToString();
}
Again, just the idea, no real code. Of course, you should use the sender within the events.
CodePudding user response:
Consider using a class setup with change notification using INotifyPropertyChanged
and in your form data binding to, in this case two properties with a third property for the result.
Here when values change in the form the label will reflect Value1 * Value2.
public class Item : INotifyPropertyChanged
{
private double _value1;
private double _value2;
public double Value1
{
get => _value1;
set
{
_value1 = value;
OnPropertyChanged();
}
}
public double Value2
{
get => _value2;
set
{
_value2 = value;
OnPropertyChanged();
}
}
public double Total => Value1 * Value2;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
In the form, setup a key press event for two TextBox controls to allow only double.
Use data binding for two TextBox and one Label for the results of the two TextBox controls.
public partial class Form1 : Form
{
private readonly BindingSource _bindingSource = new BindingSource();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, System.EventArgs e)
{
textBox1.KeyPress = TextBoxOnKeyPress;
textBox2.KeyPress = TextBoxOnKeyPress;
// setting properties is optional as they default to 0
_bindingSource.DataSource = new List<Item>()
{
new Item() {Value1 = 12, Value2 = 4}
};
textBox1.DataBindings.Add("Text", _bindingSource,
nameof(Item.Value1));
textBox2.DataBindings.Add("Text", _bindingSource,
nameof(Item.Value2));
label1.DataBindings.Add("Text", _bindingSource,
nameof(Item.Total));
}
private void TextBoxOnKeyPress(object sender, KeyPressEventArgs e)
{
if (((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 46))
{
e.Handled = true;
return;
}
if (e.KeyChar == 46)
{
if ((sender as TextBox).Text.IndexOf(e.KeyChar) != -1)
e.Handled = true;
}
}
}