Search This Blog

Wednesday 28 October 2015

TreeView Traversing in c#

To get tree view structure in C# language according to business logic

private List<string> GetAllSubItems(string ItemId)
 {
    List<string> ItemIds = new List<string>();
    List<ClassName> subItems = null;
    var objController = new StorageManager();
    subItems = objController.GetAllItemPreAssemblyPickupLocationItem();
    System.Func<stringIEnumerable<ClassName>, 
                IEnumerable<ClassName>> getItems = null;
    getItems = (strItemId, Items) => Items
.Where(x => x.ItemId == strItemId).
    SelectMany(x => getItems(x.SubItemId, Items)
.Concat(new ClassName[] { x }));
    var allItems = getItems(ItemId, subItems);
    if (allItems != null && allItems.Count() > 0)
        allItems.ToList().ForEach(x => { ItemIds.Add(x.ItemId);
 ItemIds.Add(x.SubItemId); });
    return ItemIds.Distinct().ToList();
 
}

Monday 21 September 2015

How to change the ForeColor of individual items in a ComboBox in C# Winforms

How to change the ForeColor of individual items in a ComboBox in C# Winforms

//SET COMBOBOXBOX PROPERTY TO DRAWMODE =>Ownerdrawfixed or Ownerdravariable
 private void cmbColour_DrawItem(object sender, DrawItemEventArgs e)
        {
            try
            {    
               YourClassName  presenter =new YourClassName();    
                presenter.DrawColourCode(sender, e);
            }
            catch (Exception ex)
            {
           
                    throw ex;
             
            }
        }

   public void DrawColourCode(object sender, DrawItemEventArgs e)
        {

//HERR I AM USING DATABASE TO FETCH THE COLOURCODE, ,FOREGROUDCOLOUR //AND BACKGROUNDCOLOUR
            float size = 10;
            System.Drawing.Font myFont;
            string backgroundColorCode = Convert.ToString(((sender as ComboBox).Items[e.Index] as ClassEntityName).BackgroundColourCode);
            // Draw the background of the item.
            e.DrawBackground();
            Brush BrsFore = null;
            // Create a square filled with the item color. Vary the size
            // of the rectangle based on the length of the item name.
            Rectangle rectangle = new Rectangle(1, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height);
            if (!string.IsNullOrEmpty(backgroundColorCode))
            {
                Color backgroundcolorCode = HexToColor(backgroundColorCode);
                e.Graphics.FillRectangle(new SolidBrush(backgroundcolorCode), rectangle);
            }
            else
            {
                Color backgroundcolorCode = HexToColor("#FFFFFF");
                e.Graphics.FillRectangle(new SolidBrush(backgroundcolorCode), rectangle);
            }
            // Draw each string in the array, using a different size, color,
            // and font for each item.
            var kitColour = ((sender as ComboBox).Items[e.Index] as ClassEntityName).ColourCode;
            string foreColour = ((sender as ComboBox).Items[e.Index] as ClassEntityName).ForegroundColourCode;
            if (!string.IsNullOrEmpty(foreColour))
            {
                Color foregroundcolorCode = HexToColor(foreColour);
                BrsFore = new SolidBrush(foregroundcolorCode);

                myFont = new Font("Times New Roman", size, FontStyle.Regular);
                if (!string.IsNullOrEmpty(((sender as ComboBox).Name)))
                {
                    e.Graphics.DrawString(kitColour, myFont, BrsFore, new RectangleF(e.Bounds.X + rectangle.Width - 115, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
                }
                else
                {
                    e.Graphics.DrawString(kitColour, myFont, BrsFore, new RectangleF(e.Bounds.X + rectangle.Width - 108, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
                }
            }
            // Draw the focus rectangle if the mouse hovers over an item.
            e.DrawFocusRectangle();
        }

 public static Color HexToColor(string hexColor)
        {
            //Remove # if present
            if (hexColor.IndexOf('#') != -1)
                hexColor = hexColor.Replace("#", "");

            int red = 0;
            int green = 0;
            int blue = 0;

            if (hexColor.Length == 6)
            {
                //#RRGGBB
                red = int.Parse(hexColor.Substring(0, 2), NumberStyles.AllowHexSpecifier);
                green = int.Parse(hexColor.Substring(2, 2), NumberStyles.AllowHexSpecifier);
                blue = int.Parse(hexColor.Substring(4, 2), NumberStyles.AllowHexSpecifier);
            }
            else if (hexColor.Length == 3)
            {
                //#RGB
                red = int.Parse(hexColor[0].ToString() + hexColor[0].ToString(), NumberStyles.AllowHexSpecifier);
                green = int.Parse(hexColor[1].ToString() + hexColor[1].ToString(), NumberStyles.AllowHexSpecifier);
                blue = int.Parse(hexColor[2].ToString() + hexColor[2].ToString(), NumberStyles.AllowHexSpecifier);
            }

            return Color.FromArgb(red, green, blue);
        }

Wednesday 28 January 2015

Sibling number and get largest number in the family using C#

Following code will illustrate to get largest number from given sibling numbers.

static void Main(string[] args)
        {
            int n2 = 3456789;
            var digits = new List<int>();
            int temp = 0;
            for (; n2 != 0; n2 /= 10)
            {
                digits.Add(n2 % 10);
            }
            var arr2 = digits;
            for (int num = 0; num < arr2.Count; num++)
            {
                for (int sort = 0; sort < arr2.Count - 1; sort++)
                {
                    if (arr2[sort] < arr2[sort + 1])
                    {
                        temp = arr2[sort + 1];
                        arr2[sort + 1] = arr2[sort];
                        arr2[sort] = temp;
                    }
                }
            }

            for (int i = 0; i < arr2.Count; i++)
                Console.Write(arr2[i] + "");
            Console.ReadKey();
        }

Sibling number and get largest number in the family using C#

Sunday 4 January 2015

Temporarily avoid foreign key constraint in sql server


We can avoid foreign key constraint temporarly by using following code

--ALTER TABLE Station NOCHECK CONSTRAINT MyConstraint

ALTER TABLE TABLE_NAME NOCHECK CONSTRAINT FK_CONSTRAINTNAME

select * from TABLE_NAME where COLUMN_NAME='UPDATEDVALUE'

update TABLE_NAME set COLUMN_NAME='UPDATEDVALUE' where COLUMN_NAME='WHERECONDITION_VALUE'

select * from TABLE_NAME where COLUMN_NAME='WHERECONDITION_VALUE'
-- Enable single constraint

ALTER TABLE TABLE_NAME CHECK CONSTRAINT FK_CONSTRAINTNAME

Temporarily avoid foreign key constraint in sql server