C#: Generic method

This entry is part 61 of 71 in the series C#

A generic method, as the name implies, allows a general method to be declared for reusability.

It offers us a way to declare a general type called the type parameter.  With these general types, we can input different type of parameters into the methods.

Consider the following example.

Generics method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Generics1
{
    class Program
    {
        static void Swap<T>(ref T lhs, ref T rhs)
        {
            T temp;
            temp = lhs;
            lhs = rhs;
            rhs = temp;
        }

        static void Main(string[] args)
        {
            int a = 1;
            int b = 2;

            string l = "left";
            string r = "right";

            Swap<int>(ref a, ref b);
            Console.WriteLine(a + " " + b);
            Console.WriteLine();

            Swap<string>(ref l, ref r);
            Console.WriteLine(l + " " + r);
            Console.ReadKey();
        }
    }
}

generics

Here in this example, we have one swap function but it can take in either integer or string parameters.

In a way, it offers us a quick way to reuse our code.

 

WPF: Adding items to listbox

This entry is part 29 of 54 in the series Learn WPF

We have been able to add items to a combo box using ObservableCollection class.

Using the same technique, we can add items to a ListBox from code behind.  This is really useful if the list items depend on what is available in the database.

MainWindow.xaml
<Window x:Class="ListBox2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:src="clr-namespace:ListBox2"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel  Grid.Column="0"
             Grid.Row="6">
        <StackPanel.Resources>
            <src:VacationSpots x:Key="myVacations"/>
        </StackPanel.Resources>
        <ListBox Name="listBox" ItemsSource="{StaticResource myVacations}" SelectedIndex="1" Margin="50,50,50,50"/>

        <TextBlock Text="{Binding ElementName=listBox, Path=SelectedItem}" Margin="50,10,50,50"/>
    </StackPanel>
</Window>

MainWindow.xaml
using System.Collections.ObjectModel;
using System.Windows;

namespace ListBox2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    class VacationSpots : ObservableCollection<string>
    {
        public VacationSpots()
        {
            Add("Spain");
            Add("France");
            Add("Peru");
            Add("Mexico");
            Add("Italy");
        }
    }
}

listbox_item

 

As I have put SelectedIndex=”1″, this causes France to be the default selected item in the list.

 

WPF: Simple combobox data binding

This entry is part 25 of 54 in the series Learn WPF

Here we will just work out a simple way to display a selected item in a combo box using data binding method.

This is all done with XAML without using code behind.

MainWindow.xaml
<Window x:Class="ComboBox2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Margin="20,20,20,20">
        <ComboBox Name="cbox" Text="My Cities" IsEditable="True" IsReadOnly="True">
            <ComboBoxItem>Spain</ComboBoxItem>
            <ComboBoxItem>France</ComboBoxItem>
            <ComboBoxItem>Peru</ComboBoxItem>
            <ComboBoxItem>Mexico</ComboBoxItem>
            <ComboBoxItem>Italy</ComboBoxItem>
        </ComboBox>
        <TextBlock Text="{Binding ElementName=cbox, Path=SelectedItem.Content}" Width="200" Height="30" HorizontalAlignment="Center" Margin="10,100,50,50"></TextBlock>
    </StackPanel>
</Window>

simplecombo

 

 

 

 

WPF: XAML ComboBox

This entry is part 26 of 54 in the series Learn WPF

A ComboBox is just like a drop down list in HTML.

Here we use some XAML tags to work out a ComboBox.  We have made the ComboBox to be editable, so you can actually type in the box to make a selection.

MainWindow.xaml
<Window x:Class="ComboBox2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Margin="20,20,20,20">
        <TextBox Width="200" Height="30" HorizontalAlignment="left">Where would you like to go today?</TextBox>
        <ComboBox Name="cb" SelectionChanged="DisplayOutput" Text="My Cities" IsEditable="true">
            <ComboBoxItem>Spain</ComboBoxItem>
            <ComboBoxItem>France</ComboBoxItem>
            <ComboBoxItem>Peru</ComboBoxItem>
            <ComboBoxItem>Mexico</ComboBoxItem>
            <ComboBoxItem>Italy</ComboBoxItem>
        </ComboBox>
        <TextBlock Name="tb" Width="200" Height="30" HorizontalAlignment="Center" Margin="10,100,50,50">You selected</TextBlock>
    </StackPanel>
</Window>

MainWindow.xaml.cs
using System.Windows;
using System.Windows.Controls;

namespace ComboBox2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        void DisplayOutput(object sender, SelectionChangedEventArgs args)
        {
            ComboBoxItem cb = ((sender as ComboBox).SelectedItem as ComboBoxItem);
            tb.Text = "You selected " + cb.Content.ToString() + ".";
        }
    }
}

xamlcombobox

 

WPF: Adding items to combobox using ObservableCollection

This entry is part 28 of 54 in the series Learn WPF

ObservableCollection is usually used for providing notifications when new items are added, removed or refleshed in a list.

As in the example below, as new countries are added into the list, the XAML UI will be notified of the change and add the new items into the combo box for selection accordingly.

Here we only make use of the Add method to add new items into a combo box list.

MainWindow.xaml
<Window x:Class="ComboBox1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:src="clr-namespace:ComboBox1"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel  Grid.Column="0"
             Grid.Row="6">
        <StackPanel.Resources>
            <src:VacationSpots x:Key="myVacations"/>
        </StackPanel.Resources>
        <ComboBox Name="comboBox1"
            ItemsSource="{StaticResource myVacations}"
            Text="My Cities"
            IsEditable="true"
            IsReadOnly="true"/>

        <TextBlock Text="{Binding ElementName=comboBox1, Path=SelectedItem}"/>
    </StackPanel>
</Window>

MainWindow.xaml.cs
using System.Collections.ObjectModel;
using System.Windows;

namespace ComboBox1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    class VacationSpots: ObservableCollection<string>
    {
        public VacationSpots()
        {
            Add("Spain");
            Add("France");
            Add("Peru");
            Add("Mexico");
            Add("Italy");
        }
    }
}

The namespace of this class is System.Collections.ObjectModel which I have added as shown in line 3.

observablecollection

Output when a city country is selected.

cbox