MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to Edit the ListView Subitems in C#

In this article we will discuss the process to alter the color and font of each of the sub item present in a listview.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

Introduction

The article aims to explain the readers the process to edit the ListView Subitems such as playing with the color. Also we will look at its font attributes of each and every subitem of a ListView. We will look at the controls that will be used in order to accomplish the task for this tutorial and then the tutorial explains on the process to position and the functions of the controls we are using.

Introduction to Controls:

We will make use of the following controls:

  • ListView
  • six RadioButtons from row 1 to row 6
  • three ComboBoxes from column 2 to column 4
  • a GroupBox, a Button, a ColorDialog and a FontDialog.

The ListView comprise of six rows and four columns where in the first column known by the name of "Pid" is the item that specifies the ProductId. We have a second column as "product" followed by third as "stock" and the fourth column "price" which are subitems of the related item.

The colors of these subitems will be changed by colorbrowser, and font by making use of fontbrowser. Post this, we would write modified values into the ListView.

Introduction to Controls Positioning

Displays the controls positioning used

Figure 1: Displays the controls positioning used.

It is required to fill the text of three comboboxes first and then one must access radiobutton1 for row 1, one need to select the color and font via browsers. Post this, one need to again fill three comboboxes for row 2 and click radiobutton 2 for row 2. This needs to be repeated until row 6.

The button displayed by the name of "try again" is used so as to repeat this application from the beginning.

Displays stage numbers of cycles 1 as red numbers for row 1, Cycle 2 blue numbers are for row 2, and so on for other rows.

Figure 2: Displays stage numbers of cycles 1 as red numbers for row 1, Cycle 2 blue numbers are for row 2, and so on for other rows.

Displays adding a color to (row 1, column 2).

Figure 3: Displays adding a color to (row 1, column 2).

Displays adding a font for item 1 (row 1, column 2).

Figure 4: Displays adding a font for item 1 (row 1, column 2).

Displaying row 1 and row 2 as completed

Figure 5 : Displaying row 1 and row 2 as completed

Output for the six rows above

Figure 6: Output for the six rows above

Listing 1: Modification to few attributes of the ListView

listView1.GridLines = true;
listView1.View = View.Details;
listView1.FullRowSelect = true;
listView1.BackColor = Color.Silver;

Listing 2: Represents Columns and ComboBox items incorporated to the ListView.

listView1.Columns.Add("Pid", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Product", 150, HorizontalAlignment.Left);
listView1.Columns.Add("Stock", 150, HorizontalAlignment.Left);
listView1.Columns.Add("Price", 100, HorizontalAlignment.Left);

cmbPrdct.Items.Add("Led Lcd TV");
cmbPrdct.Items.Add("Desktop");
cmbPrdct.Items.Add("HDD 500 GB");
cmbPrdct.Items.Add("3D Led Monitor");
cmbPrdct.Items.Add("GSM Phone");
cmbPrdct.Items.Add("Printer");
cmbStck.Items.Add(" 55");
cmbStck.Items.Add("112");
cmbStck.Items.Add(" 88");
cmbStck.Items.Add(" 43");
cmbStck.Items.Add("210");
cmbStck.Items.Add(" 86");
cmbPrc.Items.Add("1660 $");
cmbPrc.Items.Add("1300 $");
cmbPrc.Items.Add(" 260 $");
cmbPrc.Items.Add(" 690 $");
cmbPrc.Items.Add(" 350 $");
cmbPrc.Items.Add(" 240 $");

We run the color and font codes in the Radiobutton1 Click Event Handler for item 1 (row 1).

ListViewItem change1 = listView1.Items.Add("1");

There is a need to create a Listitem object to the Listview first. "1" is the item number as well as the row number.

change1.UseItemStyleForSubItems = false;

Post this the UseItemStyleForSubItems property ought to be false.

ListViewItem.ListViewSubItem product = change1.SubItems.Add(cmbPrdct.Text);

The subitem known by the name of "product" which is actually a column 2 of the ListView needs to be incorporated. cmbPrdct .Text is a string that is being entered for Product.

The below code represents the Colour and font attributes that are specified for subitem known by the name of "product":

Listing 3: Code represents the attributes for “product”

 ColorDialog colord = new ColorDialog();
            if (colord.ShowDialog() == DialogResult.OK)
            {
                product.ForeColor = colord.Color;
            }
   FontDialog fontp = new FontDialog();
            if (fontp.ShowDialog() == DialogResult.OK)
            {
                product.Font = fontp.Font;
            }

The Subitem by the name of "stock" which is actually a column 3 of the ListView needs to be incorporated. "cmbStck.Text" is basically a string that is being enter for stock.

ListViewItem.ListViewSubItem stock = change1.SubItems.Add(cmbStck.Text);

The below code lists the Colour and font attributes that are specified for subitem known by the name of "stock":

Listing 4: Code represents the attributes for “stock”

  ColorDialog colorStock = new ColorDialog();
            if (colorStock.ShowDialog() == DialogResult.OK)
            {
                stock.ForeColor = colorStock.Color;
            }
  FontDialog fontst = new FontDialog();
            if (fontst.ShowDialog() == DialogResult.OK)
            {
                stock.Font = fontst.Font;
            }

The Subitem known by the name of "price" which is actually column 4 of the ListView needs to be incorporated. "cmbPrc.Text" is a string that has been entered for price.

ListViewItem.ListViewSubItem Price = change1.SubItems.Add(cmbPrc.Text);

The below code lists the colour and font attributes that are specified for subitem which is know by the name of "price".

Listing 5: Code represents the attributes for “price”

 ColorDialog colorPrice = new ColorDialog();
            if (colorPrice.ShowDialog() == DialogResult.OK)
            {
                Price.ForeColor = colorPrice.Color;
            }
   FontDialog fontprc = new FontDialog();
            if (fontprc.ShowDialog() == DialogResult.OK)
            {
                Price.Font = fontprc.Font;
            }

Conclusion

Coming to the end of the article, we discussed the process to alter the colour and font of each of the sub item present in a listview. We took the help of C#. Other than this, we witnessed the way to position controls on the form, process to set some of the properties of the ListView at first.

After that, we looked at the cycles of colour and font changes and then during the final phase, it was the turn of looking out for changes of colour and font on the ListView. One can make use of some of the code snippets in the applications you are using. This is just to emphasize the priority of sub items in rows or columns by different color and font so as to generate interfaces that are very user-friendly.



Software Developer from India. I hold Master in Computer Applications Degree and is well versed with programming languages such as Java, .Net, C and C++ and possess good working knowledge on Mobile Platforms as well.

What did you think of this post?
Services
Know how to keep MrBool Online
SUPPORT US
SUPPORT US
With your help, we can keep providing free content and helping you to be a better professional
support us
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login