Merged Array in Windows Store App

Introduction

In this article I explain how to merge two arrays into a single array. To implement this I created two separate arrays with a different size that contains the array elements according to their size and finally another array which with a size equivalent to the first array plus the second array which contains the elements of both the first and second 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="merge_two_array.MainPage"

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

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

    xmlns:local="using:merge_two_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="81*"/>

            <RowDefinition Height="41*"/>

            <RowDefinition Height="38*"/>

            <RowDefinition Height="37*"/>

            <RowDefinition Height="39*"/>

            <RowDefinition Height="40*"/>

            <RowDefinition Height="492*"/>

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="179*"/>

            <ColumnDefinition Width="333*"/>

            <ColumnDefinition Width="252*"/>

            <ColumnDefinition Width="602*"/>

        </Grid.ColumnDefinitions>

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

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

        <Button x:Name="button1" Grid.Column="2" Grid.Row="2" Background="Yellow" Foreground="Black" Width="150" Content="insert" Height="38" Click="button1_Click"></Button>

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

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

        <Button x:Name="button2" Grid.Column="2" Grid.Row="4" Background="Yellow" Foreground="Black" Width="150" Content="insert" Height="38" Margin="0,1,0,0" Click="button2_Click" RenderTransformOrigin="0.5,0.5">

            <Button.RenderTransform>

                <CompositeTransform SkewX="6.387" TranslateX="2.127"/>

            </Button.RenderTransform>

        </Button>

        <Button x:Name="button3" Grid.Column="2" Grid.Row="5" Background="Yellow" Foreground="Black" Width="169" Content="Array after Merged" Height="38" Margin="0,2,0,0" Click="button3_Click"></Button>

        <TextBlock x:Name="text1" FontFamily="Arial" FontSize="20" FontWeight="ExtraBold" Grid.Column="3" Grid.Row="1" VerticalAlignment="Top" Margin="24,0,0,0" ></TextBlock>

        <TextBlock x:Name="text2" FontFamily="Arial" FontSize="20" FontWeight="ExtraBold" Grid.Column="3" Grid.Row="3" VerticalAlignment="Top" Margin="24,0,0,0" ></TextBlock>

        <TextBlock x:Name="text3" FontFamily="Arial" FontSize="20" FontWeight="ExtraBold" Grid.Column="3" Grid.Row="5" VerticalAlignment="Top" Margin="24,0,0,0" ></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 merge_two_array

{

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)

        {

        }

        int[] arr1 = new int[5];

        int[] arr2 = new int[6];

        int i = 0, j=0;

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            if (textbox1 .Text .Length >0)

            {

                if (i < arr1.Length)

                {

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

                    i++;

                    textbox1.Text = " ";

                }

                else

                {

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

                }

            }

        }

 

        private void button2_Click(object sender, RoutedEventArgs e)

        {

            if (textbox2.Text.Length > 0)

            {

                if (j < arr2.Length)

                {

                    arr2[j] = int.Parse(textbox2.Text);

                    j++;

                    textbox2.Text = " ";

                }

                else

                {

                    text2.Text = "Sorry array contain only 6 element";

                }

            }

        }

 

        private void button3_Click(object sender, RoutedEventArgs e)

        {

            int[] arr3 = new int[arr1.Length + arr2.Length];

            Array.Copy(arr1,0, arr3, 0,arr1.Length);

            Array.Copy(arr2,0, arr3,arr1.Length,arr2 .Length);

            foreach (int a in arr3)

            {

                text3.Text += a + " ";

            }

        }

    }

}

 

Step 4

Now Run your app.

merged-array.jpg


Similar Articles