I'm having trouble getting text from a textbox in c#/xaml. I am running 2 methods - the first which creates a stackpanel and adds 2 textboxes to it and the second is intended to just take the text from the 2 textboxes and assign it to a class object I have defined elsewhere. However - when I try to get the textbox.text, it says it doesn't recognise the variable name I have used for the textbox object. Can anyone offer any clue as to what I'm doing wrong? Here's my code.
public void createstackpanel()
{
StackPanel myStackPanel = new StackPanel();
myStackPanel.Orientation = Windows.UI.Xaml.Controls.Orientation.Vertical;
MyTextBoxTextClass Text1 = new MyTextBoxTextClass ();
TextBox tb1 = new TextBox();
TextBox tb2 = new TextBox();
tb1.Text = "My TextBox 1 Text";
tb2.Text = "My TextBox 2 Text";
myStackPanel.Children.Add(tb1);
myStackPanel.Children.Add(tb2);
}
private void CreateStackPanelButton_Click(object sender, RoutedEventArgs e)
{
//This gets pressed first
createstackpanel();
}
private void SendTextToClass_Click(object sender, RoutedEventArgs e)
{
//This gets pressed second. I have created the StoreMyText class elsewhere and it simply contains 2 properties - textbox1 and textbox2 (both strings)
StoreMyText mytext = new StoreMyText();
mytext.textbox1 = tb1.Text;
mytext.textbox2 = tb2.Text;
}
The issue here is that tb1.Text and tb2.Text aren't being recognised. Why?