C# Taschenrechner Key-Down

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • C# Taschenrechner Key-Down

    Hey Leute,
    ich bin jetzt dabei, einen Taschenrechner zu bauen.

    Hier mein XAML:



    Source Code

    1. <Window x:Class="WpfApplication2.MainWindow"
    2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    6. xmlns:local="clr-namespace:Taschenrechner_3"
    7. mc:Ignorable="d"
    8. Title="Rechner" Height="350" Width="265" ResizeMode="CanMinimize" KeyDown="Window_KeyDown">
    9. <Grid>
    10. <Grid.RowDefinitions>
    11. <!--Zeilen-->
    12. <RowDefinition Height="16"/>
    13. <RowDefinition Height="32" />
    14. <RowDefinition Height="10" />
    15. <RowDefinition Height="40" />
    16. <RowDefinition Height="10" />
    17. <RowDefinition Height="40"/>
    18. <RowDefinition Height="10"/>
    19. <RowDefinition Height="40"/>
    20. <RowDefinition Height="10"/>
    21. <RowDefinition Height="40"/>
    22. <RowDefinition Height="10"/>
    23. <RowDefinition Height="40"/>
    24. <RowDefinition Height="10"/>
    25. </Grid.RowDefinitions>
    26. <Grid.ColumnDefinitions>
    27. <ColumnDefinition Width="28" />
    28. <ColumnDefinition Width="40" />
    29. <ColumnDefinition Width="10"/>
    30. <ColumnDefinition Width="40" />
    31. <ColumnDefinition Width="10" />
    32. <ColumnDefinition Width="40"/>
    33. <ColumnDefinition Width="10"/>
    34. <ColumnDefinition Width="40"/>
    35. <ColumnDefinition Width="20"/>
    36. </Grid.ColumnDefinitions>
    37. <Button x:Name="eins" Content="1" FontSize="16" FontWeight="Bold" Grid.Column="1" Grid.Row="3" IsTabStop="False" Focusable="False" Click="eins_click" Cursor="Hand" BorderBrush="Black" KeyDown="eins_KeyDown"/>
    38. <Button x:Name="zwei" Content="2" FontSize="16" FontWeight="Bold" Grid.Column="3" Grid.Row="3" Click="zwei_Click" IsTabStop="False" Focusable="False" Cursor="Hand" BorderBrush="Black"/>
    39. <Button x:Name="drei" Content="3" FontSize="16" FontWeight="Bold" Grid.Column="5" Grid.Row="3" Click="drei_Click" IsTabStop="False" Focusable="False" Cursor="Hand" BorderBrush="Black"/>
    40. <Button x:Name="plus" Content="+" FontSize="16" FontWeight="Bold" Grid.Column="7" Grid.Row="3" Click="plus_Click" IsTabStop="False" Focusable="False" Cursor="Hand" BorderBrush="Black"/>
    41. <Button x:Name="vier" Content="4" FontSize="16" FontWeight="Bold" Grid.Column="1" Grid.Row="5" Click="vier_Click" IsTabStop="False" Focusable="False" Cursor="Hand" BorderBrush="Black"/>
    42. <Button x:Name="fünf" Content="5" FontSize="16" FontWeight="Bold" Grid.Column="3" Grid.Row="5" Click="fünf_Click" IsTabStop="False" Focusable="False" Cursor="Hand" BorderBrush="Black"/>
    43. <Button x:Name="sechs" Content="6" FontSize="16" FontWeight="Bold" Grid.Column="5" Grid.Row="5" Click="sechs_Click" IsTabStop="False" Focusable="False" Cursor="Hand" BorderBrush="Black"/>
    44. <Button x:Name="minus" Content="-" FontSize="16" FontWeight="Bold" Grid.Column="7" Grid.Row="5" Click="minus_Click" IsTabStop="False" Focusable="False" Cursor="Hand" BorderBrush="Black"/>
    45. <Button x:Name="sieben" Content="7" FontSize="16" FontWeight="Bold" Grid.Column="1" Grid.Row="7" Click="sieben_Click" IsTabStop="False" Focusable="False" Cursor="Hand" BorderBrush="Black"/>
    46. <Button x:Name="acht" Content="8" FontSize="16" FontWeight="Bold" Grid.Column="3" Grid.Row="7" Click="acht_Click" IsTabStop="False" Focusable="False" Cursor="Hand" BorderBrush="Black"/>
    47. <Button x:Name="neun" Content="9" FontSize="16" FontWeight="Bold" Grid.Column="5" Grid.Row="7" Click="neun_Click" IsTabStop="False" Focusable="False" Cursor="Hand" BorderBrush="Black"/>
    48. <Button x:Name="mal" Content="*" FontSize="16" FontWeight="Bold" Grid.Column="7" Grid.Row="7" Click="mal_Click" IsTabStop="False" Focusable="False" Cursor="Hand" BorderBrush="Black"/>
    49. <Button x:Name="clean" Content="C" FontSize="16" FontWeight="Bold" Grid.Column="1" Grid.Row="9" Click="clean_Click" IsTabStop="False" Focusable="False" Cursor="Hand" IsCancel="True" BorderBrush="Black"/>
    50. <Button x:Name="null" Content="0" FontSize="16" FontWeight="Bold" Grid.Column="3" Grid.Row="9" Click="null_Click" IsTabStop="False" Focusable="False" Cursor="Hand" BorderBrush="Black"/>
    51. <Button x:Name="komma" Content="." FontSize="16" FontWeight="Bold" Grid.Column="5" Grid.Row="9" Click="komma_Click" IsTabStop="False" Focusable="False" Cursor="Hand" BorderBrush="Black"/>
    52. <Button x:Name="teil" Content="/" FontSize="16" FontWeight="Bold" Grid.Column="7" Grid.Row="9" Click="teil_Click" IsTabStop="False" Focusable="False" Cursor="Hand" BorderBrush="Black"/>
    53. <Button x:Name="gleich" Content="=" FontSize="16" FontWeight="Bold" Grid.Column="1" Grid.ColumnSpan="3" Grid.Row="11" Cursor="Hand" Click="gleich_Click" IsDefault="True" BorderBrush="Black"/>
    54. <Button x:Name="lösch" Content="f" FontSize="16" FontWeight="Bold" FontFamily="Wingdings 3" Grid.Column="5" Grid.ColumnSpan="3" Grid.Row="11" Click="lösch_Click" IsTabStop="False" Focusable="False" Cursor="Hand" BorderBrush="Black"/>
    55. <TextBox x:Name="rechner" BorderBrush="Black" Background="Cyan" FontSize="17" HorizontalContentAlignment="Right" Grid.Column="2" Grid.ColumnSpan="6" Grid.Row="1" TextAlignment="Right" MaxLines="1" FontFamily="Verdana" FontWeight="Bold"/>
    56. </Grid>
    57. </Window>
    Display All




    Und hier mein C#:



    C Source Code

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. using System.Windows;
    7. using System.Windows.Controls;
    8. using System.Windows.Data;
    9. using System.IO;
    10. using System.Globalization;
    11. using System.Deployment;
    12. using System.Collections;
    13. using System.Windows.Documents;
    14. using System.Windows.Input;
    15. using System.Windows.Media;
    16. using System.Windows.Media.Imaging;
    17. using System.Windows.Navigation;
    18. using System.Windows.Shapes;
    19. namespace WpfApplication2
    20. {
    21. /// <summary>
    22. /// Interaktionslogik für MainWindow.xaml
    23. /// </summary>
    24. public partial class MainWindow : Window
    25. {
    26. public MainWindow()
    27. {
    28. InitializeComponent();
    29. }
    30. private void eins_click(object sender, RoutedEventArgs e)
    31. {
    32. rechner.Text += "1";
    33. }
    34. private void zwei_Click(object sender, RoutedEventArgs e)
    35. {
    36. rechner.Text += "2";
    37. }
    38. private void drei_Click(object sender, RoutedEventArgs e)
    39. {
    40. rechner.Text += "3";
    41. }
    42. private void plus_Click(object sender, RoutedEventArgs e)
    43. {
    44. rechner.Text += "+";
    45. }
    46. private void vier_Click(object sender, RoutedEventArgs e)
    47. {
    48. rechner.Text += "4";
    49. }
    50. private void fünf_Click(object sender, RoutedEventArgs e)
    51. {
    52. rechner.Text += "5";
    53. }
    54. private void sechs_Click(object sender, RoutedEventArgs e)
    55. {
    56. rechner.Text += "6";
    57. }
    58. private void minus_Click(object sender, RoutedEventArgs e)
    59. {
    60. rechner.Text += "-";
    61. }
    62. private void sieben_Click(object sender, RoutedEventArgs e)
    63. {
    64. rechner.Text += "7";
    65. }
    66. private void acht_Click(object sender, RoutedEventArgs e)
    67. {
    68. rechner.Text += "8";
    69. }
    70. private void neun_Click(object sender, RoutedEventArgs e)
    71. {
    72. rechner.Text += "9";
    73. }
    74. private void mal_Click(object sender, RoutedEventArgs e)
    75. {
    76. rechner.Text += "*";
    77. }
    78. private void clean_Click(object sender, RoutedEventArgs e)
    79. {
    80. rechner.Text = "";
    81. }
    82. private void null_Click(object sender, RoutedEventArgs e)
    83. {
    84. rechner.Text += "0";
    85. }
    86. private void komma_Click(object sender, RoutedEventArgs e)
    87. {
    88. rechner.Text += ",";
    89. }
    90. private void teil_Click(object sender, RoutedEventArgs e)
    91. {
    92. rechner.Text += "/";
    93. }
    94. private void lösch_Click(object sender, RoutedEventArgs e)
    95. {
    96. rechner.Text = rechner.Text.Remove(rechner.Text.Length - 1, 1);
    97. }
    98. private void gleich_Click(object sender, RoutedEventArgs e)
    99. {
    100. rechner.Text += "Text1";
    101. }
    102. private void Window_KeyDown(object sender, KeyEventArgs e)
    103. {
    104. rechner.Text += e.Key.ToString(rechner.Text = "Text2");
    105. }
    106. private void eins_KeyDown(object sender, KeyEventArgs e)
    107. {
    108. if (e.Key == Key.NumPad1)
    109. {
    110. rechner.Text = "Text3";
    111. }
    112. }
    113. }
    114. }
    Display All
    Ich möchte, dass ich ohne in die TextBox zu klicken (also per NumPad und wie im Windows-Taschenrechner) Zahlen hineinschreiben kann. Benötige dringend Hilfe. Danke

    Euer Kirk

    P.S.: Es wäre auch toll, wenn Ihr mir in meinem anderen Thema (ComboBox-Inhalt an DataGrid senden) helft. <3 :thumbsup: :love: :thumbup:

    The post was edited 1 time, last by Kirk1701A ().