Wednesday, 20 July 2016

Custom Keyboard in WPF


XAML:


<Style x:Key="KeyBoardButton" TargetType="Button">
            <Setter Property="FontFamily" Value="Arial-BoldMT"></Setter>
            <Setter Property="FontSize" Value="36"></Setter>
            <Setter Property="Foreground" Value="#071c2c"></Setter>
            <Setter Property="BorderBrush" Value="#071c2c"></Setter>
            <Setter Property="BorderThickness" Value="2"></Setter>
            <Setter Property="Background" Value="#dde1e9"></Setter>
            <Setter Property="Width" Value="85"></Setter>
            <Setter Property="Height" Value="75"></Setter>
            <Setter Property="HorizontalAlignment" Value="Center"></Setter>
            <Setter Property="VerticalAlignment" Value="Center"></Setter>
        </Style>

   <Popup Name="PopUpKeyBoard" Placement="Absolute"  Height="410" Width="270"  HorizontalOffset="490" VerticalOffset="145">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="1*"></RowDefinition>
                        <RowDefinition Height="1*"></RowDefinition>
                        <RowDefinition Height="1*"></RowDefinition>
                        <RowDefinition Height="1*"></RowDefinition>
                        <RowDefinition Height="1*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1*"></ColumnDefinition>
                        <ColumnDefinition Width="1*"></ColumnDefinition>
                        <ColumnDefinition Width="1*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>

                    <Button x:Name="Closeboard" Grid.Row="0" Grid.Column="2" Content="X" Style="{StaticResource KeyBoardButton}" CommandParameter="CLOSE" Click="KeyPadButton_Click" ></Button>
                    <Button x:Name="one" Grid.Row="1" Grid.Column="0" Content="1" Style="{StaticResource KeyBoardButton}" CommandParameter="NUMPAD1"  Click="KeyPadButton_Click"></Button>
                    <Button x:Name="two" Grid.Row="1" Grid.Column="1" Content="2" Style="{StaticResource KeyBoardButton}" CommandParameter="NUMPAD2"  Click="KeyPadButton_Click"></Button>
                    <Button x:Name="three" Grid.Row="1" Grid.Column="2" Content="3" Style="{StaticResource KeyBoardButton}" CommandParameter="NUMPAD3"  Click="KeyPadButton_Click"></Button>
                    <Button x:Name="four" Grid.Row="2" Grid.Column="0" Content="4" Style="{StaticResource KeyBoardButton}" CommandParameter="NUMPAD4"  Click="KeyPadButton_Click"></Button>
                    <Button x:Name="five" Grid.Row="2" Grid.Column="1" Content="5" Style="{StaticResource KeyBoardButton}" CommandParameter="NUMPAD5"  Click="KeyPadButton_Click"></Button>
                    <Button x:Name="six" Grid.Row="2" Grid.Column="2" Content="6" Style="{StaticResource KeyBoardButton}" CommandParameter="NUMPAD6"  Click="KeyPadButton_Click"></Button>
                    <Button x:Name="seven" Grid.Row="3" Grid.Column="0" Content="7" Style="{StaticResource KeyBoardButton}" CommandParameter="NUMPAD7"  Click="KeyPadButton_Click"></Button>
                    <Button x:Name="eight" Grid.Row="3" Grid.Column="1" Content="8" Style="{StaticResource KeyBoardButton}" CommandParameter="NUMPAD8" Click="KeyPadButton_Click"></Button>
                    <Button x:Name="nine" Grid.Row="3" Grid.Column="2" Content="9" Style="{StaticResource KeyBoardButton}" CommandParameter="NUMPAD9" Click="KeyPadButton_Click"></Button>
                    <Button x:Name="zero" Grid.Row="4" Grid.Column="1" Content="0" Style="{StaticResource KeyBoardButton}" CommandParameter="NUMPAD0" Click="KeyPadButton_Click"></Button>
                    <Button x:Name="back" Grid.Row="4" Grid.Column="2" Style="{StaticResource KeyBoardButton}" CommandParameter="BACK" Click="KeyPadButton_Click">
                        <Image Source="..\Images\icn-backarrow@3x.png" Width="40"></Image>
                    </Button>
                </Grid>
            </Popup>



CS Code:

  private TextBox SelectedTextBox;
        private bool GotFocusTextbox=false;


private void OpenKeyboard(TextBox txtBox)
        {
            try
            {
                ostk.CloseKeyboard();
                PopUpKeyBoard.IsOpen = true;
                SelectedTextBox = txtBox;
                GotFocusTextbox = true;
            }
            catch (Exception ex)
            {
                var methodName = new StackTrace(ex).GetFrame(0).GetMethod().Name;
                Errorlog.Errorlog.WriteToErrorLog(ex.Message, ex.StackTrace, "Error", methodName);
            }
        }

     

        private void CloseKeyboard()
        {
            try
            {
                PopUpKeyBoard.IsOpen = false;
                GotFocusTextbox = false;
                SelectedTextBox = null;
            }
            catch (Exception ex)
            {
                var methodName = new StackTrace(ex).GetFrame(0).GetMethod().Name;
                Errorlog.Errorlog.WriteToErrorLog(ex.Message, ex.StackTrace, "Error", methodName);
            }
        }

private void Textbox_GotFocus(object sender, RoutedEventArgs e)
        {        
            OpenKeyboard(sender as TextBox); //Open a keyboard
        }

  private void KeyPadButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Button button = sender as Button;

                switch (button.CommandParameter.ToString())
                {

                    case "CLOSE":
                        CloseKeyboard();
                        break;

                    case "BACK":
                        if (SelectedTextBox.Text.Length > 0)
                            SelectedTextBox.Text = SelectedTextBox.Text.Remove(SelectedTextBox.Text.Length - 1);
                        break;

                    default:
                        if (SelectedTextBox != null)
                        {
                            if (GotFocusTextbox)
                            {
                                SelectedTextBox.Text = "";
                                SelectedTextBox.Text += button.Content.ToString();
                            }
                            else
                            {
                                SelectedTextBox.Text += button.Content.ToString();
                            }
                        }
                        GotFocusTextbox = false;
                        break;
                }
            }
            catch (Exception ex)
            {
                var methodName = new StackTrace(ex).GetFrame(0).GetMethod().Name;
                Errorlog.Errorlog.WriteToErrorLog(ex.Message, ex.StackTrace, "Error", methodName);
            }
        }


No comments:

Post a Comment