Thursday, July 31, 2014

How to remove chat history for a single contact from Skype?

This post is going to be a little strange compared with other posts on this blog.
In order to test some functionality on a web application I had to change the calendar date in future on my local machine.
In the while, I was using Skype and conversations were recording as of being on 8th of August (the date I experienced this). Then I return 'back from time' on current date. Well, from that time on Skype was behaving weird because it showed me conversations ordered by timespan ascending.
Well that is when you will got messed up :)

As Skype only allows you to delete all history conversation at once I had to search for a way to delete only those conversation that were 'in future' so Skype will show me last chat discussions on last row ;)

Searched around and found this solution.(it will require a little knowledge of SQL)

  1. Quit from Sype - required - otherwise you will have a sql error - database locked
  2. Go to C:\Users\PROFILE NAME\AppData\Roaming\Skype\SKYPE LOGIN ID and make a backup of main.db
  3. Go to http://sqlitebrowser.org/ and download the exe and install it on your machine.
  4. Go to: http://www.onlineconversion.com/unix_time.htm and get current timespan. - you will need it for sql query
  5. Open  SqLite and then go to OpenDatabase and choose the same database mentioned at 2)
  6. In order to make sure what you want to delete you could execute the next query   
  • SELECT * FROM "Messages" where timestamp > '1406808732'   (for timestamp you could put the one obtained at 4)  or if you want to check of an author use: author = 'authorId' ) authorId is skype Name
 If you are happy with the results that you see then execute same script with delete command:
  • DELETE FROM "Messages" where timestamp > '1406808732' --or use: (author = 'authorId')

Note that you could also filter the results by author/from_dispname or other columns of same Message table. AuthorIds could be taken from Contacts table and then skypename column .

Cheers.

Tuesday, May 27, 2014

How to remove a wrong playlist from Test Explorer in visual studio 2012?

After you create several playlists for your failing unit tests or a custom set of unit tests there will come the time when you want to remove them from the list.
So, in case you will be looking in Text Explorer(Visual Studio Professional 2012) for a way to remove/delete a playlist from your list this is what you will have to do:

1. Close solution
2. Locate your playlist where it is saved and then delete it, easy - isn't it? :)
3. Reopen your solution and re build your solution. Everything should be fine. The playlist should not appear anymore.

Caution: If you delete the playlist while the solution is still opened and then build you will get this nice error:

------ Discover test started ------
Error reading playlist file 'C:\Users\xxx\MyPlaylist.playlist'.


So that is why you need first to close solution.
I hope this helped you, I looked around and found no answer for this. I thought that as a playlist is created from Text Explorer interface it should also be removed from same place.

Have a nice day and leave a comment if this helped you.

Friday, April 25, 2014

How can I get the list of available databases on a SQL server instance?

Whenever you want to get the list of available databases on an instance you could use this query. You could also change it as will fit you better. This query it is helpful when you need a list of databases and the list is big, of course.

Query that runs well for Sql Server 2012:
SELECT * 
FROM sys.databases d
WHERE d.name NOT IN ('master', 'tempdb', 'model', 'msdb', 'ReportServer', 'ReportServerTempDB');
--or you could use a simpler condition which in my opinion is not so relevant 
--WHERE d.database_id > 4

Otherwise you could also use:
EXEC sp_databases

Cheers, Adrian

Friday, April 11, 2014

How to call for a dynamic OrderBy method for a LINQ?

As I wanted to do some experiences in Asp.Mvc 4 I needed a nice way to generate tables using Ajax calls for showing rows, edit, delete and create new item as well with filtering and paging functions
For that I have found this nice package jTable. You could go to http://www.jtable.org/ where you can see a demo by yourself.

Now that I have introduced the scenario I came to an issue. When implementing the sorting of columns by pressing on a column I had as an input parameter string jtSorting which had as a value: "Name ASC", so column name and ordering. Then in my controller's action I had a LINQ query that accessed also the OrderBy extension method which needs exactly the name of property. So I needed a method to order dynamically by property as a string.

Happily I found a solution. Here are my helper classes that I used in final extension method.

public static class OrderHelper
    {
        public class ColumnToSort
        {
            public string ColumnName { get; set; }
            public SortDirection Direction { get; set; }

            public ColumnToSort()
            { }

            public ColumnToSort(string columnName, SortDirection direction)
            {
                ColumnName = columnName;
                Direction = direction;
            }
        }

        public static ColumnToSort SplitJqueryFormatColumn(string sortingColumn) //assume we have as input: "Name ASC"
        {
            ColumnToSort columnToSort = new ColumnToSort();

            if (sortingColumn != null)
            {
                string[] parts = sortingColumn.Split(' ');
                if (parts.Length == 2)
                {
                    columnToSort.ColumnName = parts[0];
                    columnToSort.Direction = parts[1].ToLower() == "asc" ? SortDirection.Ascending : SortDirection.Descending;
                }
            }

            return columnToSort;
        }
    }

This is the extension method that will Order dynamically by a column string value and an sorting order.

public static class OrderExt
    {
        public static IOrderedQueryable< T > Order< T>(this IQueryable< T> source, string propertyName, SortDirection descending, bool anotherLevel = false)
        {
            var param = Expression.Parameter(typeof(T), string.Empty);
            var property = Expression.PropertyOrField(param, propertyName);
            var sort = Expression.Lambda(property, param);

            var call = Expression.Call(
                typeof(Queryable),
                (!anotherLevel ? "OrderBy" : "ThenBy") +
                (descending == SortDirection.Descending ? "Descending" : string.Empty),
                new[] { typeof(T), property.Type },
                source.Expression,
                Expression.Quote(sort));

            return (IOrderedQueryable< T>)source.Provider.CreateQuery< T>(call);
        }
    }

Here is how to use the Order extension method for List Action that is needed for JTable in ASP.NET MVC 4:

[HttpPost]
        public JsonResult List(int jtStartIndex, int jtPageSize, string jtSorting = null)
        {
            try
            {
                int totalItemsNumber = db.Authors.Count();
        
                OrderHelper.ColumnToSort columnToSort = new OrderHelper.ColumnToSort();
                if (jtSorting != null)
                    columnToSort = OrderHelper.SplitJqueryFormatColumn(jtSorting);
                else
                    columnToSort = new OrderHelper.ColumnToSort("Name", SortDirection.Ascending);

                List< author> authors = db.Authors
                                         .Order(columnToSort.ColumnName, columnToSort.Direction)
                                         .Skip(jtStartIndex)
                                         .Take(jtPageSize)
                                         .ToList();

                return Json(new { Result = "OK", Records = authors, TotalRecordCount = totalItemsNumber });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

Hope it solved your issue, too.

Source for the extension method found at: linq-to-entities-dynamic-sorting.

App_Code classes not available in Asp.Net Mvc 4 with Microsoft Visual Studio 2012

As in the last few weeks I have started to learn Asp.Net MVC 4 I found few issues. I want to tell you about one pretty awkward.

Just for you to know I have created a project "ASP.NET MVC 4 Web Application" and then as project template I've chosen "Internet Application".


Now: as I wanted to add some of my helper classes and other utilities I created a "App_Code" folder.


Later as I said, I added few classes in that folder. Then I was surprised for I could not use those classes from Controller's folder.
I've tried to change namespace,changing from  MyDemoProject.App_Code to just MyDemoProject. No success :)

So as App_Code classes were not available(accessible) I've searched a little around and I found this solution: you have to go at Properties section of the file and then for Build Action change from Content to Compile. That is it ;)


Of course, you have to bring in the namespace MyDemoProject or MyDemoProject.App_Code as it fits your scenario.

Hope it helped you, too.

Monday, January 27, 2014

How to loop through a collection that supports IEnumerable?

Whenever you get to this issue, though it seems a really easy job to do here 2 ways how you can iterate on a collection that supports IEnumerable:

First one using a foreach statement:

foreach (var item in collection)
{
    // play with your item
}

I suggest that always when you know your object types of collection to use that type instead of var. I always try to avoid var type. It is something that my team lead advised me and I came to realize it is a good practice.

Then second way to do it is using for statement. Whenever you have a collection and you need to iterate it and according to some conditions some elements might have to be removed you cannot use foreach because you will have a nice error saying: "Collection was modified; enumeration operation may not execute" :) So that is why you need to use for statement.

for(int i = 0; i < collection.Count(); i++) 
{
    string str1 = collection.ElementAt(i);
    // play with your item
}

There might be other ways to iterate, like using GetEnumerator but is enough, for the moment :)
Have a nice day.

Monday, January 20, 2014

How to make a custom richTextBox control in winforms with C#?

During the last week I had to make a custom control that should have had a richTextBox control with a toolbar that contains: a dropdown of fonts and one with font sizes plus 3 buttons(which in fact are checkboxes with Appearance of a button) that will emulate 'make bold', 'make italic', 'make underline'. Of course there could be other improvements but client was happy with only these controls.


I will try to explain you which are the steps in the next lines:

- create a new control - ExtraRichTextBoxUC
- add 2 comboboxes with drop down style set on DropDownList: cmbFontFamily, cmbFontSize
- add 3 checkBoxes with Appearance on Button: ckbBold, ckbItalic, ckbUnderline
- finally you add a richTextBox control: txtFunctionality

Generate event methods for controls as follows: cmbFontFamily_SelectedIndexChanged, cmbFontSize_SelectedIndexChanged, ckbBold_CheckedChanged, ckbItalic_CheckedChanged, ckbUnderline_CheckedChanged.

I think that one of the most important methods from bellow is ChangeFontStyleForSelectedText. You have to understand that when you want to change font for a text you must iterate character by character and change font. First I thought that a style can be applied a word or a phrase. To issue is that styles are overwritten in such a case so you must iterate each letter.
So for each text that you want to change font you copy in a memory RichTextBox(so you won't cause flickering on current richTextBox) and set new font. Finally that rtf generated you assign to current richTextBox.

private void ChangeFontStyleForSelectedText(string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle)
        {
            _maskChanges = true;
            try
            {
                int txtStartPosition = txtFunctionality.SelectionStart;
                int selectionLength = txtFunctionality.SelectionLength;
                if (selectionLength > 0)
                    using (RichTextBox txtTemp = new RichTextBox())
                    {
                        txtTemp.Rtf = txtFunctionality.SelectedRtf;
                        for (int i = 0; i < selectionLength; ++i)
                        {
                            txtTemp.Select(i, 1);
                            txtTemp.SelectionFont = RenderFont(txtTemp.SelectionFont, familyName, emSize, fontStyle, enableFontStyle);
                        }

                        txtTemp.Select(0, selectionLength);
                        txtFunctionality.SelectedRtf = txtTemp.SelectedRtf;
                        txtFunctionality.Select(txtStartPosition, selectionLength);
                    }
            }
            finally
            {
                _maskChanges = false;
            }
        }
Next method important that I had to work a little on is RenderFont:
/// 
        /// Changes a font from originalFont appending other properties
        /// 
        /// Original font of text
        /// Target family name
        /// Target text Size
        /// Target font style
        /// true when enable false when disable
        /// A new font with all provided properties added/removed to original font
        private Font RenderFont(Font originalFont, string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle)
        {
            if (fontStyle.HasValue && fontStyle != FontStyle.Regular && fontStyle != FontStyle.Bold && fontStyle != FontStyle.Italic && fontStyle != FontStyle.Underline)
                throw new System.InvalidProgramException("Invalid style parameter to ChangeFontStyleForSelectedText");

            Font newFont;
            FontStyle? newStyle = null;
            if (fontStyle.HasValue)
            {
                if (fontStyle.HasValue && fontStyle == FontStyle.Regular)
                    newStyle = fontStyle.Value;
                else if (originalFont != null && enableFontStyle.HasValue && enableFontStyle.Value)
                    newStyle = originalFont.Style | fontStyle.Value;
                else
                    newStyle = originalFont.Style & ~fontStyle.Value;
            }

            newFont = new Font(!string.IsNullOrEmpty(familyName) ? familyName : originalFont.FontFamily.Name,
                                emSize.HasValue ? emSize.Value : originalFont.Size,
                                newStyle.HasValue ? newStyle.Value : originalFont.Style);
            return newFont;
        }

Observation: you must highlight the toolbar for your current position. For that after you load the rtf text you must position on first position of your text and according to that font you show font family, font size, if is bold - underline or italic.

In order to catch key combination of Ctrl+B, Ctrl+I, Ctrl+U you will use _KeyDown event of richTextBox control. For Control+I you must use a trick. Setting SuppressKeyPress = true will avoid executing 'Insert TAB' :)

In order to avoid flickering of text in richTextBox control especially when you delete last letter of control or when you try to detect font for first letter of textBox you have to use another trick. Use Suspend/Resume methods of next extension.

namespace System.Windows.Forms
{
    public static class ControlExtensions
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern bool LockWindowUpdate(IntPtr hWndLock);

        //Method used agains flickering
        public static void Suspend(this Control control)
        {
            LockWindowUpdate(control.Handle);
        }

        public static void Resume(this Control control)
        {
            LockWindowUpdate(IntPtr.Zero);
        }
    }
}

I hope that this will help you a lot. When I searched around I did not found such as a control that will make a simple richTextBox with simple toolbar controls. If you encounter issues implementing it feel free to ask. I could also share code but I don't know a good reliable service to share files and not to be removed after a while :-? Do you?

Full code of control:

public partial class ExtraRichTextBoxUC : UserControl
    {
        private bool _maskChanges;
        public ExtraRichTextBoxUC()
        {
            InitializeComponent();
            LoadData();

            DisplayData();
        }

        private void DisplayData()
        {
//dummy data to preview
txtFunctionality.Rtf = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Tahoma;}{\f1\fnil\fcharset0 Times New Roman;}}\viewkind4\uc1\pard\b\f0\fs18 W\b0 indows 8.1, Windows Server 2012 R2, \b Windows\b0  8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 \fs24 R2 (Server \fs18 Core Role \ul supported with SP1 \ulnone or later; Itanium \fs24 not supported\fs18 ).NET Framework does not support all versions of \fs26 every\f1  platform. For \f0 a list of \fs18 the supported versions, see .NET Framework System Requirements.\par}";

            GoToFirstPositionAndHighlightToolbar();
        }

        private void LoadData()
        {
            LoadRichTextEditorData();
        }

        private void LoadRichTextEditorData()
        {
            string[] availableFontFamilies;

            //check to see if fonts are installed on machine
            try
            {
                availableFontFamilies = new string[] { 
                new FontFamily("Arial").Name, 
                new FontFamily("Microsoft Sans Serif").Name, 
                new FontFamily("Tahoma").Name, 
                new FontFamily("Times New Roman").Name 
                };
            }
            catch (ArgumentException e)
            {
                throw new Exception("Font not present: " + e.Message);
            }

            List < object > availableFontSizes = new List < object >();

            for (float i = 8; i <= 14; i++)
                availableFontSizes.Add(i);

            cmbFontFamily.Items.AddRange(availableFontFamilies);
            cmbFontSize.Items.AddRange(availableFontSizes.ToArray());
        }

        private void ckbBold_CheckedChanged(object sender, EventArgs e)
        {
            if (_maskChanges)
                return;

            ChangeOrSetFont(string.Empty, null, FontStyle.Bold, ckbBold.Checked);
            txtFunctionality.Focus();
        }

        private void ckbItalic_CheckedChanged(object sender, EventArgs e)
        {
            if (_maskChanges)
                return;

            ChangeOrSetFont(string.Empty, null, FontStyle.Italic, ckbItalic.Checked);
            txtFunctionality.Focus();
        }

        private void ckbUnderline_CheckedChanged(object sender, EventArgs e)
        {
            if (_maskChanges)
                return;

            ChangeOrSetFont(string.Empty, null, FontStyle.Underline, ckbUnderline.Checked);
            txtFunctionality.Focus();
        }

        private void cmbFontFamily_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_maskChanges)
                return;

            ChangeOrSetFont(cmbFontFamily.SelectedItem.ToString(), null, null, null);
            txtFunctionality.Focus();
        }

        private void cmbFontSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_maskChanges)
                return;
            ChangeOrSetFont(null, float.Parse(cmbFontSize.SelectedItem.ToString()), null, null);
            txtFunctionality.Focus();
        }

        private void ChangeOrSetFont(string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle)
        {
            if (txtFunctionality.SelectionType == RichTextBoxSelectionTypes.Empty)
            {
                SetSelectionFont(familyName, emSize, fontStyle, enableFontStyle);
            }
            else
            {
                ChangeFontStyleForSelectedText(familyName, emSize, fontStyle, enableFontStyle);
            }
        }

        private void SetSelectionFont(string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle)
        {
            Font renderedFont = RenderFont(txtFunctionality.SelectionFont, familyName, emSize, fontStyle, enableFontStyle);
            txtFunctionality.SelectionFont = renderedFont;
        }

        private void ChangeFontStyleForSelectedText(string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle)
        {
            _maskChanges = true;
            try
            {
                int txtStartPosition = txtFunctionality.SelectionStart;
                int selectionLength = txtFunctionality.SelectionLength;
                if (selectionLength > 0)
                    using (RichTextBox txtTemp = new RichTextBox())
                    {
                        txtTemp.Rtf = txtFunctionality.SelectedRtf;
                        for (int i = 0; i < selectionLength; ++i)
                        {
                            txtTemp.Select(i, 1);
                            txtTemp.SelectionFont = RenderFont(txtTemp.SelectionFont, familyName, emSize, fontStyle, enableFontStyle);
                        }

                        txtTemp.Select(0, selectionLength);
                        txtFunctionality.SelectedRtf = txtTemp.SelectedRtf;
                        txtFunctionality.Select(txtStartPosition, selectionLength);
                    }
            }
            finally
            {
                _maskChanges = false;
            }
        }

        /// 
        /// Changes a font from originalFont appending other properties
        /// 
        /// Original font of text
        /// Target family name
        /// Target text Size
        /// Target font style
        /// true when enable false when disable
        /// A new font with all provided properties added/removed to original font
        private Font RenderFont(Font originalFont, string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle)
        {
            if (fontStyle.HasValue && fontStyle != FontStyle.Regular && fontStyle != FontStyle.Bold && fontStyle != FontStyle.Italic && fontStyle != FontStyle.Underline)
                throw new System.InvalidProgramException("Invalid style parameter to ChangeFontStyleForSelectedText");

            Font newFont;
            FontStyle? newStyle = null;
            if (fontStyle.HasValue)
            {
                if (fontStyle.HasValue && fontStyle == FontStyle.Regular)
                    newStyle = fontStyle.Value;
                else if (originalFont != null && enableFontStyle.HasValue && enableFontStyle.Value)
                    newStyle = originalFont.Style | fontStyle.Value;
                else
                    newStyle = originalFont.Style & ~fontStyle.Value;
            }

            newFont = new Font(!string.IsNullOrEmpty(familyName) ? familyName : originalFont.FontFamily.Name,
                                emSize.HasValue ? emSize.Value : originalFont.Size,
                                newStyle.HasValue ? newStyle.Value : originalFont.Style);
            return newFont;
        }

        private void txtFunctionality_SelectionChanged(object sender, EventArgs e)
        {
            if (_maskChanges)
                return;

            if (string.IsNullOrEmpty(txtFunctionality.Text))
            {
                //clear all text with its ex-formatting
                txtFunctionality.Clear();
                LoadAndSetDefaultFont();
            }
            else
            {
                ScanSelectedTextAndHighlightToolbar();
            }
        }

        private void txtFunctionality_KeyDown(object sender, KeyEventArgs e)
        {
            if (_maskChanges)
                return;

            if (e.Control && e.KeyCode == Keys.B)
            {
                ckbBold.Checked = !ckbBold.Checked;
                e.Handled = true;
            }
            else if (e.Control && e.KeyCode == Keys.I)
            {
                ckbItalic.Checked = !ckbItalic.Checked;
                e.Handled = true;
                e.SuppressKeyPress = true; //avoid executing 'Insert TAB' for CTRL + I
            }
            else if (e.Control && e.KeyCode == Keys.U)
            {
                ckbUnderline.Checked = !ckbUnderline.Checked;
                e.Handled = true;
            }
        }

        private void GoToFirstPositionAndHighlightToolbar()
        {
            _maskChanges = true;
            try
            {
                if (!string.IsNullOrEmpty(txtFunctionality.Text))
                {
                    txtFunctionality.Suspend();
                    txtFunctionality.Select(0, 1);

                    using (RichTextBox txtTemp = new RichTextBox())
                    {
                        txtTemp.Rtf = txtFunctionality.SelectedRtf;
                        Font currFont = txtTemp.SelectionFont;

                        HighlightToolbar(currFont.FontFamily.Name, currFont.Size, currFont.Bold, currFont.Italic, currFont.Underline);
                    }
                    txtFunctionality.Select(0, 0);
                    txtFunctionality.Resume();
                }
                else
                {
                    LoadAndSetDefaultFont();
                }
            }
            finally
            {
                _maskChanges = false;
            }
        }
        
        private Font GetFontFromToolbar()
        {
            FontStyle toolbarFontStyle = new FontStyle();
            if (ckbBold.Checked)
                toolbarFontStyle |= FontStyle.Bold;
            if (ckbItalic.Checked)
                toolbarFontStyle |= FontStyle.Italic;
            if (ckbUnderline.Checked)
                toolbarFontStyle |= FontStyle.Underline;

            Font font = new Font(cmbFontFamily.SelectedItem.ToString(), (float)cmbFontSize.SelectedItem, toolbarFontStyle);
            return font;
        }

        //could be changed into an extentension 
        public string TrimLastChar(string text)
        {
            if (text.Length >= 1)
                return text.Substring(0, text.Length - 1);
            else
                return text;
        }

        private void LoadAndSetDefaultFont()
        {
            Font font = txtFunctionality.Font;
            HighlightToolbar(font.FontFamily.Name, font.Size, font.Bold, font.Italic, font.Underline);

            SetSelectionFont(font.FontFamily.Name, font.Size, font.Style, null);
        }

        private void ScanSelectedTextAndHighlightToolbar()
        {
            _maskChanges = true;
            try
            {
                if (txtFunctionality.SelectionType == RichTextBoxSelectionTypes.Empty)
                {
                    int selectionStart = txtFunctionality.SelectionStart != 0 ? txtFunctionality.SelectionStart - 1 : 0;
                    int selectionEnd = txtFunctionality.SelectionStart;

                    txtFunctionality.Suspend();
                    //case when passes to a new line - it looses font style so must get from Tooolbar
                    if (TrimLastChar(txtTextContent.Text).EndsWith("\n"))
                    {
                        txtTextContent.Select(selectionStart, 1);
                        txtTextContent.SelectionFont = GetFontFromToolbar();
                        txtTextContent.Select(selectionEnd, 0);
                    }
                    else
                    {
                        txtTextContent.Select(selectionStart, 1);
                        using (RichTextBox txtTemp = new RichTextBox())
                        {
                            txtTemp.Rtf = txtTextContent.SelectedRtf;
                            Font currFont = txtTemp.SelectionFont;

                            HighlightToolbar(currFont.FontFamily.Name, (float)Math.Truncate(currFont.Size), currFont.Bold, currFont.Italic, currFont.Underline);
                            txtTextContent.SelectionFont = currFont;
                        }
                        txtTextContent.Select(selectionEnd, 0);
                    }
                    txtFunctionality.Resume();
                }
                else
                    if (!string.IsNullOrEmpty(txtFunctionality.SelectedText))
                    {
                        int txtStartPosition = txtFunctionality.SelectionStart;
                        int selectionLength = txtFunctionality.SelectionLength;

                        if (selectionLength > 0)
                            using (RichTextBox txtTemp = new RichTextBox())
                            {
                                txtTemp.Rtf = txtFunctionality.SelectedRtf;

                                if (selectionLength < 2)
                                {
                                    FontFamily firstCharFontFamily = txtTemp.SelectionFont.FontFamily;
                                    float firstCharSize = txtTemp.SelectionFont.Size;
                                    bool isBold = txtTemp.SelectionFont.Bold;
                                    bool isItalic = txtTemp.SelectionFont.Italic;
                                    bool isUnderline = txtTemp.SelectionFont.Underline;

                                    HighlightToolbar(firstCharFontFamily.Name, firstCharSize, isBold, isItalic, isUnderline);
                                }
                                else
                                {
                                    txtTemp.Select(0, 1);
                                    FontFamily firstCharFontFamily = txtTemp.SelectionFont.FontFamily;
                                    float firstCharSize = txtTemp.SelectionFont.Size;
                                    bool isBold = txtTemp.SelectionFont.Bold;
                                    bool isItalic = txtTemp.SelectionFont.Italic;
                                    bool isUnderline = txtTemp.SelectionFont.Underline;

                                    bool sameFontFamily = true, sameFontSize = true;

                                    for (int i = 1; i < selectionLength; i++)
                                    {
                                        txtTemp.Select(i, 1);
                                        sameFontFamily = txtTemp.SelectionFont.FontFamily.Name == firstCharFontFamily.Name;
                                        sameFontSize = txtTemp.SelectionFont.Size == firstCharSize;
                                        isBold = isBold && txtTemp.SelectionFont.Bold;
                                        isItalic = isItalic && txtTemp.SelectionFont.Italic;
                                        isUnderline = isUnderline && txtTemp.SelectionFont.Underline;

                                        if (!sameFontFamily && !sameFontSize && !isBold && !isItalic && !isUnderline)
                                            break;
                                    }

                                    HighlightToolbar(sameFontFamily ? firstCharFontFamily.Name : string.Empty,
                                        sameFontSize ? firstCharSize : (float?)null,
                                        isBold, isItalic, isUnderline);
                                }
                            }
                    }
            }
            finally
            {
                _maskChanges = false;
            }
        }

        private void HighlightToolbar(string commonFamilyName, float? emSize, bool? isBold, bool? isItalic, bool? isUnderline)
        {
            if (!string.IsNullOrEmpty(commonFamilyName))
                cmbFontFamily.SelectedItem = commonFamilyName;
            else
                cmbFontFamily.SelectedItem = null;

            if (emSize.HasValue)
                cmbFontSize.SelectedItem = (float)Math.Truncate(emSize.Value);
            else
                cmbFontSize.SelectedItem = null;

            if (isBold.HasValue)
            {
                ckbBold.CheckState = isBold.Value ? CheckState.Checked : CheckState.Unchecked;
            }
            else
                ckbBold.CheckState = CheckState.Unchecked;

            if (isItalic.HasValue)
            {
                ckbItalic.CheckState = isItalic.Value ? CheckState.Checked : CheckState.Unchecked;
            }
            else
                ckbItalic.CheckState = CheckState.Unchecked;

            if (isUnderline.HasValue)
            {
                ckbUnderline.CheckState = isUnderline.Value ? CheckState.Checked : CheckState.Unchecked;
            }
            else
                ckbUnderline.CheckState = CheckState.Unchecked;
        }
    }
}

namespace System.Windows.Forms
{
    public static class ControlExtensions
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern bool LockWindowUpdate(IntPtr hWndLock);

        //Method used agains flickering
        public static void Suspend(this Control control)
        {
            LockWindowUpdate(control.Handle);
        }

        public static void Resume(this Control control)
        {
            LockWindowUpdate(IntPtr.Zero);
        }
    }