Click here to Skip to main content
15,918,742 members
Home / Discussions / C#
   

C#

 
AnswerRe: MSMQ Receiving messages Help!!! Pin
Jimmanuel5-Jan-10 8:52
Jimmanuel5-Jan-10 8:52 
GeneralRe: MSMQ Receiving messages Help!!! Pin
faheemnadeem5-Jan-10 17:57
faheemnadeem5-Jan-10 17:57 
AnswerRe: MSMQ Receiving messages Help!!! Pin
Jimmanuel6-Jan-10 1:09
Jimmanuel6-Jan-10 1:09 
GeneralRe: MSMQ Receiving messages Help!!! Pin
faheemnadeem6-Jan-10 4:05
faheemnadeem6-Jan-10 4:05 
GeneralRe: MSMQ Receiving messages Help!!! Pin
Jimmanuel6-Jan-10 4:52
Jimmanuel6-Jan-10 4:52 
GeneralRe: MSMQ Receiving messages Help!!! Pin
faheemnadeem6-Jan-10 5:58
faheemnadeem6-Jan-10 5:58 
GeneralRe: MSMQ Receiving messages Help!!! Pin
Jimmanuel6-Jan-10 6:57
Jimmanuel6-Jan-10 6:57 
QuestionRead data from a SerialPort: cross-thread not true Pin
AlexB474-Jan-10 18:29
AlexB474-Jan-10 18:29 
AnswerMessage Closed Pin
4-Jan-10 21:50
stancrm4-Jan-10 21:50 
GeneralRe: Read data from a SerialPort: cross-thread not true Pin
AlexB475-Jan-10 1:46
AlexB475-Jan-10 1:46 
AnswerRe: Read data from a SerialPort: cross-thread not true Pin
Luc Pattyn5-Jan-10 2:36
sitebuilderLuc Pattyn5-Jan-10 2:36 
GeneralRe: Read data from a SerialPort: cross-thread not true Pin
AlexB475-Jan-10 3:44
AlexB475-Jan-10 3:44 
GeneralRe: Read data from a SerialPort: cross-thread not true Pin
Luc Pattyn5-Jan-10 3:52
sitebuilderLuc Pattyn5-Jan-10 3:52 
Questionminimizing and maximizing Pin
ummu 24-Jan-10 18:13
ummu 24-Jan-10 18:13 
AnswerMessage Closed Pin
4-Jan-10 21:48
stancrm4-Jan-10 21:48 
GeneralRe: minimizing and maximizing Pin
ummu 24-Jan-10 22:21
ummu 24-Jan-10 22:21 
QuestionHow to bookmark in ritchtextbox Pin
SuhebAhamd4-Jan-10 18:12
SuhebAhamd4-Jan-10 18:12 
AnswerRe: How to bookmark in ritchtextbox Pin
Pawel Gielmuda5-Jan-10 0:16
Pawel Gielmuda5-Jan-10 0:16 
AnswerRe: How to bookmark in ritchtextbox Pin
Eddy Vluggen5-Jan-10 10:29
professionalEddy Vluggen5-Jan-10 10:29 
QuestionWhat is the difference between Activator.CreateInstance and new operator ? Pin
md_azy4-Jan-10 17:45
md_azy4-Jan-10 17:45 
AnswerRe: What is the difference between Activator.CreateInstance and new operator ? Pin
_Maxxx_4-Jan-10 17:52
professional_Maxxx_4-Jan-10 17:52 
QuestionPrint Fit to Page Image Pin
rina184-Jan-10 15:45
rina184-Jan-10 15:45 
AnswerRe: Print Fit to Page Image Pin
AspDotNetDev4-Jan-10 17:56
protectorAspDotNetDev4-Jan-10 17:56 
QuestionMulti-color text in single cell of datagridview in C# Pin
auaryamane0074-Jan-10 15:21
auaryamane0074-Jan-10 15:21 
AnswerRe: Multi-color text in single cell of datagridview in C# [modified] Pin
petercrab5-Jan-10 0:11
petercrab5-Jan-10 0:11 
Hi,

You need to override the paint method for the cell object. Cells that contain just text are instances of DataGridViewTextBoxCell Class, so override that. Below is my attempt at a new class with over-ridden paint method. The draw method also only draws what will fit in cell so there will be no overlapping of data between cells. I have tried to make it as reusable as possible so you can define what colour you wish to change to and the number of consecutive numbers that should have colour changed. I also include code I used to test the class out and a couple of screenshots showing it working.

public class MultiColourDataGridViewTextBoxCell : DataGridViewTextBoxCell
    {
        public MultiColourDataGridViewTextBoxCell():base()
        {
        }

        /// <summary>
        /// overide paint method to colour text dependant on number of consecutive numbers
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="clipBounds"></param>
        /// <param name="cellBounds"></param>
        /// <param name="rowIndex"></param>
        /// <param name="cellState"></param>
        /// <param name="value"></param>
        /// <param name="formattedValue"></param>
        /// <param name="errorText"></param>
        /// <param name="cellStyle"></param>
        /// <param name="advancedBorderStyle"></param>
        /// <param name="paintParts"></param>
        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            int fontSize = 9;
            Brush textBrush = Brushes.Green;
            Font font = new Font(FontFamily.GenericSansSerif,fontSize,FontStyle.Regular);
            String stringPrint = (String)formattedValue;

            graphics.FillRectangles(Brushes.White,new Rectangle[]{cellBounds});
            StringPrintInfo[] stringPrintInfo = GetConsectiveNumbers(4, stringPrint, Brushes.Black, Brushes.Red);
            PrintConsectiveNumbers(stringPrintInfo,graphics, font,textBrush,cellBounds,StringFormat.GenericDefault);
            this.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);

        }

        /// <summary>
        /// from input string finds consective numbers marjs them in specified colour.
        /// </summary>
        /// <param name="numberConsecutive">number of consecutive numbers to look for</param>
        /// <param name="input">input string</param>
        /// <param name="bOriginal">default brush colour</param>
        /// <param name="bConsecutive">brush for consecutive numbers</param>
        /// <returns> an ordered array of text and colour pairs</returns>
        public StringPrintInfo[] GetConsectiveNumbers(int numberConsecutive, String input, Brush bOriginal, Brush bConsecutive)
        {
            List<StringPrintInfo> formatInformation = new List<StringPrintInfo>();
            int inputCount = 0; //how much of input string is used
            StringPrintInfo tmpStrPrntInfo;
            String regExp = "\\b[0-9]{4}\\b";

            Regex exp = new Regex(regExp);
            foreach (Match match in exp.Matches(input))
            {

                if (inputCount < match.Index)
                {
                    tmpStrPrntInfo = new StringPrintInfo();
                    String strTmp = input.Substring(0, match.Index);
                    tmpStrPrntInfo.str = strTmp;
                    tmpStrPrntInfo.brush = bOriginal;
                    inputCount = match.Index;
                    formatInformation.Add(tmpStrPrntInfo);
                }

                tmpStrPrntInfo = new StringPrintInfo();
                tmpStrPrntInfo.str = match.Value;
                tmpStrPrntInfo.brush = bConsecutive;
                inputCount = match.Index + match.Length;
                formatInformation.Add(tmpStrPrntInfo);
            }

            if (inputCount < input.Length)
            {
                tmpStrPrntInfo = new StringPrintInfo();
                String strTmp = input.Substring(inputCount);
                tmpStrPrntInfo.str = strTmp;
                tmpStrPrntInfo.brush = bOriginal;
                inputCount = input.Length;
                formatInformation.Add(tmpStrPrntInfo);
            }

            return formatInformation.ToArray();
        }

        /// <summary>
        /// Prints the cell with multicolour text. should be called as part of paint method
        /// </summary>
        /// <param name="toPrint"></param>
        /// <param name="graphics"></param>
        /// <param name="font"></param>
        /// <param name="textBrush"></param>
        /// <param name="cellBounds"></param>
        /// <param name="stringFormat"></param>
        public void PrintConsectiveNumbers(StringPrintInfo[] toPrint,Graphics graphics, Font font ,Brush textBrush,Rectangle cellBounds, StringFormat stringFormat)
        {
            float xOffset = 0;

            for(int i = 0;i<toPrint.Length;i++)
            {

                if (i > 0)
                {
                    SizeF stringSize = graphics.MeasureString(toPrint[i-1].str, font);
                    xOffset += stringSize.Width;
                }

                /*dont print of edges of cell*/
                if (cellBounds.X+ xOffset + graphics.MeasureString(toPrint[i].str, font).Width > cellBounds.X + cellBounds.Width)
                {
                    string cutDown = toPrint[i].str;

                    for (int j = cutDown.Length; j > 0; j--)
                    {
                        cutDown = cutDown.Substring(0, j);
                        if (cellBounds.X + xOffset + graphics.MeasureString(cutDown, font).Width <= cellBounds.X + cellBounds.Width)
                        {
                            graphics.DrawString(cutDown, font, toPrint[i].brush, cellBounds.X + xOffset, cellBounds.Y, stringFormat);
                            break;
                        }
                    }
                }
                else
                {
                    graphics.DrawString(toPrint[i].str, font, toPrint[i].brush, cellBounds.X + xOffset, cellBounds.Y, stringFormat);

                }
            }
            
        }

        /// <summary>
        /// represent string to print and colour to print it
        /// </summary>
        public struct StringPrintInfo
        {
            public String str;
            public Brush brush;
        }
    }



//DataSet
DataSet dataSet = new DataSet();
DataTable dStable = new DataTable();
DataColumn dSCol1 = new DataColumn("name", typeof(string));
DataColumn dSCol2 = new DataColumn("value", typeof(string));
DataRow rTemp;

//DataGridView
DataGridViewColumnCollection dGVColumnCollection;
DataGridViewColumn dGVColumn1 = new DataGridViewColumn();
DataGridViewColumn dGVColumn2 = new DataGridViewColumn();

public partial class Form1 : Form
{
    //DataSet
    DataSet dataSet = new DataSet();
    DataTable dStable = new DataTable();
    DataColumn dSCol1 = new DataColumn("name", typeof(string));
    DataColumn dSCol2 = new DataColumn("value", typeof(string));
    DataRow rTemp;

    //DataGridView
    DataGridViewColumnCollection dGVColumnCollection;
    DataGridViewColumn dGVColumn1 = new DataGridViewColumn();
    DataGridViewColumn dGVColumn2 = new DataGridViewColumn();



    public Form1()
    {
        InitializeComponent();

        //initialise dataset
        dStable.TableName = "sample";
        dStable.Columns.Add(dSCol1);
        dStable.Columns.Add(dSCol2);
        rTemp = dStable.NewRow();
        rTemp.ItemArray = new object[]{"bill", " 000 0000 0000000 0000000 0000000000"};
        dStable.Rows.Add(rTemp);
        rTemp = dStable.NewRow();
        rTemp.ItemArray = new object[]{"ted", " 111 1111 1111111 1111111 1111111111"};
        dStable.Rows.Add(rTemp);
        rTemp = dStable.NewRow();
        rTemp.ItemArray = new object[] { "john", " 222 2222 2222222 2222222 2222222222" };
        dStable.Rows.Add(rTemp);
        dataSet.Tables.Add(dStable);

        //add coloumns to datagridView
        dGVColumnCollection = dataGridView1.Columns;
        dGVColumnCollection.Add("col1", "Name");
        dGVColumnCollection.Add("col2", "Value");
        dGVColumn1 = dGVColumnCollection[0];
        dGVColumn2 = dGVColumnCollection[1];


        //pupulate DataGridView
        foreach(DataRow row in dataSet.Tables[0].Rows)
        {
            DataGridViewRow dGVRow = new DataGridViewRow();
            DataGridViewTextBoxCell textCell = new DataGridViewTextBoxCell();
            MultiColourDataGridViewTextBoxCell textCel2 = new MultiColourDataGridViewTextBoxCell();
            textCell.Value = row.ItemArray[0];
            textCel2.Value = row.ItemArray[1];
            dGVRow.Cells.Add(textCell);
            dGVRow.Cells.Add(textCel2);
            dataGridView1.Rows.Add(dGVRow);
        }

    }
}



http://img2.pict.com/b4/07/ad/2401377/0/gridcellcolour1.jpg
http://img2.pict.com/91/13/8d/2401376/0/gridcellcolour2.jpg

modified on Tuesday, January 5, 2010 9:17 PM

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.