1

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?

2 Answers 2

1

Declare

TextBox tb1;
TextBox tb2;

outside the createstackpanel() function in the Class level.

and initialize

tb1 = new TextBox();
tb2 = new TextBox();

inside the createstackpanel() function.

Sign up to request clarification or add additional context in comments.

2 Comments

As per comment below - the idea of that button is that the user is able to keep on creating new textboxes (which wont have default text, but text they enter themselves). Therefore i want the user to have control over how many text boxes are made by how many times they click that button
Updated my answer. Please, refer to that.
1

tb1 and tb2 declared in createstackpanel method. They can't accessed in SendTextToClass_Click method.

P.S. I think it's not doog idea to use dynamically created textboxes in this situation. What is a final goal of your code?

List of textboxes sample:

// class level declaration:
List<TextBox> textboxes = new List<TextBox>();

// createstackpanel method:
textboxes.Add(new TextBox() { Text = "textbox #1" });
textboxes.Add(new TextBox() { Text = "textbox #2" });

// SendTextToClass_Click method:
// some operation with textboxes list

5 Comments

So how do i get their information, on a button click for example? Is their any way to make that data public? (i assumed creating the method as a pubilc method might do that...i was wrong :) / :( )
For example, you can declare these textboxes in a class level.
The problem is that i want to keep creating them dynamically. The idea of that button is that the user is able to keep on creating new textboxes (which wont have default text, but text they enter themselves). Therefore i want the user to have control over how many text boxes are made by how many times they click that button
Use List<TextBox> and add new textboxex to it.
@General-Dommer. To answer your Q, the app i am creating for learning purposes is a recipe app that takes ingredients and their nutritional value (carbs, protein etc). Therefore, for every additional ingredient, i have to add new textboxes (in fact, i add 8 new textboxes to new stackpanel). As i wont know how many ingredients the user wants to enter, i have to create them dynamically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.