26 Haz

C# Indent String With Spaces

This example shows how to indent strings using method for padding in C#. “String.PadLeft” static method is used to repeat spaces.

Usage:

            Console.WriteLine(Indent(0) + "Versions");
            Console.WriteLine(Indent(2) + "Version 1");
            Console.WriteLine(Indent(4) + "Version 1.1");
            Console.WriteLine(Indent(4) + "Version 1.2");
            Console.WriteLine(Indent(6) + "Version 1.2.1");
            Console.WriteLine(Indent(2) + "Version 2");
            Console.WriteLine(Indent(4) + "Version 2.1");
            Console.WriteLine(Indent(4) + "Version 2.2");
        public static string Indent(int count)
        {
            return "".PadLeft(count);
        }

Output is like below.

Versions
  Version 1
    Version 1.1
    Version 1.2
      Version 1.2.1
  Version 2
    Version 2.1
    Version 2.2
25 Haz

C# Update A GUI Element From Another Thread

This examples shows how to update a user interface element from another thread. To do this, we must update to the user interface in user interface thread. Invoke method in Control class implements this task.

Sample Usage:

            //Usage 1
            txtSampleTextBox.Invoke(new Action(() => txtSampleTextBox.Text = "Sample Text"));

            //Usage 2
            txtSampleTextBox.Invoke((MethodInvoker)(() => txtSampleTextBox.Text = "Sample Text"));
24 Haz

C# ReadOnly Wrapper Collection

This examples shows how to create a readonly wrapper collection. If you have a private collection in a class and you only want to access as readonly, you can use AsReadOnly method in “List” generic class in System.Collections.Generic namespace. This method says that “I don’t want people to be able to modify the list content.” It returns a read-only wrapper collection.

Also you can expose your list through a property of type IEnumerable. ( “AsReadOnly” method is mentioned above is prefered.)

Sample Usage:

    public class ReadOnlyCollectionExample
    {
        private List<string> _collection = new List<string>();
        public IList<string> Collection1
        {
            get
            {
                return _collection.AsReadOnly();
            }
        }

        // Adding is not allowed - only iteration
        public IEnumerable<string> Collection2
        {
            get { return _collection; }
        }
    }

23 Haz

C# Disable Or Hide Close Button(X) in Windows Form

This examples show how to hide/disable close button(X) in C#. To achieve this, we must override the CreateParams method in Form class.

Disable Close Button:

            
        //Disable close button
        private const int CP_DISABLE_CLOSE_BUTTON = 0x200;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ClassStyle = cp.ClassStyle | CP_DISABLE_CLOSE_BUTTON;
                return cp;
            }
        }

Disable Close Button

Remove The Entire System Menu:

        //remove the entire system menu:
        private const int WS_SYSMENU = 0x80000;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.Style &= ~WS_SYSMENU;
                return cp;
            }
        }            

Remove Entire System Menu