Mouse Up Event when using dynamic controls

I am using the following to create controls dynamically in my web app.

Dim c As New SimpleContainer c.EmbedWithin(Self, Me.Left, mOffset, c.Width, c.Height)

I cant seem to work out how you add events to the instance of the new container, I am guessing it is something to do with AddHandler MouseUp but not exactly sure and then in the handler how do I know which version of the instance has been clicked on?

You have to have the events added to simplecontainer before you embed more instances of it.

I mean in the navigator, so you probably wouldn’t need addhandler for what you’re working on.

You can use AddHandler. For example, you could have:

  dim c as new simpleContainer
  c.EmbedWithin me,0,0  
  c.Title = "first" //This is one way to know which one you clicked on
  AddHandler c.mouseup, addressof mymethod

You would define myMethod as:

Function myMethod(s as simplecontainer, x as integer, y as Integer)
  msgbox s.title
End Function

Then if you clicked on your container you would get a message box that said “first”. Make the title unique for each embedded container and you can switch on that in your myMethod function. Note that for mouseUp, you will need to return true for mouseDown in your container control.

Note that if you want to add an event that returns a value (like MouseDown, which returns a boolean), you’d need to define your method as returning that value.

Thanks guys that now makes sense. Do you know if each instance is different in any way. What I mean is is it possible to loop through each of the instance to make changes to check the value of something in that instance?

Do you mean loop through the simpleContainers you create to check them? I believe you can keep track of them in an array and use the array to do things like that.

If you did that, you could then store the array index in the title to keep track of which instance’s event is being handled (there’s probably a better way, though). If you start deleting instances, you’ll have to track that though. Perhaps store them in a dictionary instead? I’m not sure if that would work, but it’s worth trying.

Or you can loop through all the controls in the window using ISA. You can search the language reference I think there’s an example. You can see if your control isa simplecontainer if it is check the value and respond accordingly.

I like the idea of using an array but am struggling with getting the code right. Below is a simple example of what I am doing but it just doesnt seem to store the container in the array for later use.

[code]
Dim instanceArray(100) As cPrescriptionPad
Dim mOffset as Integer = 60
Dim myControlWidth as Integer = 500

for lp as integer = 0 to 100
Dim c As New cPrescriptionPad

c.EmbedWithin(Self, 15, mOffset, myControlWidth, c.Height)
c.ScrollbarsVisible = 2
c.lblProductName.Text = "Product Number " + cstr(lp)

instanceArray.Append (c)

next[/code]

I think Instancearray() should be a window variable. Then you can access it later and in other places in your window.

Yes, instanceArray() should be a window property, and don’t give it a dimension, just define it as instanceArray()…Append will automatically increase the ubound for you.

Also, you could do this:

for lp as integer = 0 to 100
    Dim c As New cPrescriptionPad

    c.EmbedWithin(Self, 15, mOffset, myControlWidth, c.Height)
    c.ScrollbarsVisible = 2
    c.lblProductName.Text = "Product Number " + cstr(lp)

    instanceArray.Append (c)
    c.title = str(instanceArray.ubound) '<-- NEW CODE HERE

  next

Then in your event handlers you could look at “s.title” (from my example, s is the container control) and then you know the index of your instanceArray variable to look at - instanceArray(val(s.title)).

Argh, can’t edit my post. I meant to call more attention to the line marked “NEW CODE HERE”.

Thanks guys, moving stuff to the window and all is now working really well. These dynamic controls are really cool and in effect allow you to create a listbox without a listbox which is especially nice for mobile type apps as each of the contains that you dynamically create can contain whatever you want, very cool.