Reputation: 7
I'm trying to increase the value in the textbox by using arrowkeys. I'm using wfp, c#
if (ke.Key == Key.Up || ke.Key == Key.Down)
ke.Handled = false;// need a method in here
How can I increase the value in the textbox using arrow keys?
Upvotes: -4
Views: 1433
Reputation: 19
If using the Syncfusion library is possible, an IntegerTextBox
which uses arrow buttons to in-/decrease the value could be used. Minimal and maximal values can be defined in this element too.
<Window x:Class="ConTecBrowser.Example"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
mc:Ignorable="d"
Title="Example" Height="450" Width="800">
<Grid>
<syncfusion:IntegerTextBox ShowSpinButton="True" MinValue="1"/>
</Grid>
</Window>
Upvotes: 0
Reputation: 612
The texbox in WPF
<TextBox Name="textbox1" HorizontalAlignment="Left" Text="0" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Margin="154,138,0,0" PreviewKeyDown="TextBox_KeyDown"/>
CodeBehind
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
int currentNumber = Convert.ToInt32(textbox1.Text);
if (e.Key == Key.Up)
{
textbox1.Text = (currentNumber + 1).ToString();
}
else if (e.Key == Key.Down)
{
textbox1.Text = (currentNumber - 1).ToString();
}
}
Upvotes: 2