Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8524635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:44:38+00:00 2026-06-11T07:44:38+00:00

I have created a stack panel and added that in a list box .

  • 0

I have created a stack panel and added that in a list box .
Stack panel contains two text blocks , 1st textblock text is id and other’s text is name .
Comes from database . I want to delete stack panel of specific id .
Here is the code:

public partial class MainPage : PhoneApplicationPage
    {
        StackPanel stackpanel2 = null;
        Border br = null;
        TextBox tb1 = null;
        TextBox tb2 = null;
        string str1 = null;
        TextBlock tblk1 = null;
        TextBlock tblk2 = null;
        ListBox lb = null;

        public MainPage()
        {
            InitializeComponent();           
            createListBox();

        }
        public void createListBox()
        {
            updateDatabase();
            deleteRow(i);  
            lb = new ListBox();
            lb.Margin = new Thickness();
            lb.Height = double.NaN;
            lb.Width = double.NaN;            
            ContentPanel.Children.Add(lb);
            int rowCount = noofrows();
            for (int i = 1; i <= rowCount; i++)
            {

                createStackPanel(i);


                createTextBlock1(i);
                createTextBlock2(i);

                lb.Items.Add(br);
            }               

        }
        public void createTextBlock1(int y )
        {
            tblk1 = new TextBlock();
            tblk1.Height = 80;
            tblk1.Width = 150;
            tblk1.FontSize = 30;
            tblk1.Foreground = new SolidColorBrush(Colors.White);
           // tblk1.Margin = new Thickness();
            tblk1.Text = returnID(y);          
            stackpanel2.Children.Add(tblk1);
        }
        public void createTextBlock2(int a)
        {
            tblk2 = new TextBlock();
            tblk2.Height = 80;
            tblk2.Width = 150;
            tblk2.FontSize = 30;
            tblk2.Foreground = new SolidColorBrush(Colors.White);
            //tblk2.Margin = new Thickness();
            tblk2.Text = SelectName(a);
            stackpanel2.Children.Add(tblk2);
        }
        private void deleteRow(int x)
        {
            deleteStackPanel(x);
            string str2 = "delete  from Details where id =" + x;
            (Application.Current as App).db.SelectList(str2);

        }
        private void updateDatabase()
        {
            string str1 = "insert into Details (id,name,age,contact) values('1','sanjay','20','4444')";
            (Application.Current as App).db.SelectList(str1);
        }

        void createStackPanel(int c)
        {
            br = new Border();
            br.BorderBrush = new SolidColorBrush(Colors.Green);
            br.BorderThickness = new Thickness(5);
            stackpanel2 = new StackPanel();
            stackpanel2.Height = 100;
            stackpanel2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            stackpanel2.Margin = new Thickness();
            stackpanel2.Orientation = System.Windows.Controls.Orientation.Horizontal;
            stackpanel2.Background = new SolidColorBrush(Colors.Blue);
            br.Child = stackpanel2;
            stackpanel2.Tap += (s, e) =>
                {
                    RowValue.rowcount = c ;
                    NavigationService.Navigate(new Uri("/Details.xaml", UriKind.Relative));
                };

        }
        public String SelectName(int x)
        {

            return Convert.ToString((Application.Current as App).db.SelectList("select name from details where id ="+x));
        }
        public string returnID(int z)
        {
            return Convert.ToString((Application.Current as App).db.SelectList("select id from details where id ="+z));
        }
        public Int32 noofrows()
        {
            int b = Convert.ToInt32((Application.Current as App).db.SelectList("select count(*) from details"));
            return b;
        }
        public void  deleteStackPanel(int x)
        {
            lb.Items.Remove(stackpanel2);         
        }

    }

    public static class RowValue
    {
        public static int rowcount = 0;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:44:40+00:00Added an answer on June 11, 2026 at 7:44 am

    Let me simplify your code a litle.

    Go to xaml and write the following:

       <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <ListBox ItemsSource="{Binding Items}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Border BorderThickness="5" Background="Green">
                        <StackPanel Height="100" HorizontalAlignment="Left"
                                    Orientation="Horizontal"
                                    Background="Blue"   >
                                <TextBlock Height="80" Width="150" 
                                           FontSize="30" Foreground="White" 
                                           Text="{Binding Id}"/>
                                <TextBlock Height="80" Width="150" 
                                           FontSize="30" Foreground="White" 
                                           Text="{Binding Name}"/>
                            </StackPanel>
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    

    We declared the same layout that you did in code.

    Than go to MainPage.xaml.cs and write the following:

    public partial class MainPage : PhoneApplicationPage
    {
    public class DataBaseEntry 
    {
        public string Id {get;set;}
        public string Name {get;set;}
    }
    
    public ObservableCollection<DataBaseEntry> Items {get;set;}
    
    public MainPage()
    {
        InitializeComponent();
        Items = new ObservableCollection<DataBaseEntry>();
        PopulateListBox();
    
    }
    
    public void PopulateListBox()
    {
        updateDatabase();
        deleteRow(i);
    
        int rowCount = noofrows();
        for (int i = 1; i <= rowCount; i++)
        {
            var entry = new DataBaseEntry
            {
                Id = returnID(i),
                Name = SelectName(i)
            }
            Items.Add(entry);
        }
    
    }
    
    //this will remove item from Items collection and updates listbox
    public void DeleteItemById(int id) 
    {
        var item = Items.FirstOrDefault(item => item.id == id.ToString());
        if (item != null) 
        {
            Items.Remove(item);
        }
        string str2 = "delete  from Details where id =" + id;
        (Application.Current as App).db.SelectList(str2);
    }
    

    I suggets you to read more about Xaml -it is an elegant way to create and manage UI.

    If you need some more info about how to deal with selected items in a list box, read this blog post.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a button that is programatically created, it's content is a stack panel
I have created a Treeview and used a stack panel to include a checkbox,
I have a stack panel with a few (different) items: <StackPanel ...> <TextBlock ...
I have a StackPanel of Image objects that are created and added dynamically. How
I have created a Stack panel with 10 buttons. Whenever i put the focus
I have created a component derived from TCustomPanel. On that panel I have a
I have created some JQuery that will expand a div 'popup' on hover and
I have created an android application that calls (using kSOAP library) a SOAP based
I have a custom UserControl that I created as a navigation menu that parses
I have instantiated several System.Drawing.Icon Objects. Note that these are created at runtime and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.