C# 为什么可以';是否删除windows 8.1应用程序中的文本框文本?
如何在Windows 8.1应用程序中从文本框中删除文本?? 每当我点击按钮时,它会在文本框中添加“1”:C# 为什么可以';是否删除windows 8.1应用程序中的文本框文本?,c#,windows-8.1,C#,Windows 8.1,如何在Windows 8.1应用程序中从文本框中删除文本?? 每当我点击按钮时,它会在文本框中添加“1”: private void button1_Click(object sender, RoutedEventArgs e) { textBox1.Text += 1; } 但当我使用另一个按钮替换/删除textBox中的所有文本时,它不会删除文本: private void button0_Click(object sender,
private void button1_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += 1;
}
但当我使用另一个按钮替换/删除textBox中的所有文本时,它不会删除文本:
private void button0_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = "";
}
我还使用了textBox1.Text=string.Empty代码>但什么也没发生
这是一个简单的计算器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)
{
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Runtime.InteropServices.WindowsRuntime;
使用Windows基金会;
使用Windows。
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Controls.Primitives;
使用Windows.UI.Xaml.Data;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Navigation;
//空白页项模板被记录在http://go.microsoft.com/fwlink/?LinkId=234238
名称空间App3
{
///
///可以单独使用或在框架内导航到的空页。
///
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
私有无效按钮0\u单击(对象发送方,路由目标)
{
textBox1.Text=“”;
//或者textBox1.Text=string.Empty;//它也不工作!!
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
textBox1.Text+=1;
}
私有无效按钮2\u单击(对象发送者,路由目标)
{
textBox1.Text+=2;
}
私有无效按钮3\u单击(对象发送者,路由目标)
{
textBox1.Text+=3;
}
私有无效按钮5_单击(对象发送者,路由目标)
{
textBox1.Text+=4;
}
私有无效按钮6_单击(对象发送者,路由目标)
{
textBox1.Text+=5;
}
私有无效按钮7_单击(对象发送者,路由目标)
{
textBox1.Text+=6;
}
私有无效按钮9_单击(对象发送者,路由目标)
{
textBox1.Text+=7;
}
私有无效按钮10_单击(对象发送者,路由目标)
{
textBox1.Text+=8;
}
私有无效按钮11_单击(对象发送者,路由目标)
{
textBox1.Text+=9;
}
私有无效按钮13_单击(对象发送者,路由目标)
{
textBox1.Text+=0;
}
私有无效按钮14_单击(对象发送方,路由目标)
{
textBox1.Text+=”;
}
私有无效按钮15_单击(对象发送者,路由目标)
{
textBox1.Text+=“=”;
}
私有无效按钮4_单击(对象发送者,路由目标)
{
textBox1.Text+=“+”;
}
专用无效按钮8_单击(对象发送者,路由目标)
{
textBox1.Text+=“-”;
}
私有无效按钮12_单击(对象发送者,路由目标)
{
textBox1.Text+=“*”;
}
专用无效按钮16_单击(对象发送方,路由目标)
{
textBox1.Text+=“/”;
}
私有void textBox1_TextChanged(对象发送者,textchangedventargs e)
{
}
}
}
因此,当我单击按钮0时,它不会清除文本框
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>
看看Xaml。执行清除的方法是孤立的,没有订阅该方法的单击事件。您需要订阅它:
<Button x:Name="button0" Content="CLEAR" Click="button0_Click"/>
你试过调试吗?这个函数被调用了吗?事实上,这应该是可行的。尝试Console.WriteLine(“空文本框”)代码>甚至调试失败(“测试”)代码>在您的按钮中0\u单击并观看控制台输出。windows应用商店应用程序中没有控制台。当然没有,但是如果您使用Visual Studio进行调试(我猜),您会有一个控制台输出窗口(视图->输出),您可以在其中看到这一点。您还可以调试解决方案(在该行上设置断点)。否则,我们需要更多的代码来帮助您。您的IDE中没有代码吗?对于vs,我相信它默认显示在应用程序的构建上。我附上了完整的代码,现在请确定问题所在。
<Button Content="1" Click="OperationsClick"/>
<Button Content="2" Click="OperationsClick"/>
private void OperationsClick(object sender, RoutedEventArgs e)
{
var btn = sender as Button;
if (btn != null)
textbox1.Text += btn.Content.ToString();
}