I have several text boxes which will hold a default value of 10, each box has two button adjacent to them, these are supposed to be used to increment the text box values by 1 or decrease by one once clicked. I do know i could simply make a click event handler for each and every button but instead I would like to make it so instead the method would find the designated text box possibly by passing it from xaml. Forgive me if this is obvious how to do so but I'm stumped.
I've into binding but can't quite figure out how to utilize that in order to make it work.
<Grid Background="DarkMagenta" Margin="0,0,0,-31">
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="0,10, 0, 20">
<Label>Set Your Stats!</Label>
<StackPanel Orientation="Vertical" HorizontalAlignment="Left">
<Label>Strenght</Label>
<StackPanel Orientation="Horizontal">
<TextBox Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button Click="Button_Click">+</Button>
<Button>-</Button>
</StackPanel>
</StackPanel>
<Label>Dexterity</Label>
<StackPanel Orientation="Horizontal">
<TextBox Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button>+</Button>
<Button>-</Button>
</StackPanel>
</StackPanel>
<Label>Constitution</Label>
<StackPanel Orientation="Horizontal">
<TextBox Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button>+</Button>
<Button>-</Button>
</StackPanel>
</StackPanel>
<Label>Intelligence</Label>
<StackPanel Orientation="Horizontal">
<TextBox Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button>+</Button>
<Button>-</Button>
</StackPanel>
</StackPanel>
<Label>Wisdom</Label>
<StackPanel Orientation="Horizontal">
<TextBox Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button>+</Button>
<Button>-</Button>
</StackPanel>
</StackPanel>
<Label>Charisma</Label>
<StackPanel Orientation="Horizontal">
<TextBox Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button>+</Button>
<Button>-</Button>
</StackPanel>
</StackPanel>
</StackPanel>
<Button HorizontalAlignment="Stretch" Margin="80,0,80,0" >Continue</Button>
</StackPanel>
CS. File Method
public partial class StatSelection : Page
{
CharacterDetails charDetails = new CharacterDetails();
public StatSelection(CharacterDetails addedDetails)
{
charDetails = addedDetails;
InitializeComponent();
}
private void IncrementBy1(object sender, RoutedEventArgs e)
{
// code for incrementing the textbox by 1
}
}
I'd like the method to have one or two methods that find the right text box to increment based on the text box being passed through to the method each time its called.
Thank you to user2818985 and Nawed Nabi Zada for your answers. Using your advice I found a method to use only one click event for all the buttons!
<Grid Background="DarkMagenta" Margin="0,0,0,-31">
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="0,10, 0, 20">
<Label>Set Your Stats!</Label>
<StackPanel Orientation="Vertical" HorizontalAlignment="Left">
<Label>Strenght</Label>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="StrTxtBx" Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button x:Name="StrIncrease" Click="IncreaseDecrease">+</Button>
<Button x:Name="StrDecrease" Click="IncreaseDecrease">-</Button>
</StackPanel>
</StackPanel>
<Label>Dexterity</Label>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="DexTxtBx" Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button x:Name="DexIncrease" Click="IncreaseDecrease">+</Button>
<Button x:Name="DexDecrease" Click="IncreaseDecrease">-</Button>
</StackPanel>
</StackPanel>
<Label>Constitution</Label>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="ConTxtBx" Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button x:Name="ConIncrease" Click="IncreaseDecrease">+</Button>
<Button x:Name="ConDecrease" Click="IncreaseDecrease">-</Button>
</StackPanel>
</StackPanel>
<Label>Intelligence</Label>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="IntTxtBx" Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button x:Name="IntIncrease" Click="IncreaseDecrease">+</Button>
<Button x:Name="IntDecrease" Click="IncreaseDecrease">-</Button>
</StackPanel>
</StackPanel>
<Label>Wisdom</Label>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="WisTxtBx" Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button x:Name="WisIncrease" Click="IncreaseDecrease">+</Button>
<Button x:Name="WisDecrease" Click="IncreaseDecrease">-</Button>
</StackPanel>
</StackPanel>
<Label>Charisma</Label>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="ChrTxtBx" Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button x:Name="ChrIncrease" Click="IncreaseDecrease">+</Button>
<Button x:Name="ChrDecrease" Click="IncreaseDecrease">-</Button>
</StackPanel>
</StackPanel>
</StackPanel>
<Button HorizontalAlignment="Stretch" Margin="80,0,80,0" >Continue</Button>
</StackPanel>
</Grid>
Cs Code
private void IncreaseDecrease(object sender, RoutedEventArgs e)
{
string btnName = ((Button)sender).Name;
int TxtBxVal;
switch(btnName)
{
case "StrIncrease":
TxtBxVal = int.Parse(StrTxtBx.Text);
StrTxtBx.Text = (++TxtBxVal).ToString();
break;
case "StrDecrease":
TxtBxVal = int.Parse(StrTxtBx.Text);
StrTxtBx.Text = (--TxtBxVal).ToString();
break;
case "DexIncrease":
TxtBxVal = int.Parse(DexTxtBx.Text);
DexTxtBx.Text = (++TxtBxVal).ToString();
break;
case "DexDecrease":
TxtBxVal = int.Parse(DexTxtBx.Text);
DexTxtBx.Text = (--TxtBxVal).ToString();
break;
case "ConIncrease":
TxtBxVal = int.Parse(ConTxtBx.Text);
ConTxtBx.Text = (++TxtBxVal).ToString();
break;
case "ConDecrease":
TxtBxVal = int.Parse(ConTxtBx.Text);
ConTxtBx.Text = (--TxtBxVal).ToString();
break;
case "IntIncrease":
TxtBxVal = int.Parse(IntTxtBx.Text);
IntTxtBx.Text = (++TxtBxVal).ToString();
break;
case "IntDecrease":
TxtBxVal = int.Parse(IntTxtBx.Text);
IntTxtBx.Text = (--TxtBxVal).ToString();
break;
case "WisIncrease":
TxtBxVal = int.Parse(WisTxtBx.Text);
WisTxtBx.Text = (++TxtBxVal).ToString();
break;
case "WisDecrease":
TxtBxVal = int.Parse(WisTxtBx.Text);
WisTxtBx.Text = (--TxtBxVal).ToString();
break;
case "ChrIncrease":
TxtBxVal = int.Parse(ChrTxtBx.Text);
ChrTxtBx.Text = (++TxtBxVal).ToString();
break;
case "ChrDecrease":
TxtBxVal = int.Parse(ChrTxtBx.Text);
ChrTxtBx.Text = (--TxtBxVal).ToString();
break;
}
}
With this the box increase and decrease as needed.
Might look into ways of trimming it down in the future but for now im happy with it.
Thank you both once again!