---
title: "Getting new line in wpf"  
description: "Getting new line in wpf"  
author: "Pravesh Singh"  
published: 2013-09-23  
updated: 2013-09-23  
canonical: https://www.mindstick.com/forum/1489/getting-new-line-in-wpf  
category: "wpf"  
tags: ["wpf"]  
reading_time: 2 minutes  

---

# Getting new line in wpf

I am having [arraylist](https://www.mindstick.com/articles/11973/arraylist-class-in-c-sharp).let [say](https://answers.mindstick.com/qa/116645/a1-wreckers-reviews-what-do-customers-say) it contains 15 [elements](https://www.mindstick.com/forum/1440/wpf-button-with-multiple-text-elements). I am adding these to [stack](https://www.mindstick.com/blog/301746/why-is-stack-overflow-so-important-for-developers) panel. I need to [add](https://www.mindstick.com/forum/12983/add-address-in-textbox-when-page-is-load-and-if-i-search-address-in-textbox-show-in-map-by-javascript) 3 elements per line. my [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) is below. I [am getting](https://www.mindstick.com/forum/34179/i-am-getting-error-while-assigning-the-data-to-the-array-list) either horizontal or verrtical.Let me know how to do this.

```
    MainWindow w;    public ShopCart(MainWindow m,ArrayList _list)    {                              InitializeComponent();        w = m;        int i = 1;        foreach (string cartitems in _list)        {                mystackpanel.Orientation = Orientation.Horizontal;                mystackpanel.Margin.Left.Equals(150);                Label lbl = new Label();                lbl.Name = "Label" + i;                lbl.Height = 30;                lbl.Width = 200;                lbl.Margin.Left.Equals(150);                //lbl.Margin.Top.Equals(150);                lbl.Content = cartitems.ToString();                mystackpanel.Children.Add(lbl);                i++;                int str = mystackpanel.Children.Count;                MessageBox.Show(Convert.ToString(str));                if (str%3 == 0)                {                    Button btndelete = new Button();                    btndelete.Content = "Delete";                    btndelete.Width = 120;                    btndelete.Height = 35;                    mystackpanel.Children.Add(btndelete);mystackpanel.Margin.Top.Equals(500);}}
```

## Replies

### Reply by Sumit Kesarwani

Hi Pravesh,\

Here is some sample code(assuming you already have a list of buttons, and will add outer stack panel to your main control) you can try, you may need to change few things as per your need:

```
            List<Button> buttons = new List<Button>();            StackPanel panel = new StackPanel();            panel.Orientation = Orientation.Horizontal;            int count = 0;            StackPanel innerPanel = new StackPanel();            innerPanel.Orientation = Orientation.Vertical;            foreach (Button button in buttons)            {                innerPanel.Children.Add(button);                ++count;                if (count % 3 == 0 && count != 0)                {                    panel.Children.Add(innerPanel);                    innerPanel = new StackPanel();                    innerPanel.Orientation = Orientation.Vertical;                }            }            if (panel.Children.Contains(innerPanel) == false)            {                panel.Children.Add(innerPanel);            }
```

Although in my opinion the best way will be to have a Grid with n*n row and columns and add your buttons to respective row, columns.


---

Original Source: https://www.mindstick.com/forum/1489/getting-new-line-in-wpf

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
