【问题标题】:how to reset the value of a radio button in windows phone 7 application using c#如何使用 C# 在 Windows Phone 7 应用程序中重置单选按钮的值
【发布时间】:2014-02-14 07:13:09
【问题描述】:

我的 windows phone 7 应用程序中有一个表单,其中包含一些文本框和一个单选按钮。我想在单击重置按钮时重置文本框和单选按钮的值。我能够清除文本框,但不知道如何清除单选按钮的值。请帮忙:

我的表格是:

<TextBox GotFocus="OnGotFocus" Canvas.Left="6" Canvas.Top="6" Height="74" Name="name" Text="*Name" Width="453" BorderThickness="0"/>
            <TextBox GotFocus="OnGotFocus1" Canvas.Left="6" Canvas.Top="66" Height="74" Name="age" Text="*Age" Width="453" BorderThickness="0" />
            <TextBlock Canvas.Left="20" Canvas.Top="157" Height="44" Name="gen" Text="Gender" Foreground="Black" FontFamily="Verdana" FontSize="24" Width="134" />
            <RadioButton Canvas.Left="139" Canvas.Top="157" FontStyle="Italic" Foreground="Black" Content="Male" Height="71" Name="male" Width="154" />
            <RadioButton Canvas.Left="139" Canvas.Top="207"  FontStyle="Italic" Foreground="Black" Content="Female" Height="71" Name="fem" Width="140" />
            <TextBox GotFocus="OnGotFocus2" Canvas.Left="6" Canvas.Top="267" Height="74" Name="sadd" Text="*Street Address" Width="453" BorderThickness="0"/>
            <TextBox GotFocus="OnGotFocus3" Canvas.Left="6" Canvas.Top="327" Height="74" Name="cadd" Text="*City Address" Width="453" BorderThickness="0"/>
            <TextBox GotFocus="OnGotFocus4" Canvas.Left="6" Canvas.Top="387" Height="74" Name="eadd" Text="*Email Address" Width="453" BorderThickness="0"/>
            <TextBox GotFocus="OnGotFocus5" Canvas.Left="6" Canvas.Top="447" Height="74" Name="phn" Text="*Phone" Width="453" BorderThickness="0"/>
            <TextBox GotFocus="OnGotFocus6" Canvas.Left="6" Canvas.Top="507" Height="74" Name="zip" Text="*Zip Code" Width="453" BorderThickness="0"/>

点击重置按钮时我的重置代码是:

private void reset_Click(object sender, RoutedEventArgs e)
    {
        name.Text = "";
        age.Text  = "";
        sadd.Text = "";
        cadd.Text = "";
        eadd.Text = "";
        phn.Text  = "";
        zip.Text  = "";
    }

请添加我应该在此处添加的代码以重置单选按钮

现在点击提交按钮,如果我想检查是否选择了男性或女性,我该怎么做。对于文本框,我正在执行以下代码:

   private void submit_Click(object sender, RoutedEventArgs e)
    {
        if (name.Text == "")
        {
            MessageBox.Show("Please Enter the name");
            name.Focus();
        }


    }

【问题讨论】:

    标签: c# windows-phone-7


    【解决方案1】:

    有一个名为 content 的属性,它相当于一个文本框文本属性

    这就是你要设置它的方式

    male.IsChecked=false;
    male.Content=String.Empty;
    

    关于编辑。

    为两个单选按钮分配一个共同的 groupname 属性。即

    <RadioButton Canvas.Left="139" Canvas.Top="157" FontStyle="Italic" GroupName="Gender" Foreground="Black" Content="Male" Height="71" Name="male" Width="154" />
    <RadioButton Canvas.Left="139" Canvas.Top="207"  FontStyle="Italic" GroupName="Gender" Foreground="Black" Content="Female" Height="71" Name="fem" Width="140" />
    

    这使得每次在 xaml 中选择这两个中的任何一个。

    现在在代码中获取单选按钮的值应该是这样的。

    if(male.IsChecked==true)
    {
    string gender=male.Content.ToString();
    }
    else if(female.isChecked==true)
    {
    string gender=female.Content.ToString();
    }
    else    //none of them is selected.
    {
     MessageBox.Show("Text");
    }
    

    【讨论】:

    • 请看我编辑的代码。我想验证该字段,即是否选择了男性或女性。
    • 不应使用提示,name.Text == "",而应使用 name.Text==String.Empty。分配也一样。
    • 我想在此处添加一个消息框,请选择您的性别。我将如何验证性别字段
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多