Dynamic Container Control

Hi All,

I am writing my own simple Calendar. I have a master container with title and navigation buttons. Dynamically creates “day containers” by simple calculation. The calendar displays correctly for the default current month/year. I can click on each “day container” to show the date of that container (mouse down event). When I navigate to previous/next month, the calendar displays correctly except that when I click on the day container, it shows the date 2 times (current one and the last date at the same position). The number of times if shows follows number of times I navigate through the calendar. Any ideas?

Simplified code:

[code]dim c(43) as CalendarDayContainerControl
dim left, top, width, height as integer

//some calculation for first day and last day of month and position

for i = 1 to 43
c(i) = new CalendarDayContainterControl
c(i).myDate.Year = CurrentYear
c(i).myDate.Month = CurrentMonth
c(i).myDate.Day = CurrentDay
c(i).EmbedWithin(self, left, top, width, height)
CurrentDay = CurrentDay + 1 //something like that


Close the previous ones before you create the new ones.

for i = 1 to 43
     if c(i) <> Nil then c(i).Close
     c(i) = new CalendarDayContainerControl

Hi Tim,

That is what I was thinking and I did try but it doesn’t work.

Don’t do it that way… no need to create an object (container) for each day (or possible day)… just use a canvas
In the Paint routine, draw what you need to draw.
In the Mouse Events, determine what “day” you are on via the X/Y coordinates…
ONE control, less overhead, make it look anyway you need/want

Dave, do you mean to make the whole calendar as one single control? However, I need to make some functions to repeat many times by myself, e.g. I plan to put at least 2 functions in each day container, add and edit event.

For the same issue, what about if I plan to setup dynamic containers in a search result list? The same problem may still happen.

Yes I mean exactly that… I think you may be attempting to over-engineer what is a relativlty simple situation.

The canvas needs to only do two major things (keyword major)… it needs to DISPLAY the calendar information , and it needs to detect and react to mouse clicks and movements.

A calendar is either a grid (rows and columns) or its a list (yeah I know there are a few other display modes), the point is, that if you click ANYWHERE in the canvas I am talking about, it is a simple bit of math to know “I just clicked on Nov, 26th 2016”…

The canvas (subclass) only need contain your two events (ADD and EDIT)… since you calendar “data” should be in sometype of database (not stored inside each container)…

A properly designed event driven Object Oriented Program, should have a minimal number of object instances, especially when ONE custom object can contain all the logic to handle the entire thing.

Search this forum and you will find existing calendar and calendar-related controls (Mike Cotrone has a really good one)… and I’ll bet every single one of them is subclassed from the CANVAS.

The calendar has to be the single most reinvented control in RB/ Xojo history
It seems that virtually everyone has made at least one (if not several)
I know I have one posted on my site, mike cotrone has one and I’m sure there are many many others

Another approach I have used is a ListBox to display the calendar grid.

As Norman said, there are many different ways to build a calendar.

[quote=300087:@Tony Lam]Hi Tim,

That is what I was thinking and I did try but it doesn’t work.[/quote]
I am very surprised by that. It seems to work for me here.

Just tested it again with a setup identical to what you describe and code pretty much the same as what you posted. If I fail to close the previous container, I get 2 mousedowns (or as many as I have pressed the button). If I close the container, I only ever get one mousedown, regardless of how many iterations of closing and embedding I go through.

Hi Tim,

Here is my test calendar, the MouseDown event handler fires more than once even I close the containers before creating a new one.


I don’t see anywhere in that project where you are closing a container.

Yes I did, every time before I create a new instance:

  if c(counter) <> nil then c(counter).close
  c(counter) = new CalendarDayContainerControl

Wrong syntax?

Correct syntax, but I don’t see that in the project you linked to.

Oh sorry, wrong link…


Your problem is here

dim c(42) as CalendarDayContainerControl

This creates a local variable that disappears as soon as the method finishes. That means you lose track of the containers and will never be able to close them. Make the array more persistent, like a property of CalendarContainerControl, so you don’t lose your references to the containers.

Thanks Tim, it works now.