Wednesday, June 30, 2010

MDI (Multiple Document Interface) Set Background Image


MDI (Multiple Document Interface) Set  Background Image
                To change the background color of a parent MDI form is to set the background color property. However, it seems to ignore it.  Also some of us hate the dark gray background.
In this tutorial I will help you to programmatically set the Background Image of the MDI parent. This includes vs2003 & vs2005 c# source file.
First we set the Form property ISMDICONTAINER = TRUE 
 

VS2003  C#
 
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;

private string filepath = System.Environment.CurrentDirectory; 

private void Form1_Load(object sender, System.EventArgs e)
{
			
filepath = System.IO.Path.Combine (filepath, "Desert.jpg");

 foreach(Control cntrl in this.Controls) 
 {
	if ((cntrl.GetType() == typeof(System.Windows.Forms.MdiClient ))){
					
	     cntrl.BackgroundImage = Image.FromFile(filepath);
				}
 }
}

private void menuItem2_Click(object sender, System.EventArgs e)
{	
	Form frmChild = new Form();

	frmChild.MdiParent = this;
	frmChild.Text = "Hello, im a child form";
	frmChild.Show ();
}


VS2005 C# - applicable to VS2008
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MdiApplicationCS2005
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (Control cntrl in this.Controls)
            {
                if (cntrl is MdiClient)
                {
                    cntrl.BackgroundImage = Properties.Resources.Jellyfish;  
                }
            }
        }

        private void childToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frmChild = new Form();
            frmChild.MdiParent = this;
            frmChild.Text = "Hello i'm a child form";
            frmChild.Show();
        }
    }
} 
 

Sunday, June 27, 2010

Recursion C# and VB.Net

Recursion in C# and VB.Net

Recursion is a procedure (in VB.net) or method (in C#) that calls itself with in the program either directly or indirectly. In other words, a method or procedure is said to be recursive when it contains a statement that calls itself. Moreover, defining looping constructs in these two languages like “while for loops” are used to do repetition.
To really understand, on how to use recursion I will try to give some basic examples. First one will be the Factorial and Fibonacci Series both languages will be used on this tutorial. So let’s get started.

Sample codes goes here:

C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;

static class clsMain
{

 private static int Factorial(int intNumber)
 {
        if (intNumber <= 1)
   return 1;
        else 
            return intNumber * Factorial(intNumber - 1);
 }

 private static int Fibonnacci(int intNumber)
 {
        if ((intNumber == 1) | (intNumber == 0)) 
           return intNumber;
        else 
           return Fibonnacci(intNumber - 1) + Fibonnacci(intNumber - 1);
 }


 public static void Main()
 {
      try 
            {
       Console.WriteLine("Welcome to Recursion");

       Console.WriteLine("---------------------");

       Console.WriteLine("Our example Factorial Program and Fibonacci");

       Console.WriteLine("---------------------");

       Console.WriteLine("Enter number to get factorial");

                int intNumber = int.Parse (Console.ReadLine());

       string factorialAnswer = null;
       
       factorialAnswer = string.Format("{0}!={1}", intNumber, Factorial(intNumber));
                
                Console.WriteLine(factorialAnswer);
                
                string fibonacciAnswer = null;
                
                fibonacciAnswer = string.Format("Fibonacci Value is {0}", Fibonnacci(intNumber));

                Console.WriteLine(fibonacciAnswer);

                Console.ReadLine();


      } 
            catch (Exception ex) 
            {
                Console.WriteLine(ex.Message);
            }
        }
 }

VB.Net
Option Explicit On
Option Strict On

Module modMain

    Private Function Factorial(ByVal intNumber As Integer) As Integer

        If (intNumber <= 1) Then

            Return 1

        Else

            Return intNumber * Factorial(intNumber - 1)

        End If

    End Function

    Private Function Fibonnacci(ByVal intNumber As Integer) As Integer

        If (intNumber = 1) Or (intNumber = 0) Then

            Return intNumber

        Else

            Return Fibonnacci(intNumber - 1) + Fibonnacci(intNumber - 1)

        End If

    End Function

    Sub Main()

        Try

            Console.WriteLine("Welcome to Recursion")

            Console.WriteLine("---------------------")

            Console.WriteLine("Our example Factorial Program and Fibonacci")

            Console.WriteLine("---------------------")

            Console.WriteLine("Enter number to get factorial")

            Dim intNumber As Integer = Integer.Parse(Console.ReadLine())

            Dim factorialAnswer, fibonacciAnswer As String

            factorialAnswer = String.Format("{0}!={1}", intNumber, Factorial(intNumber))

            Console.WriteLine(factorialAnswer)

            fibonacciAnswer = String.Format("Fibonacci Value is {0}", Fibonnacci(intNumber))

            Console.WriteLine(fibonacciAnswer)

            Console.ReadLine()

        Catch ex As Exception

            Console.WriteLine(ex.Message)

        End Try

    End Sub

End Module



Sunday, June 20, 2010

ComboBox AutoComplete

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace cmbAutoComplete
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private List<string> SampleList()
        {
            List<string> ListToAddinCombobox = new List<string>();

            ListToAddinCombobox.Add("Angeles City");
            ListToAddinCombobox.Add("Baguio City");
            ListToAddinCombobox.Add("Caloocan City");
            ListToAddinCombobox.Add("Davao City");
            ListToAddinCombobox.Add("Malabon City");
            ListToAddinCombobox.Add("Quezon City");
            ListToAddinCombobox.Add("Naga City");
            ListToAddinCombobox.Add("Tacloban City");
            ListToAddinCombobox.Add("Leyte City");
            ListToAddinCombobox.Add("Cebu City");
            ListToAddinCombobox.Add("Dipolog City");
            ListToAddinCombobox.Add("Marikina City");
            ListToAddinCombobox.Add("Navotas City");
            ListToAddinCombobox.Add("Muntinlupa City");
            ListToAddinCombobox.Add("Pasig City");
            ListToAddinCombobox.Add("Las Pinas City");

            ListToAddinCombobox.Sort();

            return ListToAddinCombobox;
        }


        private void Form1_Load(object sender, 
                                  EventArgs e)
        {
            try
            {
              
                this.comboBox1.DataSource = 
                    this.SampleList();
                this.comboBox1.SelectedIndex = -1;

                this.comboBox1.AutoCompleteSource = 
                        AutoCompleteSource.ListItems;

                this.comboBox1.AutoCompleteMode = 
                        AutoCompleteMode.SuggestAppend;
   
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

Sunday, June 13, 2010

Local Sql Server Instances using the registry

vb.net
Option Strict On
Imports Microsoft.Win32
Imports System.Environment

Module modMain
    ''' <summary>
    ''' gets all of the installed 
    ''' instances server of sql 
    ''' </summary>
    ''' <returns>string arrays of sqlserverinstances</returns>
    ''' <remarks>for local servers only</remarks>
    Private Function SqlServerInstances() As String()

        Dim instances As String() = Nothing

        Try

            Dim key As RegistryKey

            key = Registry.LocalMachine. _ 
OpenSubKey("SOFTWARE\Microsoft\MicrosoftSQLServer")

            instances = _ 
                 DirectCast(key. _ 
            GetValue("InstalledInstances"), String())

        Catch ex As Exception

            Console.WriteLine(ex.Message)

        End Try

        Return instances

    End Function

   <stathread()> _
   Sub Main(ByVal args As String())

        If (SqlServerInstances.Count > 0) Then

            For Each sqlElment As String _ 
            In SqlServerInstances()

                Select Case sqlElment

                    Case "MSSQLSERVER"

                        Console.WriteLine _ 
                              (Environment.MachineName)
                    Case Else

                        Dim strSQL As String = _
                        String.Format("{0}\{1}", _ 
                        Environment.MachineName,sqlElment)

                        Console.WriteLine (strSQL)

                End Select

            Next

        End If

    End Sub

End Module

c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32; 

namespace sqlLocalRegistryCS
{
    class Program
    {
        /// <summary>
        /// gets all of the installed 
        /// instances server of sql 
        /// must be an administrator
        /// </summary>
        /// <returns>string arrays 
        /// of sqlserverinstances</returns>
        /// <remarks>for local servers only </remarks>
        private static  string[] SqlServerInstances()
        {
            string[] instances = null;

            try
            {
                RegistryKey key = default(RegistryKey);

                key = Registry.LocalMachine.
                OpenSubKey
                ("SOFTWARE\\Microsoft\\Microsoft SQL Server");

                instances = 
                (string[])key.GetValue("InstalledInstances");

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

            }

            return instances;
        }

        static void Main(string[] args)
        {
            if ((SqlServerInstances().Length  > 0))
                {

                    foreach (string sqlElment 
                    in SqlServerInstances())
                    {
                        switch (sqlElment)
                        {

                            case "MSSQLSERVER":

                                Console.WriteLine
                                (Environment.MachineName);

                                break;
                            
                               default:

                               string strSQL = 
                               string.Format("{0}\\{1}",
                               Environment.MachineName, 
                               sqlElment);

                               Console.WriteLine(strSQL);

                               break;
                        }
                    }
                }
        }
    }
}

Thursday, June 10, 2010

Welcome

Welcome to my blog.
Actually this is my first post, I'm still thinking what programming topics should I write. If you guys have some suggestions please do so. Thanks and enjoy your stay.