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

How do I create XML with namespace prefixes?
To refresh a listview in a form from a button click in another form
How to integrate Facebook like button to our asp.net c# website
Passing C-style arrays of structs/records from delphi 5 to C# via COM
Grid Pager not working correctly
AJAX Panel not working on initial global RouteTable
Entity Framework, strange behaviour with required reference, lazy loading
use a ListBox to Filter Datagrid
regasm.exe is not a valid win32 application
How can i pass parameter from javascript to generic handler?
Task<WebResponse>.Wait lasts forever
How to keep a .NET Socket object connection open after postback?
How To send datagrid in function
Can I call functions of an Excel addin in C# project?
How do I know when an Image is loaded in Picturebox
Modify the content of the home page programmatically in SharePoint 2010

Categories

HOME
c#
asp.net
spring-boot
max
algolia
typo3
cors
service
ebay-api
css-selectors
sequence
rmarkdown
android-webview
ndepend
xmlhttprequest
repeater
aggregate
luis
dompdf
owncloud
versioning
autologin
polybase
datastax-enterprise
stack-trace
android-6.0-marshmallow
sqldependency
spock
ipython-parallel
openoffice-impress
undo
opencsv
urhosharp
angularjs-resource
bootcamp
perf
rtsp
lfe
file-conversion
pyexcel
django-tables2
dcom
jscoverage
visualizer
sp
liferay-ide
overhead
bipartite
was
apache-commons-cli
pyautogui
esprima
breadcrumbs
lattice
addressbook
dimensionality-reduction
tinybox2
instaparse
particle-system
database-administration
bbedit
custom-build-step
dynamics-nav
github-for-mac
jcomponent
page-flipping
fileinputstream
stderr
esb-toolkit-2.1
skview
nfa
dockerpy
sony-xperia
shunting-yard
iphone-6
windows-search
mvcgrid
image-preloader
power-management
object-oriented-database
windows-phone-store
mdp
ekevent
jfilechooser
google-voice
dylan
azure-scheduler
uikit-dynamics
junction-table
microsoft-speech-platform
dnsbl
webpage-screenshot
optimistic-concurrency
copyfile
rjs
sipdroid
response-time
mass-emails
code-golf
web-widget
scala-designer
office-2003

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App