Delete Specific Element of an Array in Windows Store App

Introduction

In this article I explain how to delete a specific element of an array. In this article I have an array from the end user that is limited in size to only 5 elements in the array. Then the user enters the number to be deleted from the array. Then see the remaining array elements after deleting the specified element 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="43*"/>

            <RowDefinition Height="48*"/>

            <RowDefinition Height="43*"/>

            <RowDefinition Height="328*"/>

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="248*"/>

            <ColumnDefinition Width="407*"/>

            <ColumnDefinition Width="257*"/>

            <ColumnDefinition Width="454*"/>

        </Grid.ColumnDefinitions>

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

        <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 element for delete" FontFamily="Arial" Grid.Column="1" Grid.Row="6" FontSize="20" FontWeight="ExtraBold" ></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="Delete" Grid.Column="2" Grid.Row="7" Background="Yellow" Foreground="Black" Width="97" Height="35" Margin="0,7,0,6" Click="button3_Click" ></Button>

        <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,5,0,3" Click="button4_Click" />

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

    </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 num = int.Parse(textbox2.Text);

            int index = Array.IndexOf(arr, num);

            List<int> temp = new List<int>(arr);

            temp.RemoveAt(index);

            arr = temp.ToArray();

        }

 

        private void button4_Click(object sender, RoutedEventArgs e)

        {

            foreach (int a in arr)

            {

                text3.Text += a + " ";

            }

        }

    }

}

 

Step 4

Run your app.

Delete-from-array.jpg


Similar Articles