Multiple Colored texts in RichTextBox using C#

This article is created for making multiple colored texts in a textbox. By using the SelectionText property in richtextbox we have created a sample application for multiple colored texts in Microsoft’s richtextbox control. Let us see how to make it in below code.

Font font = new Font("Tahoma", 8, FontStyle.Regular);
richTextBox1.SelectionFont = font;
richTextBox1.SelectionColor = Color.Red;
richTextBox1.SelectedText = Environment.NewLine + textBox1.Text;

By using the above code, we can make a multiple colored texts in our richtextbox. This is basically used for doing some chat application. We will have to show the other end of person’s text in different color in chat application. So that we can use this method. And we can create a simple method and calling in every time of showing the text in richtextbox with the parameters of like “textbox1.text, color,..etc”.

For more visit my article in codeproject here with more explanation.


How to sum previous rows value SqlServer?

I needed to write a query for sum the values with the previous row value in current row of a column. So that I have written a single query after a long googled. Here I am trying to give you what I have done.

I have a table with three fields Id, Name and Mark. I have values of all fields like as follows.

Id Name Mark
--- ----- -----

1 aaaa 10
2 bbbb 20
3 cccc 30

Now I wants to get the results set of that table like as

Id Name Mark
--- ----- -----

1 aaaa 10
2 bbbb 30

3 cccc 60

So I need a single select query to do this performance. For that I have written a query using cross join.

Code Snippets for Table and Query

CREATE TABLE [dbo].[Marks]( [Id] [bigint] NOT NULL, [Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [Mark] [bigint] NOT NULL ) ON [PRIMARY]

select * from Marks

select a.Id, a.Name, sum(b.Mark) as Mark
from Marks a cross join Marks b
where b.Id <= a.Id group by a.Id, a.Name

Hence we have done the fetching the result set for sum of the previous rows in sql server.

For more ...

visit here


Multiple rows in a single row with separator in SqlServer

Here is a simple way to make a single row value from multiple rows with a separator in SqlServer.

I have a table like the following…

Table Name: People

select * from people

Now I need the ‘Name’ column values where matching the value of ‘aaaa’ in to a single row. For this I have tried to make a query.

Just use the following query to get the solution.

declare @res varchar(1000)
select @res = coalesce(@res + ',', '') +Name
from people
WHERE Name = 'aaaa'

select @res

See the result…


Simple recursive function for string folding in C#

Here is the sample function of string folding in c#. I just tried bind the body content of a mail in to a label box. For that I need to folding the string content. I have created a method to do this string folding as follows,

if (lblBody.Text.Length > 150)
lblBody.Text = lblbodystring(lblBody.Text,150);

//Simple recursive function for string folding.
private string lblbodystring(string bodyString, int Length)
string retVal = "";
if (bodyString.Length > Length)
retVal = bodyString.Substring(0, Length);
retVal = retVal + Environment.NewLine;
retVal = retVal + lblbodystring(bodyString.Substring(Length));
retVal = retVal + Environment.NewLine;
retVal = retVal + bodyString;
return retVal;

The thing is that we can use textbox with multiline attribute for this kind of process :). But I have used in label. So...


Query for importing data’s from XL to SqlServer DB

Here is a simple query for importing data's from Microsoft Excel sheet to Sql Server database.

Just copy the following code and run in your sql server query window. Before that make XL sheet with the appropriate fields and values and place it in c drive or wherever you wants.

INSERT INTO [dbo].[TableName] ([FieldName1] ,[FieldName2] ,[FieldName3] ,[FieldName4] FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0', 'Data Source=C:\db.xls;Extended Properties=Excel 8.0')...[SheetName$]

that's it...


Silverlight - Introduction

Microsoft Silverlight is a programmable web browser plug-in that enables features such as animation, vector graphics and audio-video playback that characterizes rich Internet applications.

Silverlight provides a retained mode graphics system similar to Windows Presentation Foundation, and integrates multimedia, graphics, animations and interactivity into a single runtime environment. In Silverlight applications, user interfaces are declared in XAML and programmed using a subset of the .NET Framework. XAML can be used for marking up the vector graphics and animations. Textual content created with Silverlight is searchable and indexable by search engines as it is not compiled, but represented as text (XAML).


1. Silverlight 1.0
2. Silverlight 2
3. Silverlight 3

Tools for Silverlight application

To developing Silverlight application, Microsoft has been released some set of tools. They are,

• Visual Studio 2008 with Service Pack 1
• Silverlight 2 Beta 2 SDK
• Expression Blend 2 July 2008 Preview

All of us known about Visual Studio. Expression Blend is Microsoft's user interface design tool for creating graphical interfaces for web and desktop applications. Blend is writing using the .NET Framework 3.0 and Windows Presentation Foundation (WPF). Microsoft Expression Blend does the same of Adobe Photoshop to edit your images as well as add effects to existing images.

How to create Silverlight applications?

There is a question in our mind raising that “How to create Silverlight applications?” Here is the answer for your question,

1. Use Visual Studio 2008 SP1 to create a new Silverlight project based on the Silverlight 2 Beta 2 Template. Visual Studio will do the necessary steps to add a single HTML page or a single website to host your Silverlight application. This is because Silverlight is a plug-in that renders in the browser.

2. Add your necessary XAML files needed for the user interface. This is similar to the forms that you would add to a Windows Form application.

3. Edit the XAML page using Expression Blend 2 July 2008 preview. Note that Because each page in Silverlight 2 Beta 2 is treated as a user control, only Expression Blend 2 July 2008 preview will be capable of opening these particular XAML files. Any older version of Expression Blend will throw an error message saying that it is unable to open that file type.

4. Switch to Visual Studio 2008 SP1 to write code behind files for the controls and pages that you created in Step 2.

5. Build and compile the application.

6. Host the same in IIS or any web server of your choice. For this demonstration, you will rely on Windows Vista for development and IIS for hosting the application.


Simple Collapse Panel - Windows application in C#

Hi all, There is no option to collapsing functionality in panel control.

I have created a panel control with up/down arrow images. We can collapse this control by clicking those arrows. There are some property values allowing us the interval of collapsing time. The ScrollInterval value is used to set the interval time for collapsing animation. And the HeaderHeight property value is set to be the height of header part in this control. I just used some calculation to set the height value for this control in a certain loop. This much of calculation makes some animation to collapsing the control.

You can see the magic of this control by downloading the demo project which is available to download here.

The simple calculation of control’s height is possible to collapse the control. Just see the following code,

if (this.Height > this.lblTop.Height)
while (this.Height > this.lblTop.Height)
this.Height -= ScrollIntervalValue;
this.lblTop.ImageIndex = 1;
this.Height = this.lblTop.Height;
else if (this.Height == this.lblTop.Height)
int x = this.FixedHeight;
while (this.Height <= (x))
this.Height += ScrollIntervalValue;
this.lblTop.ImageIndex = 0;
this.Height = x;

Just refer the control in your application, and add the control in your form. Then set the property values of this control ScrollInterval and HeaderHeight as you want. If you don’t set those property values it might be taken by default values. Now build your project and run. By clicking the arrow images in collapse panel, you can see the magic.

For More…see my article in code project.


How do we change the header BackColor and selected color in ListView using c#?

How do we change the header BackColor and selected color in ListView using c#?

Yes, we can make it possible to change the header backcolor and style for ListView control. And also we can change the some styles in ListView control in the code behind. Here is the way to explain how it is possible.

There is property named “OwnerDraw” is used to do the style changing process by our code behind. Just we need to set that property as “true”.

I have a ListView control with the name of “lstView” in my sample form, and the code has been written as follows,

this.lstView.BackColor = System.Drawing.SystemColors.ControlText;
this.lstView.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.lstView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.lstView.ForeColor = System.Drawing.SystemColors.ControlLightLight;
this.lstView.FullRowSelect = true;
this.lstView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
this.lstView.Location = new System.Drawing.Point(2, 1);
this.lstView.MultiSelect = false;
this.lstView.Name = "lstView";
this.lstView.OwnerDraw = true;
this.lstView.View = System.Windows.Forms.View.Details;

private void lstView_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
System.Drawing.Drawing2D.LinearGradientBrush GradientBrush = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.Blue, Color.LightBlue, 270);

e.Graphics.FillRectangle(GradientBrush, e.Bounds);

e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.White), 2), e.Bounds.X + 1, e.Bounds.Y + 1, e.Bounds.Width - 2, e.Bounds.Height - 2);

e.Graphics.DrawString(e.Header.Text, new Font("Arial", 12), new SolidBrush(Color.Yellow), e.Bounds);

private void lstView_DrawItem(object sender, DrawListViewItemEventArgs e)
if (e.Item.Selected)
e.Graphics.FillRectangle(new SolidBrush(Color.DarkBlue), e.Bounds);
e.Graphics.DrawString(e.Item.Text, new Font("Arial", 10), new SolidBrush(Color.White), e.Bounds);

private void lstView_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
e.Graphics.DrawString(e.Item.Text, new Font("Arial", 10), new SolidBrush(Color.White), e.Bounds);

That's it...


How to change the header backcolor in datagridview C#

Here I am trying to get the new design for my DataGridView header back color. So that, I have been tried a lot in Google and got the solution which is explained below.

Set the following properties for your DataGridView control by using the properties window.

EnableHeadersVisualStyles = False

RowHeadersBorderStyle = Raised

Then copy the following code in your Form_Load event.

DataGridViewCellStyle style = this.dataGridView1.ColumnHeadersDefaultCellStyle;
style.BackColor = Color.WhiteSmoke;
style.ForeColor = Color.Gray;
style.Font = new System.Drawing.Font("Arial", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

Now run your application, the header style would be changed and showing a new style that we wants.