Rockraizer
Rockraizer
CC#
Created by Rockraizer on 9/16/2023 in #help
✅ How can I make my code reusable?
Hi! I asked a question before to calculate the average of 5 text boxes in one textbox. I wrote a code:
public class AverageViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

public double TextBox1Value
{
get { return textBox1Value; }
set
{
textBox1Value = value;
CalculateAverage();
OnPropertyChanged();
}
}

// Define properties for TextBox2Value, TextBox3Value, TextBox4Value, TextBox5Value, and Average similarly.

private void CalculateAverage()
{
Average = (TextBox1Value + TextBox2Value + TextBox3Value + TextBox4Value + TextBox5Value) / 5.0;
}

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class AverageViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

public double TextBox1Value
{
get { return textBox1Value; }
set
{
textBox1Value = value;
CalculateAverage();
OnPropertyChanged();
}
}

// Define properties for TextBox2Value, TextBox3Value, TextBox4Value, TextBox5Value, and Average similarly.

private void CalculateAverage()
{
Average = (TextBox1Value + TextBox2Value + TextBox3Value + TextBox4Value + TextBox5Value) / 5.0;
}

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
I want to use this in more situations but I don't want to define every textbox and make it really redundant.
7 replies
CC#
Created by Rockraizer on 9/14/2023 in #help
✅ Synchronize text box to calculate the average of the next 5 text boxes
I have a WPF project where I want to calculate the average of 5 textboxes. I want to synchronize them to always show the value of the currently typed values in the text box. Thanks the help!
7 replies