Showing posts with label c# Code Snippets. Show all posts
Showing posts with label c# Code Snippets. Show all posts

Automatic ListView Grouping

3:49 AM

Lets say you have lots of data your listview. Now you want to Group This data According to a Perticular Subitems.

For Example:

Suppose i have some books data in my ListView.
this listview items contains Author name and Books Title.

And there are 2000 Books in list view.

Now i want to group the data in listview according to the Authors.

Now lets say there are 50 Unique Authors , meaning we will have to create 50 Groups in listview.

this seem hectic, and i dont know if there is any inbuilt function to automatically group this items, but i have created mine To automatically do the above.

Hope it becomes usefull to someone.

Code:

public void GroupListView(ListView lstV, int SubItemIndex)
        {
            bool flag = true;

            foreach (ListViewItem l in lstV.Items)
            {
                string strmyGroupname = l.SubItems[SubItemIndex].Text;

                foreach (ListViewGroup lvg in lstV.Groups)
                {
                    if (lvg.Name == strmyGroupname)
                    {
                        l.Group = lvg;
                        flag = false;
                    }
                }

                if (flag == true)
                {
                    ListViewGroup lstGrp = new ListViewGroup(strmyGroupname, strmyGroupname);
                    lstV.Groups.Add(lstGrp);
                    l.Group = lstGrp;
                }

                flag = true;


            }
        }

How To Use The Code:

Lets say the author's sub item's index is 1 and listview name is LstBooks

then call the function like:
GroupListView(LstBooks,1);


Vb.net Version:

Public Sub GroupListView(ByVal lstV As ListView, ByVal SubItemIndex As Int16)
        Dim flag As Boolean = True
        For Each l As ListViewItem In lstV.Items

            Dim strmyGroupname As String = l.SubItems(SubItemIndex).Text

            For Each lvg As ListViewGroup In lstV.Groups

                If lvg.Name = strmyGroupname Then
                    l.Group = lvg
                    flag = False
                End If

            Next

            If flag = True Then
                Dim lstGrp As New ListViewGroup(strmyGroupname, strmyGroupname)
                lstV.Groups.Add(lstGrp)
                l.Group = lstGrp
            End If

            flag = True

        Next
    End Sub

Output:



Listing Running Processes in c#.net

1:42 AM
1. Add List box on a Forms.
2. Now add a reference to System.Diagnostics
Code:



Process[] curProcesses = Process.GetProcesses();

            foreach (Process p in curProcesses)
            {
                listBox1.Items.Add(p.ProcessName);
            }


Output:


A simple Class for Moving Controls At Runtime

2:02 AM
Here Is The Class:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace MoveControl
{
   sealed class clsMoveControl
    {
        public enum Direction
        {
            Any,
            Horizontal,
            Vertical
        }

        public static void StartMoving(Control cntrl)
        {
            StartMoving(cntrl, Direction.Any);
        }

        public static void StartMoving(Control cntrl, Direction dir)
        {
            StartMoving(cntrl, cntrl, dir);
        }

        public static void StartMoving(Control cntrl, Control container, Direction dir)
        {
            bool Dragging = false;
            Point DragStart = Point.Empty;
            cntrl.MouseDown += delegate(object sender, MouseEventArgs e)
            {
                Dragging = true;
                DragStart = new Point(e.X, e.Y);
                cntrl.Capture = true;
            };
            cntrl.MouseUp += delegate(object sender, MouseEventArgs e)
            {
                Dragging = false;
                cntrl.Capture = false;
            };
            cntrl.MouseMove += delegate(object sender, MouseEventArgs e)
            {
                if (Dragging)
                {
                    if (dir != Direction.Vertical)
                        container.Left = Math.Max(0, e.X + container.Left - DragStart.X);
                    if (dir != Direction.Horizontal)
                        container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y);
                }
            };
        }
    }
}



How to Use Example :

1. Create a simple project .
2. On the load event of the form write the following coding

clsMoveControl.StartMoving(this);

This will make the form movable by clicking anywhare on the form and dragging it.
The same can be done for other controls like:

clsMoveControl.StartMoving(Button1);
 clsMoveControl.StartMoving(Panel1);
 
See it Live:

Trimming a String Arrary

1:39 AM

Let's Say i have following array:

string[] myArray = new string[6];
            myArray[0] = "a";
            myArray[1] = "b";
            myArray[2] = " ";  //space
            myArray[3] = "c";
            myArray[4] = ""; //null
            myArray[5] = "d";
   


I want to remove the space and null parts from the array.
so after trimming the array should be like this:

            myArray[0] = "a";
            myArray[1] = "b";
            myArray[2] = "c";
            myArray[3] = "d";



Code:

public string[] TrimStringArray(string[] strArray)
        {
            
            int c = 0;
            foreach (string str in strArray)
            {
                if (str != "" && str != null)
                {
                    c++;
                }
            }

            string[] tempArrary = new string[c];
            int j=0;
            for (int i = 0; i < strArray.Length; i++)
            {
                if( strArray[i] != null && strArray[i] != "")
                {
                    tempArrary[j] = strArray[i];
                    j++;
                }
            }

            return tempArrary;
        }
   

Examle:
myArray = TrimStringArray(myArray);

A Function to Concat String Array Values by Specified Delimiter

2:13 AM
Let 's Say I have Following Array:

string[] st = new string[5];
            st[0] = "Animation";
            st[1] = "Action";
            st[2] = "Romance";
            st[3] = "Drame";
            st[4] = "Comedy";

Now I want to Merge all of it with ',' Delimiter Like Below:

Output :   Animation,Action,Romance,Drame,Comedy

Here is the Function For the Above
public string GetAllStringsFromArrary(string[] strArray,string strDelimeter)
        {
            string strFinal = string.Empty;

            for (int i = 0; i < strArray.Length ; i++)
            {
                strFinal += strArray[i];

                if (i != strArray.Length - 1)
                {
                    strFinal += strDelimeter;
                }
            }
            return strFinal;
            

        }
We will Call it Like This:
string str = GetAllStringsFromArrary( st,",");

A Function to find strings Between a Given Word/string

3:27 AM
For example :
let's say you have following Text:
Title                    Author              Product*         
~~~~~                    ~~~~~~              ~~~~~~~~         
"Aurora's Eggs"          Douglas Niles       Story (Dragons 2)

The Dragons              Douglas Niles       Novel            

The Kinslayer Wars       Douglas Niles       Novel            

The Qualinesti           Paul B. Thompson    Novel            
                         & Tonya C. Cook

Vinas Solamnus           J. Robert King      Novel            

The Dargonesti           Paul B. Thompson    Novel            
                         & Tonya C. Cook

"Storytellers"           Nick O'Donohoe      Story (Dragons 2)

"The Best"               Margaret Weis       Story (Dragons 1)

"Quarry"                 Adam Lesh           Story (Dragons 2)

"Easy Pickings"          Douglas Niles       Story (Dragons 1)

The Legend of Huma       Richard A. Knaak    Novel            

And I want to find Everthing that is in between " character.

The Answer should be like Following

Aurora's Eggs

Storytellers

The Best

Quarry

Easy Pickings


For this use the following Function:


public static List FindStringBetween(string strData,string strFindWhat)
      {
          List lstFound = new List();
          int startIndex, EndIndex;
          startIndex = strData.IndexOf(strFindWhat);

          EndIndex = strData.IndexOf(strFindWhat, startIndex + strFindWhat.Length);

          if (EndIndex > 0)
          {
              lstFound.Add(strData.Substring(startIndex+strFindWhat.Length ,EndIndex - startIndex-strFindWhat.Length )); 
          }

          while (EndIndex > 0)
          {
              startIndex = strData.IndexOf(strFindWhat, EndIndex + 1);
              if (startIndex  == -1) {
                  return lstFound; }
                  EndIndex = strData.IndexOf(strFindWhat, startIndex + 1);
              if (EndIndex > 0)
              {
                  lstFound.Add(strData.Substring(startIndex + strFindWhat.Length, EndIndex - startIndex - strFindWhat.Length)); 
              }
          }

          return lstFound;
      }

Please ignore the last line.

It will give you the complete list of the strings found.