简体   繁体   English

为什么我无法在 Windows 8.1 应用程序中删除 TextBox 文本?

[英]Why can't I remove TextBox text in windows 8.1 app?

How do i remove text from a TextBox in Windows 8.1 App??如何从 Windows 8.1 应用程序中的 TextBox 中删除文本? Whenever I click on the button, it adds "1" to the textbox :每当我单击按钮时,它都会向文本框添加“1”:

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 :但是当我使用另一个按钮替换/删除 textBox 中的所有文本时,它不会删除文本:

private void button0_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text = "";

        }

I also used textBox1.Text = string.Empty;我也用过textBox1.Text = string.Empty; but nothing happens.但什么也没有发生。

Its a simple calculator windows 8.1 store app, Complete Code is given below :它是一个简单的计算器 Windows 8.1 商店应用程序,完整代码如下:

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.因此,当我单击 button0 时,它不会清除文本框。

MainPage.xaml:主页.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>

Look at the Xaml.看看 Xaml。 The method to do the clear is orphaned and doesn't have a Click event subscribed to that method .执行清除的方法是孤立的,并且没有订阅该方法Click事件。 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: 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在Windows Phone 8.1应用程序中将RichEditBox的InputScope设置为“ Text” - Can't set InputScope of RichEditBox to “Text” in Windows Phone 8.1 app 为什么我不能从C#向Windows Phone 8.1发送WNS吐司通知 - why can't I send WNS toast notification from c# to windows phone 8.1 无法从我的Windows 8.1应用程序发送HTTP请求 - Can't send HTTP requests from my Windows 8.1 app 在Windows 8.1应用程序中无法获取剪贴板的内容 - Can't get contents of the Clipboard in Windows 8.1 app 为什么Windows 8.1程序包中不能使用System.Runtime? - Why can't System.Runtime be used in Windows 8.1 packages? 如何在Windows 10 UWP应用程序中删除TextBox控件的默认“粘贴”上下文菜单项? - How can I remove the default “Paste” context menu entry of a TextBox control in a Windows 10 UWP app? 我可以在Windows Phone 8.1应用程序中使用Motion类吗? - Can I use the Motion class in a Windows Phone 8.1 app? 如何在Windows 8.1应用程序中对XAML网格视图进行排序? - How can I sort a XAML gridview in a Windows 8.1 app? 如何为Windows 8.1应用程序创建嵌套的TreeView? - How can I create a nested TreeView for Windows 8.1 app? 我无法使用Windows Phone 8.1的IsolatedStorageSettings - I can't use IsolatedStorageSettings for windows phone 8.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM