Shahzaib Ali Khan
Shahzaib Ali Khan

Reputation: 75

Creating run time textbox in Windows Phone 7

I wanted to display no of course json objects in each textbox but as they are unpredictable number of objects therefore i created textbox on the fly using this code

List<Course> Cdata = JsonConvert.DeserializeObject<List<Course>>(App.data);
            TextBox[] Tblock = new TextBox[Cdata.Count];
            double top = 0; int i = 0;

            foreach (Course de in Cdata)
            {
                result += de.course_name + "\r\n";
                result += "Total Absents = " + de.absents;
                result += " + " + de.presents;
                result += " = " + de.sessions + "\r\n\r\n\r\n";


                Tblock[i] = new TextBox();
                Tblock[i].Text = result;
                Tblock[i].AcceptsReturn = true;
                Tblock[i].TextWrapping = TextWrapping.Wrap;
                Tblock[i].Width = 475;
                Tblock[i].Height = 270;
                Tblock[i].IsReadOnly = true;
                Tblock[i].Margin =new Thickness (0,top,0,0);
                Tblock[i].Visibility = System.Windows.Visibility.Visible;
                Tblock[i].VerticalAlignment = System.Windows.VerticalAlignment.Top;
                top += 270; i++;

                result = "";

            }

Now when i debug my app data it is working as its supposed to the only problem is textbox

never display on View

and i haven't coded any textbox in Xaml file of view Thanks in Advance

Upvotes: 0

Views: 848

Answers (1)

nkchandra
nkchandra

Reputation: 5557

You have to add the Textboxes to any existing Panel (generally to Grid or StackPanel) in the XAML as shown below

StackPanel sp = new StackPanel(); //Create stack panel before foreach loop
foreach (Course de in Cdata)
{
   //your code which you shown above
   sp.Children.Add(Tblock[i]); //Add all the Textboxes to the stackpanel
}

ContentPanel.Children.Add(sp); //And add the above stackpanel to the existing Grid named ContentPanel

By the way, I suggest you to use a ListBox with ItemTemplate to bind the data instead of creating the TextBoxes as shown above.

Also, I don't understand why you have choosen TextBox instead of TextBlock to display data

Upvotes: 2

Related Questions