h1.post-title { color:orange; font-family:verdana,Arial; font-weight:bold; padding-bottom:5px; text-shadow:#64665b 0px 1px 1px; font-size:32px; } -->

Pages

Rename File or folder in C#.net

1) Rename a File in C#.net
string path = @"C:\Documents and Settings\Narayanan\My Documents\Others\Txt\test\";
string Orgifile = path + "\\Test.txt";
string Renamefile = path + "\\Test1.txt";
File.Move(Orgifile, Renamefile);
2) Rename a Folder in C#.net
string path = @"C:\Documents and Settings\Narayanan\My Documents\Others\Txt";
string OriFolder = path + "\\test";
string RenameFolder = path + "\\Test1.txt";
Directory.Move(OrgiFolder, RenameFolder);

Collections in C#.net

What is mean by Collections?
Collections contain number of classes to required for storing group of related objects.
What are the Collections Classes?
1) Array List
2) Queue
3) Stack
4) StringCollection
5) BitArray
1) Array List:
Can Store any type of object.
Expand to any required capacity
2) Queue:
First in First Out (FIFO) Collection.
On messaging server to store messages temp before Processing
3) Stack:
Last in First Out (LIFO) Collection.
Stack to track change so that the most recent change can be undone.
4) StringCollection:
Like ArrayList, except values are strongly typed as a strings.
Does not support Sorting.
5) BitArray:
A collection of Boolean values.
ArrayList:
Now, we discussed about How to use Arraylist.Add Method in C#.net?
Using System.Collections
ArrayList ArrayLst = new ArrayList ()
ArrayLst.Add("Hello");
ArrayLst.Add ("World");
foreach (Object Obj in ArrayLst)
Console.WriteLine (Obj.Tostring ())
Output:
Hello World

Basic steps for creating a program in .Net

Introduction:
  This article may be considered as a guide for wirting programs in any .net Language. So Please read this article read carefully .If you want any suggestions and Comments to this Mail ID:irnaraayanan@yahoo.com
File Name:
   What is the entire file names created at the time of Project?
In C#.net
 File Name (Suffix):   .cs .
Usage:                        It is a Class File
File Name (Suffix):   .csproj.
Usage:                        It is a Project File
In VB.net
 File Name (Suffix):   .vb .
Usage:                        It is a Class File
File Name (Suffix):   .vbproj.
Usage:                        It is a Project File
IN ASP.Net
File Name (Suffix):   .aspx .
Usage:                        It is a ASP.Net File
File Name (Suffix):   .ascx.
Usage:                        User Defined Control in ASP.Net
File Name (Suffix):   .asmx.
Usage:                        Web service in ASP.Net
 File Name (Suffix):   .sln.
Usage:                        .Net Solution File
Common File Names in .net

  • Machine.config
  • App.config
  • Web.config
  • Assemble.vb
  • Global.asax
File Layout:
   Each .net Source file will contain only one Public class. When private classes and interfaces are associated within Public class. Here I give the example  for File Layout.

Class Name: Test
File Name: Test.cs

Class layout:
   Class/Interface Header comments
Nested Types
Object Declaration
Constructors
Properties
Methods
Method Layout:
   Method Commets
Local variable Declaration
Code
Declarations variables should be meaningful and related your Project .Don't declare single letter like (a, b).
Blank Lines:
   One blank line should always be used between
Methods,
Properties
Local variable in the Method and it's a first statement.
Class names must be nouns or nouns phrase
Use Properties spelled Plural Form to a name Collection Class.
Use Pascal Casing.
Do not use any class prefix
Use Singular names for Enum.
Use plural names for bit field.
Use the Char "I" as a prefix to indicate that the type is an Interface.
Use the Char "m_" as a prefix to indicate that the Module variable
Use the Char "l_" as a prefix to indicate that the Local variable
Name Methods with verbs or verb phrase.
Pascal Casing : First char of the word is capital  .Ex: MyCount
Camel Casing : First char of the word is capital except  the first one  .Ex: myCount

Back color for MDI in C#.net

MdiClient ctlMDI;
            foreach (Control ctl in this.Controls)
            {
                try
                {
                   ctlMDI = (MdiClient)ctl;
                    ctlMDI.BackColor = Color.Yellow;
                }
                catch (InvalidCastException exc)
                {
                    MessageBox.Show(exc.Message);
                }
            }
            }

Form will open by pressing the Enter key or Space Bar

KeyPreview = true 

Resize ComboBox in C#.net

private void cmbwidth(ComboBox cmb)
        {
            System.Drawing.Graphics g = cmb.CreateGraphics();
            float maxwidth = 0f;
            foreach (object o in cmb.Items)
            {
                float w = g.MeasureString(o.ToString(), cmb.Font).Width;
                if (w > maxwidth)
                    maxwidth = w;
            }
            g.Dispose();
            cmb.Width = (int)maxwidth + 20;
        }

Count of repeated characters in String in C#.net

Function/ Method:
public static int countofrepeatedchar(string inputstring, char ch)
        {
            char[] Inputstring = inputstring.ToCharArray();
            char Ch = ch;
            int cntofchar = Inputstring.Length;
                foreach (char chr in Inputstring)
                {
                    if (chr == Ch)
                    {
                        result++;
                    }
       
            }


            return result;
        }

int res = countofrepeatedchar("123456123",'4');
            MessageBox.Show(res.ToString());

Square Root Calculation in C#.Net

Declaration part:
public static string strvalue;


public static stringres;

Function/Method:
#region Square Root 
      public static string  squareRoot(double fv, double sv)
      {
          double Result;
         
          for (int idx = 1; idx < sv+1; idx++)
          {
              if (idx == 1)
              {
                  Result = Math.Pow(fv, idx);
                  res = Convert.ToString(Result);
              }
              else if (idx >  1)
              {
                  Result = Math.Pow(fv, idx);
                  res = res  + "," + Convert.ToString(Result);
              }
          }
          strvalue =res;
          return strvalue;
      }
      #endregion Square Root
 
Call function in Your Application : 
string str = squareRoot(2, 10);
MessageBox.Show(str);
 

Font names in Combobox using C#.Net

Declaration Part:


Using System.Drawings.Text





Code for getting font name into ComboBox :

InstalledFontCollection inst = new InstalledFontCollection();
            foreach (FontFamily fnt in inst.Families)
            {
                comboBox1.Items.Add(fnt.Name);
            }







Tooltip for TextBox

Tooltip for TextBox -- Coding  
private void textBox1_TextChanged(object sender, EventArgs e)
{
  if (textBox1.Text != "")
  {
     string str = textBox1.Text;
     toolTip1.Show(str, textBox1, 1, 10);
  }
  else
 {
   toolTip1.Hide(textBox1);
 }
}

Delete files base on character

Good Afternoon!
Today , we will discuss about Delete Files based on Specified Character.  
 
private void deletefilescheckwithstartingword(string inputPath,char ch) 
 { 
string[] InputPath = System.IO.Directory.GetFiles(inputPath); 
foreach (string fileName in InputPath)
 {
 FileInfo fileInformation = new FileInfo(fileName);
 string file = fileInformation.Name; char[] letter = file.ToCharArray(); 
if (letter[0] == ch)
 {
 System.IO.File.Delete(fileName);
 }
 } 
 } 

Happy Coding! 

Auto Complete,AutoScale,AutoSize Mode

AutoComplete Mode:
AutoComplete Mode is only for ComboBox and Text Box.
AutoScale Mode:
Default AutoScale Mode  is "Font"It is only for Form.
AutoSize Mode:
 Grow Only: Manually,you can change size of the Control when Auto size is enable.
 Grow and Shrink:Manually,you can't change size of the Control when Auto size is enable.

Control Validation using C#.net

Step by step to Control Validation:
 
   Add a Form
    Go to Form Property window.
    Disable Auto Validate .
    Add a Text Box in the Form.
    Add a Button in the Form.
    Verify Causes Validation is true.


if (textBox1.Text.Length == 0)
 { 
e.Cancel = true;
 }
 else
 {
 e.Cancel = false; 
}
 if (this.ValidateChildren())
 { 
MessageBox.Show("Validation succeeded!");
 }
 else
 {
 MessageBox.Show("Validation failed.");
 }

Form.ShowDialog vs Form.Show


Form.ShowDialog vs Form.Show
Form.ShowDialog Form.Show
It is a Modal Dialog Box. It is a Modaless.
you can't transfer a Text into Main form to Child Form. you can transfer a Text into Main form to Child Form.
Main form will not focus and you can't call another form before closing this form. Main form will focus and you can call another form before closing this form.

Disable Controls in Tab Control

Today, we will discuss about How to Disable Controls in Tab Control using C#.net? private static void DisableControls(Control.ControlCollection ctls, bool disable) { foreach (Control ctl in ctls) { ctl.Enabled = disable; DisableControls(ctl.Controls, disable); } } public static void DisableTab(TabPage page, bool disable) { DisableControls(page.Controls, disable); } private void btnAction_Click(object sender, EventArgs e) { DisableTab(tabPage1, false); btnAction.Enabled = true; } Happy Coding!

Get Parent Directory Name

Here , I give  code for Get Parent Directory Name

Use Parent.FullName Code.

string root = @"C:\Lakshmi\Naraayanan";
DirectoryInfo di = new DirectoryInfo(root);
MessageBox.Show(di.Parent.FullName);


Happy Coding!

Use Multiple Cast Delegates in C# .net

First create a class file for using Method in Delegate.Class name is  :Mathamatic.cs
Function Names are Add,Sub,Mul,Div.
Functions are given below:
public static string Add(int i, int j)
       {
          return "Sum of  I and J  is " + " " + ":" + (i + j);
       }
public static string Sub(int i, int j)
        {
            return "Sub of  I and J  is " + " " + ":" + (i - j);
        }
public static string Mul(int i, int j)
{
            return "Mul of  I and J  is " + " " + ":" + (i * j);
}
public static string Divi(int i, int j)
{
       return "Divide of  I and J  is " + " " + ":" + (i / j);
}



In our Form , drag and drop a button control in the Form.
First Declare a Delegate in the form :(like this)


public delegate string matha (int i,int j);


Now paste this code in the button Click Event.


 matha matadd = new matha(mathamatics.Add);
 MessageBox.Show(matadd(10, 5).ToString());
 matha matSub = new matha(mathamatics.Sub);
 MessageBox.Show(matSub(10, 5).ToString());
 matha matDivi = new matha(mathamatics.Divi);
 MessageBox.Show(matDivi(10, 5).ToString());
 matha matMul = new matha(mathamatics.Mul);
 MessageBox.Show(matMul(10, 5).ToString());


Happy Coding!

Add Enum value into ComboBox

Here , I will give a Small Code for Add Enum Value into ComboBox using C#.net.

First We create Enum Method using Enum Keyword:

enum days
    {
        Monday=1,
        Friday,
       Sunday
   }

Add a enum value into ComboBox box.
Add this code in the Form loading or button click event or any where...

comboBox1.Items.Add(days.Friday);
comboBox1.Items.Add(days.Monday );
comboBox1.Items.Add(days.Sunday);