Search code examples
c#wpfeventstextblock

Textblock array get Text when tapped


I created a Textblock array (for a create keyboard). I would like get the Textblock Text when I tapped one Textblock.

keyboardButtons[i] = new TextBlock();
keyboardButtons[i].Width = 45;
keyboardButtons[i].Height = 55;
keyboardButtons[i].Text = alphabet.Substring(i, 1);
keyboardButtons[i].Foreground = new SolidColorBrush(Colors.White);
keyboardButtons[i].FontSize = 25.0;
keyboardButtons[i].TextAlignment = TextAlignment.Center;
Canvas.SetLeft(keyboardButtons[i], xPos);
Canvas.SetTop(keyboardButtons[i], yPos);
RectCanvas.Children.Add(keyboardButtons[i]);

keyboardButtons[i].Tapped += new TappedEventHandler(ButtonTap);

Tapped method:

void ButtonTap(object sender, TappedRoutedEventArgs e)
{
}

What should I write in ButtonTap method?


Solution

  • A Button control for this was a better choice. You can pass the alphabet as the TAG property of the Button control.

    Following image shows Button B being pressed (just for example). You can always add your own logic to that.

    enter image description here

    Code behind C# file

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
    
        char Key = 'A';
    
        for (int i = 0; i < 10; i++)
        {
            Button button = new Button();
            button.Tag = Key + i;
    
            button.FontSize = 40;
    
            button.Width = 100;
            button.Height = 100;
    
            button.Content = (char)(Key + i);
            button.Click += button_Click;
    
            stackPanel1.Children.Add(button);
        }
    
        for (int i = 10; i < 20; i++)
        {
            Button button = new Button();
            button.Tag = Key + i;
    
            button.FontSize = 40;
    
            button.Width = 100;
            button.Height = 100;
    
            button.Content = (char)(Key + i);
            button.Click += button_Click;
    
            stackPanel2.Children.Add(button);
        }
    
        for (int i = 20; i < 26; i++)
        {
            Button button = new Button();
            button.Tag = Key + i;
    
            button.FontSize = 40;
    
            button.Width = 100;
            button.Height = 100;
    
            button.Content = (char)(Key + i);
            button.Click += button_Click;
    
            stackPanel3.Children.Add(button);
        }
    }
    
    //Now use a switch case to get the TAG property
    private void button_Click(object sender, RoutedEventArgs e)
    {
        var t = (sender as Button).Tag;
    
        char tag = Convert.ToChar(t);
    
        switch (tag)
        {
        case 'A':
    
            console.Text = "You typed " + tag;
    
            //Your code logic
            break;
    
        case 'B':
    
            console.Text = "You typed " + tag;
    
            //Your code logic
            break;
    
        case 'C':
    
            console.Text = "You typed " + tag;
    
            //Your code logic
            break;
    
    //Rest of the alphabets
        }
    }
    

    XAML file

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="150"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Margin="40, 200, 20, 20">
            <StackPanel x:Name="stackPanel1" Orientation="Horizontal">
    
            </StackPanel>
            <StackPanel x:Name="stackPanel2" Orientation="Horizontal">
    
            </StackPanel>
            <StackPanel x:Name="stackPanel3" Orientation="Horizontal">
    
            </StackPanel>
        </StackPanel>
        <TextBox x:Name="console" Height="100" Width="1000" Grid.Row="1" FontSize="50" Text="Press a Button"/>
    </Grid>