Introduction

In my previous article I described how to Delete specific element of an array and in this article I explain how to insert an element at a specific position and also do not increate the size of the array.

Use the following procedure to create it.

Step 1

First of all you must create a new Windows Store Application.

  • Open Visual Studio 2012
  • "File" -> "New" -> "Project..."
  • Choose "Template" -> "Visual C#" -> "Window Store app"
  • Choose "Blank App (XAML)" then rename the application

    new-windows-store-app.jpg

    Step 2

    Write the following XAML code in "Mainpage.Xaml" (that is available in Solution Explorer):

    <Page

    x:Class="delete_specific_element_from_array.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:delete_specific_element_from_array"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

    <Grid Background="red">

    <Grid.RowDefinitions>

    <RowDefinition Height="72*"/>

    <RowDefinition Height="38*"/>

    <RowDefinition Height="42*"/>

    <RowDefinition Height="35*"/>

    <RowDefinition Height="40*"/>

    <RowDefinition Height="79*"/>

    <RowDefinition Height="36*"/>

    <RowDefinition Height="33*"/>

    <RowDefinition Height="40*"/>

    <RowDefinition Height="42*"/>

    <RowDefinition Height="311*"/>

    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>

    <ColumnDefinition Width="248*"/>

    <ColumnDefinition Width="407*"/>

    <ColumnDefinition Width="257*"/>

    <ColumnDefinition Width="454*"/>

    </Grid.ColumnDefinitions>

    <TextBlock Text="Enter Element of an array" FontFamily="Arial" Grid.Column="1" Grid.Row="2" Foreground="White" FontSize="20" FontWeight="ExtraBold" ></TextBlock>

    <TextBox x:Name="textbox1" Grid.Column="2" Grid.Row="2" Width="200" HorizontalAlignment="Left" VerticalAlignment="Top" Height="32"></TextBox>

    <Button x:Name="button1" Content="Insert" Grid.Column="2" Grid.Row="3" Background="Yellow" Foreground="Black" Width="97" Height="35" Click="button1_Click" ></Button>

    <TextBlock x:Name="text1" FontFamily="Arial" Grid.Column="3" Grid.Row="2" FontSize="20" FontWeight="ExtraBold" ></TextBlock>

    <Button x:Name="button2" Content="show array element" Background="Yellow" Foreground="Black" Grid.Column="2" Grid.Row="4" Height="40" Width="171" Click="button2_Click"/>

    <TextBlock x:Name="text2" FontFamily="Arial" Grid.Column="1" Grid.Row="5" FontSize="20" FontWeight="ExtraBold" ></TextBlock>

    <TextBlock Text="Enter the position for insert Number" FontFamily="Arial" Grid.Column="1" Grid.Row="6" FontSize="20" FontWeight="ExtraBold" Grid.RowSpan="2" ></TextBlock>

    <TextBox x:Name="textbox2" Grid.Column="2" Grid.Row="6" Width="200" HorizontalAlignment="Left" VerticalAlignment="Top" Height="32"></TextBox>

    <Button x:Name="button3" Content="Inset at position" Grid.Column="2" Grid.Row="8" Background="Yellow" Foreground="Black" Width="200" Height="35" Margin="0,5,0,0" Click="button3_Click" />

    <Button x:Name="button4" Content=" Show after Delete array element" Grid.Column="2" Grid.Row="8" Background="Yellow" Foreground="Black" Width="257" Height="35" Margin="0,38,0,9" Click="button4_Click" Grid.RowSpan="2" />

    <TextBlock Text="Enter Element to insert" FontFamily="Arial" Grid.Column="1" Grid.Row="7" FontSize="20" FontWeight="ExtraBold" Grid.RowSpan="2" ></TextBlock>

    <TextBlock x:Name="text3" FontFamily="Arial" Grid.Column="1" Grid.Row="10" FontSize="20" FontWeight="ExtraBold" ></TextBlock>

    <TextBox x:Name="textbox3" Grid.Column="2" Grid.Row="7" Width="200" HorizontalAlignment="Left" VerticalAlignment="Top" Height="32" />

    </Grid>

    </Page>

    Step 3

    Now write the following C# code for the button within "Mainpage.Xaml.cs".

    using System;

    using System.Collections.Generic;

    using System.IO;

    using System.Linq;

    using Windows.Foundation;

    using Windows.Foundation.Collections;

    using Windows.UI.Xaml;

    using Windows.UI.Xaml.Controls;

    using Windows.UI.Xaml.Controls.Primitives;

    using Windows.UI.Xaml.Data;

    using Windows.UI.Xaml.Input;

    using Windows.UI.Xaml.Media;

    using Windows.UI.Xaml.Navigation;

    namespace delete_specific_element_from_array

    {

    public sealed partial class MainPage : Page

    {

    public MainPage()

    {

    this.InitializeComponent();

    }

    protected override void OnNavigatedTo(NavigationEventArgs e)

    {

    }

    int[] arr = new int[5];

    int i = 0;

    private void button1_Click(object sender, RoutedEventArgs e)

    {

    if (i < arr.Length)

    {

    arr[i] = int.Parse(textbox1.Text);

    i++;

    textbox1.Text = " ";

    }

    else

    {

    text1.Text = "Sorry array can contain only 5 element";

    }

    }

    private void button2_Click(object sender, RoutedEventArgs e)

    {

    for (int i = 0; i < arr.Length; i++)

    {

    text2.Text += arr[i] + " ";

    }

    }

    private void button3_Click(object sender, RoutedEventArgs e)

    {

    int pos = int.Parse(textbox2.Text);

    if (pos <= arr.Length)

    {

    int value = int.Parse(textbox3.Text);

    arr[pos] = value;

    }

    }

    private void button4_Click(object sender, RoutedEventArgs e)

    {

    foreach (int a in arr)

    {

    text3.Text += a + " ";

    }

    }

    }

    }

    Step 4

    Now Run your app.

    insert-specific-pos.jpg