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 to get the portion of image inside rectangle for cropping , windows phone 8
Use a binding to set the text property of a textbox in a usercontrol - WPF
C# Debug - cannot start debugging because the debug target is missing
Generic method in C#
tcp having trouble connecting c#
Are there commonly used delegates available in the standard class libraries?
How to set website theme to all webpages using a drop down list?
How to set selectedcolor of ColorCanvas by using Argb in code behind
How to set a transition property using Transitionals library
How to limit textbox maxlength by byte in winrt?
Entity Framework 6 database migrations for isolated multi-tenant setup
File upload / did I make a mistake choosing vb.net instead of php?
Find data in database
Calling sharepoint from web service
Baidu web search engine API
Searching for records on one exact day

Categories

HOME
stanford-nlp
sql-server-2014
domain-driven-design
cors
ssh
data-modeling
arguments
singleton
artifactory
osx-yosemite
genexus
wkhtmltopdf
elastalert
dynamics-crm-2011
vichuploaderbundle
guidewire
camunda
geography
draw2d
scotty
minimagick
restructuredtext
coccinelle
hololens
fastboot
android-xml
grunt-contrib-uglify
orgchart
tftp
multiple-inheritance
launchd
unity3d-5
android-scrollview
matlab-compiler
http-proxy
motherboard
client-server
mapquest
iterm2
motion
gem-fury
linkurious
rbac
directadmin
autodesk-data-management
f#-fake
codepen
ack
geminabox
spring-cloud-bus
programming-pearls
overhead
rmq
tsc
cardview
scanf
vertex-shader
sqlproj
cedar-bdd
turbolinks
settext
android-xmlpullparser
vitamio
database-administration
boost-propertytree
google-app-engine-php
gitk
dnvm
android-networking
genome
autopep8
data-quality-services
quickfixn
illegalargumentexception
xmlserializer
visual-c#-express-2010
lettuce
azure-caching
xps
html5-filesystem
mdp
imaplib
xcode6.1-gm-seed
tlbimp
proj4
symbian3
exi
gwt2
generator-expression
mmo
dnsbl
tasklist
shapado
flexbuilder
p4.net
scala-designer

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