Mohamed Thaufeeq
Mohamed Thaufeeq

Reputation: 1677

c# Wp8: error in getting textbox entered value?

It says createPassword and repeatPassword are not in the current context. Why and what happens ?

Code;

public MainPage()
    {
        InitializeComponent();
        TextBox createPassword = new TextBox();
        createPassword.Width = 400;
        TextBox repeatPassword = new TextBox();
        repeatPassword.Width = 400;
        Button createButton = new Button();
        createButton.Content = "Create New Password";
        createButton.Click += new RoutedEventHandler(savePassword);
        StackPanel content = new StackPanel();
        content.HorizontalAlignment = HorizontalAlignment.Center;
        content.Children.Add(createPassword);
        content.Children.Add(repeatPassword);
        content.Children.Add(createButton);
        LayoutRoot.Children.Add(content);
        }
        void savePassword(object sender, RoutedEventArgs e)
        {
            string password1 = createPassword.Text;
            string password2 = repeatPassword.Text;
        }

Upvotes: 0

Views: 583

Answers (1)

Alex F
Alex F

Reputation: 43331

createPassword and repeatPassword must be class members to use them in different class methods:

TextBox createPassword;
TextBox repeatPassword;

public MainPage()
{
    InitializeComponent();
    createPassword = new TextBox();
    createPassword.Width = 400;
    repeatPassword = new TextBox();
    repeatPassword.Width = 400;
    Button createButton = new Button();
    createButton.Content = "Create New Password";
    createButton.Click += new RoutedEventHandler(savePassword);
    StackPanel content = new StackPanel();
    content.HorizontalAlignment = HorizontalAlignment.Center;
    content.Children.Add(createPassword);
    content.Children.Add(repeatPassword);
    content.Children.Add(createButton);
    LayoutRoot.Children.Add(content);
}
void savePassword(object sender, RoutedEventArgs e)
{
    string password1 = createPassword.Text;
    string password2 = repeatPassword.Text;
}

Upvotes: 1

Related Questions