Using flexgrid instead of listbox to allow text formatting

The standard list box does not allow text formatting of each line. This is a problem if you want particular lines to stand out. This simple example provides a way to use an MSFlexGrid control instead of a listbox, giving freedom to control formatting of each line in the box. To make it simpler to use, the code to handle the grid is encapsulated within a class implementing methods similar to a list box - for example the .additem method to add a line to the list.

FlexList class

This example project shows how you can use MSFlexGrid instead of a list box so that you can format the text of each line independently, something the list box does not allow. The class FlexList, contained in the VB Class Module FlexList.cls presents some basic properties and methods of the list box, through which it makes a MSFlexGrid control behave like a list box. To use the FlexList you must first link it to an MSFlexGrid control by setting the FlexGrid property 'Code within a form which has an MSFlexGrid control named MSFlexGrid1 on it

dim myFlexList as new FlexList
:
myFlexList.FlexGrid = me.MSFlexGrid1

When you link the FlexList to the grid it automatically clears the grid and configures it to look like a list box.

Now you can use the list box-like method .addItem of the FlexList object to add lines to the list. This method has an additional optional string parameter to specify formatting - in this implementation any combination of the letters B I R or Y for Bold text, Italic text, Red text or Yellow background:

myFlexList.addItem "This line is plain text"
myFlexList.addItem "This line has red text", "R"
myFlexList.addItem "This line has bold italic text", "BI"
myFlexList.addItem "This line has bold red text on a yellow background", "BRY"

In real-time applications I have found it very useful to have an initial column that automatically contains the time that a message is added to the list, so the FlexList has a .showTimeStamp boolean property that shows or hides this first column.

I have also included an autoscroll property, that makes the last-added line visible if the previously-added line is visible. I have implemented it this way because, if you are manually scrolling through a list, it is a real pain if the list suddenly jumps to the end when an item is added.

The example code is quite basic but effective. More sophisticated uses of the MSFlexGrid could include changing font face and font size, adding glyphs or multiple columns. I have not implemented the .Sorted property of the list box, which would require rather more in the way of management of the MSFlexGrid, but it could be done if there is a demand for it.

Of course, this whole thing could be wrapped up and delivered as a control, but then that would be less easily tailored to your specific needs. ;-)

Note on detecting "clicks"

As the _Click event of the MSFlexGrid is not directly equivalent to the _Click event of the list box, you should use the event _EnterCell of the MSFlexGrid instead. The only oddity is that this event is raised whenever the current row or column number of the grid is changed, which happens when you use the .AddItem method. Therefore, I have included a property of FlexList, .Busy, that tells you if the event was raised internally rather than by the user. If you want to avoid acting on internally-generated events put this code in at the front of your _enterCell code: Private Sub MSFlexGrid1_EnterCell()

   If myFlexList.busy Then Exit Sub

   :
   : Normal action when a row is selected
   :

You might also like...

Comments

 John

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Nine people can't make a baby in a month.” - Fred Brooks