I need to have a text box that dynamically changes as values in other text boxes are being changed, and the value (would be an integer converted to string) of the "Total" textbox has to have the sum of the other values. Is there a way to do this without having to create a TextChanged event for each of the other textboxes?
CodePudding user response:
In Visual studio, go to Form1.cs[Design] and leftclick a textbox once, then, on the right hand lower side, click the lightning that says "events". Search for "TextChanged" and type in how you would like the event to be named (e.g. "textBox1_TextChanged"). on hitting enter, you should automatically be sent to Form1.cs, where a new void was created. There, insert the code proposed by Stefanovici (although change textbox1.value to textbox1.text) and rinse and repeat for each textbox
private void textBox1_TextChanged(object sender, EventArgs e) //should automatically be created
{
Label1.text= textbox1.text textbox2.text textbox3.text .....
}
Edit: you can also bind multiple textboxes to the same event. Naming the event identical for every textbox will call the event when either textbox was changed, reducing the amount of code
CodePudding user response:
One possible solution “without” having to wire up all the events is to use each text boxes DataBindings property. The only problem there is we would need some kind of DataSource. And a simple Class may be a simple solution there. Example…
We could create a class that has a string property for each text box that would represent each string in each text box. Then a Total property to be the total of all the text boxes. Once we have this class object instantiated, we could then assign each text boxes DataBindings to the matching property in our class. This simplified TextBoxTotals class may look something like…
public class TextBoxesTotal {
public string TB1 { get; set; }
public string TB2 { get; set; }
public string TB3 { get; set; }
public string Total {
get {
int.TryParse(TB1, out int v1);
int.TryParse(TB2, out int v2);
int.TryParse(TB3, out int v3);
return (v1 v2 v3).ToString();
}
}
}
Then in the forms load event, you could instantiate a new TextBoxesTotal object and then use it as a DataSource for the text boxes DataBindings. Usage may look something like…
TextBoxesTotal TextBoxTotals = new TextBoxesTotal();
public Form2() {
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e) {
textBox1.DataBindings.Add(new Binding("Text", TextBoxTotals, "TB1"));
textBox2.DataBindings.Add(new Binding("Text", TextBoxTotals, "TB2"));
textBox3.DataBindings.Add(new Binding("Text", TextBoxTotals, "TB3"));
textBox4.DataBindings.Add(new Binding("Text", TextBoxTotals, "Total"));
}
This will “update” the totals text box when the user changes the text in a text box then “leaves” the text box. Sorry if this isn’t much help as it appears to me we are STILL having to assign each text box. But it may be a little less code. If this doesn’t help, let me know and I will delete it.
CodePudding user response:
I'm assuming that you don't want to create TextChanged event handlers for each TextBox because it is a tedious job. There's a way to make it easy.
Start with a field like this:
private TextBox[] textBoxes = null;
Then you can set up all of the TextChanged event handlers like this:
textBoxes = new []
{
textBox1, textBox2, textBox3,
textBox4, textBox5,
};
foreach (var tb in textBoxes)
{
tb.TextChanged = (_, _) => UpdateTotal();
}
Now the UpdateTotals can be defined any way you want. Here's my version:
private void UpdateTotal()
{
var value =
textBoxes
.Select(x =>
double.TryParse(x.Text, out double value)
? value
: 0.0)
.Sum();
textBoxTotal.Text = value.ToString();
}
CodePudding user response:
Should be something like
Label1.text= textbox1.value textbox2.value textbox3.value .....
