Reputation: 41
How do i remove text from a TextBox in Windows 8.1 App?? Whenever I click on the button, it adds "1" to the textbox :
private void button1_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += 1;
}
But when i use another button to replace/remove all text from textBox, it doesn't remove the text :
private void button0_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = "";
}
I also used textBox1.Text = string.Empty;
but nothing happens.
Its a simple calculator windows 8.1 store app, Complete Code is given below :
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace App3
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void button0_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = "";
//or textBox1.Text = string.Empty; //It also doesn't work!!
}
private void button1_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += 1;
}
private void button2_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += 2;
}
private void button3_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += 3;
}
private void button5_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += 4;
}
private void button6_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += 5;
}
private void button7_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += 6;
}
private void button9_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += 7;
}
private void button10_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += 8;
}
private void button11_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += 9;
}
private void button13_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += 0;
}
private void button14_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += ".";
}
private void button15_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += "=";
}
private void button4_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += "+";
}
private void button8_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += "-";
}
private void button12_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += "*";
}
private void button16_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += "/";
}
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
}
}
}
So when I click button0, It does not clear the textbox.
MainPage.xaml:
<Page
x:Class="App3.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App3"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Rectangle Fill="#FF707081" HorizontalAlignment="Left" Height="483" Margin="440,59,0,0" Stroke="Black" VerticalAlignment="Top" Width="456"/>
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="41" Margin="459,135,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="416" FontSize="24" FontFamily="Times New Roman" BorderBrush="#FF1F4BE2" TextChanged="textBox1_TextChanged" Text="Hello World"/>
<Button x:Name="button2" Content="2" HorizontalAlignment="Left" Height="43" Margin="562,274,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button2_Click"/>
<Button x:Name="button3" Content="3" HorizontalAlignment="Left" Height="43" Margin="668,274,0,0" VerticalAlignment="Top" Width="107" RenderTransformOrigin="0.495,1.465" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button3_Click"/>
<Button x:Name="button5" Content="4" HorizontalAlignment="Left" Height="43" Margin="456,340,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button5_Click"/>
<Button x:Name="button6" Content="5" HorizontalAlignment="Left" Height="43" Margin="562,340,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button6_Click"/>
<Button x:Name="button7" Content="6" HorizontalAlignment="Left" Height="43" Margin="668,340,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button7_Click"/>
<Button x:Name="button9" Content="7" HorizontalAlignment="Left" Height="43" Margin="456,410,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button9_Click"/>
<Button x:Name="button1" Content="1" HorizontalAlignment="Left" Height="43" Margin="456,274,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button1_Click"/>
<Button x:Name="button10" Content="8" HorizontalAlignment="Left" Height="43" Margin="562,410,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button10_Click"/>
<Button x:Name="button11" Content="9" HorizontalAlignment="Left" Height="43" Margin="668,410,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button11_Click"/>
<Button x:Name="button13" Content="0" HorizontalAlignment="Left" Height="43" Margin="456,477,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button13_Click"/>
<Button x:Name="button14" Content="." HorizontalAlignment="Left" Height="43" Margin="562,477,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button14_Click"/>
<Button x:Name="button15" Content="=" HorizontalAlignment="Left" Height="43" Margin="668,477,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button15_Click"/>
<Button x:Name="button0" Content="CLEAR" HorizontalAlignment="Left" Height="46" Margin="459,199,0,0" VerticalAlignment="Top" Width="419" Background="#FF1D1818" BorderBrush="#FF0D22F7"/>
<Button x:Name="button4" Content="+" HorizontalAlignment="Left" Height="43" Margin="786,274,0,0" VerticalAlignment="Top" Width="92" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button4_Click"/>
<Button x:Name="button8" Content="-" HorizontalAlignment="Left" Height="43" Margin="786,340,0,0" VerticalAlignment="Top" Width="92" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button8_Click"/>
<Button x:Name="button12" Content="*" HorizontalAlignment="Left" Height="43" Margin="786,410,0,0" VerticalAlignment="Top" Width="92" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button12_Click"/>
<Button x:Name="button16" Content="/" HorizontalAlignment="Left" Height="43" Margin="786,477,0,0" VerticalAlignment="Top" Width="92" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button16_Click"/>
<TextBlock HorizontalAlignment="Left" Height="30" Margin="462,76,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="413" FontSize="24" FontFamily="Times New Roman" Foreground="#FF718BE4">
<Run Text=" "/>
<Run Foreground="#FF0A0AE0" Text="Hussam's First Calculator"/>
</TextBlock>
</Grid>
</Page>
Upvotes: 2
Views: 111
Reputation: 31721
Look at the Xaml. The method to do the clear is orphaned and doesn't have a Click
event subscribed to that method. You need to subscribe to it:
<Button x:Name="button0" Content="CLEAR" Click="button0_Click"/>
As an aside for most of the operations you could change them to call one centralized method which pulls the necessary text from the button which is calling it:
XAML:
<Button Content="1" Click="OperationsClick"/>
<Button Content="2" Click="OperationsClick"/>
CodeBehind
private void OperationsClick(object sender, RoutedEventArgs e)
{
var btn = sender as Button;
if (btn != null)
textbox1.Text += btn.Content.ToString();
}
Upvotes: 1