c#
How would I combine multiple methods in to one method
I have 15 fields that do practically the same thing: private void TextboxMessage1_TextChanged(object sender, TextChangedEventArgs e) { SaveMessage(TextboxMessage1.Text, TextboxMessageSeconds1.Text.ToInteger(), 1); } private void TextboxMessage2_TextChanged(object sender, TextChangedEventArgs e) { SaveMessage(TextboxMessage2.Text, TextboxMessageSeconds2.Text.ToInteger(), 2); } private void TextboxMessage3_TextChanged(object sender, TextChangedEventArgs e) { SaveMessage(TextboxMessage3.Text, TextboxMessageSeconds3.Text.ToInteger(), 3); } etc Which calls SaveMessage: private void SaveMessage(string textboxMessageText, int seconds, int messagenumber) { var msg = _configman.MyConfig.MessageConfigs.FirstOrDefault(x => x.MessageNumber == messagenumber); if (msg == null) { var msgconfig = new MessageConfig(); msgconfig.Seconds = seconds; msgconfig.Command = textboxMessageText; } else { msg.MessageNumber = messagenumber; msg.Command = textboxMessageText; msg.Seconds = seconds; } _configman.SaveConfig(); } My form: <CheckBox x:Name="CheckBoxMessage1" Content="" HorizontalAlignment="Left" Margin="29,23,0,0" VerticalAlignment="Top" Width="15"/> <TextBox x:Name="TextboxMessage1" Height="23" Margin="49,19,158,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" TextChanged="TextboxMessage1_TextChanged"/> <Button x:Name="ButtonMessage1" Content="Disabled" HorizontalAlignment="Left" Margin="689,20,0,0" VerticalAlignment="Top" Width="75" Background="#FFFB8686" Click="ButtonMessage1_Click"/> <CheckBox x:Name="CheckBoxMessage2" Content="" HorizontalAlignment="Left" Margin="29,48,0,0" VerticalAlignment="Top" Width="15"/> <TextBox x:Name="TextboxMessage2" Height="23" Margin="49,44,158,0" TextWrapping="Wrap" VerticalAlignment="Top" TextChanged="TextboxMessage2_TextChanged"/> <Button x:Name="ButtonMessage2" Content="Disabled" HorizontalAlignment="Left" Margin="689,45,0,0" VerticalAlignment="Top" Width="75" Background="#FFFB8686"/> <CheckBox x:Name="CheckBoxMessage3" Content="" HorizontalAlignment="Left" Margin="29,72,0,0" VerticalAlignment="Top" Width="15"/> <TextBox x:Name="TextboxMessage3" Height="23" Margin="49,68,158,0" TextWrapping="Wrap" VerticalAlignment="Top" TextChanged="TextboxMessage3_TextChanged"/> <Button x:Name="ButtonMessage3" Content="Disabled" HorizontalAlignment="Left" Margin="689,69,0,0" VerticalAlignment="Top" Width="75" Background="#FFFB8686"/> Is there a way to make the TextChanged cover all of my textboxes so I don't have to create a TextChanged for each textbox?? They are numbered 1-15 such as TextboxMessage1, TextboxMessage1, etc. Is this possible? If so, how would I do it?
Why don't you use the same "generic" callback like "TextboxMessage_TextChanged" for each of your TextChanged events. Then: <TextBox x:Name="TextboxMessage1" Height="23" Margin="49,19,158,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" TextChanged="TextboxMessage_TextChanged"/> private void TextboxMessage_TextChanged(object sender, TextChangedEventArgs e) { TextBox tb = sender as TextBox; int index = int.Parse(tb.Name.Substring("TextboxMessage".Length)); TextBox secondTextBox = (TextBox)this.FindName("TextboxMessageSeconds" + index); SaveMessage(tb.Text, secondTextBox.Text.ToInteger(), index); } Is that something like this that you expect ?
The best way would be to create a new class with an int property that inherits from TextBox //Come up with a better name public class CustomTextBox : TextBox { public int Number { get; set; } } Then in your xaml change the property type to CustomTextBox (or whatever you name it). Make sure to add the Number property and change the TextChanged property to "TextBoxSave_TextChanged": <CustomTextBox x:Name="TextboxMessage1" Height="23" Margin="49,19,158,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" TextChanged="TextBoxSave_TextChanged" Number="1"/> Then in your code behind add a new method TextBoxSave_TextChanged() //You can name it whatever, I would keep the "_TextChanged" suffix though private void TextBoxSave_TextChanged(object sender, TextChangedEventArgs e) { //Cast sender to your new CustomTextBox type CustomTextBox txtBox = ((CustomTextBox)sender); SaveMessage(txtBox.Text, txtBox.Text.ToInteger(), txtBox.Number); } Ed's suggestion: public class CustomTextBox : TextBox { #region Number Property public int Number { get { return (int)GetValue(NumberProperty); } set { SetValue(NumberProperty, value); } } public static readonly DependencyProperty NumberProperty = DependencyProperty.Register(nameof(Number), typeof(int), typeof(CustomTextBox), new PropertyMetadata(0)); #endregion Number Property #region Seconds Property public int Seconds { get { return (int)GetValue(SecondsProperty); } set { SetValue(SecondsProperty, value); } } public static readonly DependencyProperty SecondsProperty = DependencyProperty.Register(nameof(Seconds), typeof(int), typeof(CustomTextBox), new PropertyMetadata(0)); #endregion Seconds Property } TextChanged handler: // You can name it whatever, I would keep the "_TextChanged" suffix though private void TextBoxSave_TextChanged(object sender, TextChangedEventArgs e) { //Cast sender to your new CustomTextBox type CustomTextBox txtBox = (CustomTextBox)sender; SaveMessage(txtBox.Text, txtBox.Seconds, txtBox.Number); } XAML usage: <local:CustomTextBox Number="1" Seconds="{Binding Text, ElementName=TextboxMessageSeconds1}" TextChanged="TextBoxSave_TextChanged" x:Name="TextboxMessage1" Height="23" Margin="49,19,158,0" TextWrapping="Wrap" VerticalAlignment="Top" /> But really, this is a poor substitute for writing a proper UserControl.
Related Links
LINQ expression duplicates values on select instead of cycling them
Using DI in ConfigureService-Methods (especially IApplicationLifetime) in .NET Core
C#: Map strings and binary data
is nested Linq faster than foreach loop? [closed]
Performance difference with various List intialization and population techniques
The name InitializeComponent does not exist in the current context (C#, WPF)
SQLite ExecuteNonQuery doesn't let me close the connection afterwards
log4net log file is not getting created in C#
Save image to web server from window application
Data not displaying in gmail attachment
How can I name a namespace dedicated to span just classes holding exclusively static fields? [closed]
.net core identity change tables names
Visual Studio - 'View Code' not working?
in nested root node how to delete one node particularly
Signalr 2 disconnects on method to ListAll in table, but can insert into table with Create method
parse file in C# with Regex and search lines