2014-02-14 38 views
0

我在我的windows phone 7應用程序中有一個窗體,其中包含幾個文本框和一個單選按鈕。我想重置單擊復位按鈕上的文本框和單選按鈕的值。我能夠清除文本框,但不知道如何清除單選按鈕的值。請幫助:如何重置使用c的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(); 
     } 


    } 

回答

1

有一個屬性命名的內容,相當於一個文本框的文本屬性

這就是你如何會集它

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

並關於編輯。

爲兩個單選按鈕分配一個公共組名屬性。即

<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"); 
} 
+0

請看我編輯的代碼。我想驗證該領域,即是否選擇男性或女性。 – bhaku

+1

檢查編輯。 –

+1

和提示,不應使用name.Text ==「」,而應使用name.Text == String.Empty。同樣也適用於分配。 –

相關問題