Click here to Skip to main content
13,549,238 members
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 21 Mar 2006

Write your own UserControl with C++/CLI: an RSS reader

, 21 Mar 2006
Rate this:
Please Sign up or sign in to vote.
Thsi article explains how to create an RSS reader user control.


This article is about the construction of an RSS reader UserControl, and is my first contribution to CodeProject. I will show you through the development of this reader, explaining how to create a User Control with the C++/CLI using the .NET 2.0 framework.

Project Creation

First, you have to create a new CLR / Windows Forms Control Library project. I'll name it rssFeeder. Visual Studio 2005 will generates a class, that derives from System::Windows::Forms::UserControl.

public ref class rssFeederControl : 
       public System::Windows::Forms::UserControl

The form rssFeederControl.h will represent our control. It's empty for now. Before filling it, we'll first talk about RSS quickly, and then about the properties of a UserControl.

Some words about RSS

RSS is used for syndicating a structured page, like a blog. We use an XML that describes the page. A sample page will look like this:

<rss version="2.0" xmlns:dc="" [...]>
        <title>Title of the blog</title> 
          <description>This is the description of the blog.</description> 
            <title>The last news</title> 
              <description>Description of the news</description> 
            <title>The first news</title> 
              <description>Description of the news</description> 

We can have an XML feed, for the comments too, the format is quite the same.

I will create a class (in fact, two classes) to load the content of the XML.

ref class CRssObject
    String ^ title;
    String ^ link;
    String ^ description;
    String ^ language;
    System::Collections::ArrayList ^ listItem;
    bool LoadFromUrl(String ^, int, String ^);
    bool LoadFromFile(String ^, int, String ^);
    bool isRssVersion2(Xml::XmlNode ^);
    String ^ GetText(Xml::XmlNode ^, String ^);
    bool LoadObject(Xml::XmlDocument ^, int, Xml::XmlDocument ^);

ref class CItemRss
    String ^ author;
    String ^ title;
    String ^ link;
    int nbComments;

I will construct an RssObject from an XML URL (or file). I will load only elements that I want to use, for example the title and the link. This object can be loaded only if the RSS version is 2.0.

You can look in the sources files to know how I'm loading the object from XML; as an example of code, I'm using the HttpWebRequest object to read XML from a URL.

HttpWebRequest ^ HttpWReq = 
HttpWReq->CachePolicy = 
   gcnew Cache::HttpRequestCachePolicy(Cache::HttpRequestCacheLevel::Reload);
HttpWResp = dynamic_cast<HttpWebResponse^>(HttpWReq->GetResponse());
sr = gcnew StreamReader(HttpWResp->GetResponseStream());
Xml::XmlDocument ^ xml = gcnew Xml::XmlDocument();

In the CPP file, you will see my own method that finds out how many comments there are on the news. As I didn't see any specifications for this, I looked for the URL of the news in the URL of the comments. If I find it, I get a comment.


Existing common controls (button, checkbox, etc.) already have some properties, like visible, size, location, .... This control has those default properties too. We are now going to add some extra properties to our control. We will add as properties, the URL of the XML file, the number of news to display, and the delay for refreshing the control. We will add the URL of the comments as an optional property.

To create a property, add this code:

[Description("Type here the Url or the xml")]
property String ^ url
    String ^ get() { return url_; }
    void set(String ^ value) 
        url_ = value; 

We use the Category attribute to name the field where the property will be put (if this attribute is not specified, it would be put in the Misc. category). We use the Description attribute to display a description when clicking on the field. Now, by adding our control to the toolbox and by drawing the user control on a form, we will see this in the property windows:

A property can only be a string! Here, the types of my properties are simple (string, int etc.). For complex types, use a TypeConverter.

Creation of the control

As we will not know before the configuration, how many news to display, we will construct the form dynamically. We will use a control box and a cli:array of LinkLabels. See this in the source code.

Add a personal image to the control

By default, the image for the user control is a wheel. We may want to add our own image. It's very simple in C#, but with C++/CLI, you have to follow these instructions:

  • add [ToolboxBitmap(rssFeeder::rssFeederControl::typeid)] before your class.
  • Create a 16x16 bitmap.
  • Name it as follows: Namespace.ClasseName.bmp.
  • Add it as a resource: Right click on project --> Add --> Resource --> Import (bitmap).
  • Link it with the DLL. Right click on the project --> Properties --> Linker --> Input --> Embed Managed Resource File, and add the name of the bitmap.

Events on the control

We may want to do a particular action when we click on a link (for example, when browsing the URL). The user then should intercept the Click event on a LinkLabel item. To do this, we have to create a delegate:

delegate void ItemClickHandler(Object ^ sender, 
           LinkLabelLinkClickedEventArgs ^ e);

Then we have to add this event to the configuration category:

[Category("Configuration"), Browsable(true), 
   Description("Event raised when clicking on an item")] 
   event ItemClickHandler ^ itemClick;

And raise the event when clicking on a LinkLabel. For this, during the user control creation, we add a Click event handler on each LinkLabel, as follows:

linkLabel->LinkClicked += gcnew 
  this, &rssFeeder::rssFeederControl::linkLabel_LinkClicked);

Then, we just have to do this to raise the event:

void linkLabel_LinkClicked(Object ^ sender, 
   System::Windows::Forms::LinkLabelLinkClickedEventArgs ^ e)
    if (&rssFeederControl::itemClick != nullptr)
        itemClick(this, e);

Finalizing the control

This is the main part. Don't hesitate to finalize the control, for example, I've overridden the Paint method to measure the string, to avoid the LinkLabel being larger than the control. I've added the output of the number of comments (if I can load it), and a tooltip on the LinkLabel with the author.

You can use the attribute DefaultValue("Default value") for setting a default value. I've done this in the constructor.


With this article, you should now be able to create a user control. In the Zip files, you will find the complete source code and the binaries of the user control and the project demo. Don't hesitate to send me feedback about this control or this article.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

France France
I'm a french developper, specially interested in windows programming with my favourite language, C++.

Beginning Windows programs with Win32, API & MFC, I finally move on C++/CLI. Now, I'm a Visual C++ MVP since 2007

My web site (in french contains article and tutorial about Win32, MFC & C++/CLI.

I've also written a C++/CLI FAQ (to be continued ...)

You may also be interested in...


Comments and Discussions

QuestionNice Article. Quick question... Pin
RogueClay5-Apr-07 6:59
memberRogueClay5-Apr-07 6:59 
GeneralThe control is compiled successfully, but there are many errors when use it. [modified] Pin
renyh10-Jul-06 2:48
memberrenyh10-Jul-06 2:48 
GeneralRe: The control is compiled successfully, but there are many errors when use it. Pin
nico.pyright10-Jul-06 3:04
membernico.pyright10-Jul-06 3:04 
GeneralNice! Pin
Ravi Bhavnani23-Mar-06 4:11
memberRavi Bhavnani23-Mar-06 4:11 
GeneralRe: Nice! Pin
nico.pyright23-Mar-06 4:25
membernico.pyright23-Mar-06 4:25 
GeneralSlightly different approach... Pin
The_Mega_ZZTer21-Mar-06 19:01
memberThe_Mega_ZZTer21-Mar-06 19:01 
GeneralRe: Slightly different approach... Pin
nico.pyright21-Mar-06 22:11
membernico.pyright21-Mar-06 22:11 
General5++ Pin
George L. Jackson21-Mar-06 14:44
memberGeorge L. Jackson21-Mar-06 14:44 
GeneralRe: 5++ Pin
nico.pyright21-Mar-06 22:05
membernico.pyright21-Mar-06 22:05 
General5! Pin
Nishant Sivakumar21-Mar-06 10:00
staffNishant Sivakumar21-Mar-06 10:00 
GeneralRe: 5! Pin
nico.pyright21-Mar-06 22:03
membernico.pyright21-Mar-06 22:03 
GeneralRe: 5! Pin
Nishant Sivakumar22-Mar-06 3:49
staffNishant Sivakumar22-Mar-06 3:49 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180515.1 | Last Updated 21 Mar 2006
Article Copyright 2006 by nico.pyright
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid