2014-10-09 50 views
-3

我正在編寫一個程序,顯示2個文本框和一個標籤。 應該發生什麼;當單擊文本框1時,標籤顯示「輸入您的全名」 單擊文本框2時,標籤顯示「輸入您的電話號碼和區號」單擊visual studio中的事件

幫助? 我有這個迄今爲止

私人小組textbox1_click(發送者爲對象,E作爲EventArgs的)把手TextBox1.Click,TextBox1.Enter Label1.Text = 「請輸入您的全名」 結束小組

Private Sub TextBox2_Click(sender As Object, e As EventArgs) Handles TextBox2.Click, TextBox2.Enter 
    Label1.Text = "Enter your phone number and area code" 
End Sub 
+0

沒有一個真正的問題在這裏,是嗎? – 2014-10-09 22:46:51

+0

對我來說不幸的是,我的Visual Studio老師根本沒有幫助。他認爲他的時間比我的成績更有價值。 – 2014-10-09 22:49:58

+0

我設置了表單,除了幾行外,其他所有內容都已完成。我不知道如何在單擊文本框時使標籤更改文本 – 2014-10-09 22:50:44

回答

1

這裏是基本的WPF示例,請參閱代碼並嘗試完成您的示例。

View實現

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

<StackPanel> 
    <Label x:Name="lblLabel"/> 
    <TextBox x:Name="txtFullName" GotFocus="TextBox_GotFocus" /> 
    <TextBox x:Name="txtPhoneNumber" GotFocus="TextBox_GotFocus" /> 
    <Button x:Name="btnOk" Content="OK" Click="btnOk_Click"/> 
</StackPanel> 

代碼隱藏

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void TextBox_GotFocus(object sender, RoutedEventArgs e) 
    { 
     var textBox = (TextBox)sender; 

     string labelText = ""; 

     if (textBox.Name == "txtFullName") 
     { 
      labelText = "Enter your full name."; 
     } 

     if (textBox.Name == "txtPhoneNumber") 
     { 
      labelText = "Enter your phone number and area code."; 
     } 

     lblLabel.Content = labelText; 
    } 

    private void btnOk_Click(object sender, RoutedEventArgs e) 
    { 
     MessageBox.Show(string.Format("Full name: {0}, phone number: {1}", txtFullName.Text, txtPhoneNumber.Text)); 
    } 
}