# Dynamic Container Control

**URL:** <https://forum.xojo.com/t/dynamic-container-control/34642>\
**Category:** Getting Started\
**Created:** [November 25, 2016, 3:17pm UTC](https://forum.xojo.com/t/dynamic-container-control/34642 "2016-11-25T15:17:18Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![LAM\_YUK\_HONG](https://forum.xojo.com/user_avatar/forum.xojo.com/lam_yuk_hong/32/529_2.png) [@LAM\_YUK\_HONG](https://forum.xojo.com/u/LAM_YUK_HONG)\
**Post date:** [November 25, 2016, 3:17pm UTC](https://forum.xojo.com/t/dynamic-container-control/34642/1 "2016-11-25T15:17:18Z")

</div>

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  
next

[/code]

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [November 25, 2016, 6:47pm UTC](https://forum.xojo.com/t/dynamic-container-control/34642/2 "2016-11-25T18:47:05Z")

</div>

Close the previous ones before you create the new ones.

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

---

<div class="post-metadata">

**Author:** ![LAM\_YUK\_HONG](https://forum.xojo.com/user_avatar/forum.xojo.com/lam_yuk_hong/32/529_2.png) [@LAM\_YUK\_HONG](https://forum.xojo.com/u/LAM_YUK_HONG)\
**Post date:** [November 26, 2016, 2:00am UTC](https://forum.xojo.com/t/dynamic-container-control/34642/3 "2016-11-26T02:00:09Z")

</div>

Hi Tim,

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

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [November 26, 2016, 2:08am UTC](https://forum.xojo.com/t/dynamic-container-control/34642/4 "2016-11-26T02:08:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![LAM\_YUK\_HONG](https://forum.xojo.com/user_avatar/forum.xojo.com/lam_yuk_hong/32/529_2.png) [@LAM\_YUK\_HONG](https://forum.xojo.com/u/LAM_YUK_HONG)\
**Post date:** [November 27, 2016, 1:50am UTC](https://forum.xojo.com/t/dynamic-container-control/34642/5 "2016-11-27T01:50:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [November 27, 2016, 3:02am UTC](https://forum.xojo.com/t/dynamic-container-control/34642/6 "2016-11-27T03:02:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [November 27, 2016, 3:13am UTC](https://forum.xojo.com/t/dynamic-container-control/34642/7 "2016-11-27T03:13:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [November 27, 2016, 10:44am UTC](https://forum.xojo.com/t/dynamic-container-control/34642/8 "2016-11-27T10:44:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [November 28, 2016, 1:13am UTC](https://forum.xojo.com/t/dynamic-container-control/34642/9 "2016-11-28T01:13:23Z")

</div>

[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.

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [November 28, 2016, 1:48am UTC](https://forum.xojo.com/t/dynamic-container-control/34642/10 "2016-11-28T01:48:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![LAM\_YUK\_HONG](https://forum.xojo.com/user_avatar/forum.xojo.com/lam_yuk_hong/32/529_2.png) [@LAM\_YUK\_HONG](https://forum.xojo.com/u/LAM_YUK_HONG)\
**Post date:** [November 28, 2016, 12:36pm UTC](https://forum.xojo.com/t/dynamic-container-control/34642/11 "2016-11-28T12:36:23Z")

</div>

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.

[https://drive.google.com/open?id=0B2DNp\_dIdcJXSHVmMjRuaUpMeUE](https://drive.google.com/open?id=0B2DNp_dIdcJXSHVmMjRuaUpMeUE)

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [November 28, 2016, 7:27pm UTC](https://forum.xojo.com/t/dynamic-container-control/34642/12 "2016-11-28T19:27:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![LAM\_YUK\_HONG](https://forum.xojo.com/user_avatar/forum.xojo.com/lam_yuk_hong/32/529_2.png) [@LAM\_YUK\_HONG](https://forum.xojo.com/u/LAM_YUK_HONG)\
**Post date:** [November 28, 2016, 11:03pm UTC](https://forum.xojo.com/t/dynamic-container-control/34642/13 "2016-11-28T23:03:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [November 29, 2016, 12:14am UTC](https://forum.xojo.com/t/dynamic-container-control/34642/14 "2016-11-29T00:14:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![LAM\_YUK\_HONG](https://forum.xojo.com/user_avatar/forum.xojo.com/lam_yuk_hong/32/529_2.png) [@LAM\_YUK\_HONG](https://forum.xojo.com/u/LAM_YUK_HONG)\
**Post date:** [November 29, 2016, 1:23am UTC](https://forum.xojo.com/t/dynamic-container-control/34642/15 "2016-11-29T01:23:58Z")

</div>

Oh sorry, wrong link…

[https://drive.google.com/open?id=0B2DNp\_dIdcJXV0xGazBxTl9TMjA](https://drive.google.com/open?id=0B2DNp_dIdcJXV0xGazBxTl9TMjA)

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [November 29, 2016, 2:07am UTC](https://forum.xojo.com/t/dynamic-container-control/34642/16 "2016-11-29T02:07:23Z")

</div>

Your problem is here

```auto
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.

---

<div class="post-metadata">

**Author:** ![LAM\_YUK\_HONG](https://forum.xojo.com/user_avatar/forum.xojo.com/lam_yuk_hong/32/529_2.png) [@LAM\_YUK\_HONG](https://forum.xojo.com/u/LAM_YUK_HONG)\
**Post date:** [November 29, 2016, 2:22am UTC](https://forum.xojo.com/t/dynamic-container-control/34642/17 "2016-11-29T02:22:20Z")

</div>

Thanks Tim, it works now.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 6:33pm UTC](https://forum.xojo.com/t/dynamic-container-control/34642/18 "2020-10-29T18:33:45Z")

</div>


