[Gambas-user] Question about object creation ?
Doriano Blengino
doriano.blengino at ...1909...
Sun May 24 09:52:36 CEST 2009
Olivier Cruilles ha scritto:
> Hi,
>
>
> I would like to create a new application to learn how to create
> dynamically object on a Form.
>
> All that I created since I know Gambas is by drawing object on the IDE
> of Gambas.
>
> So, my idea is to create a form and inside the possibility to add new
> object as a Panel and inside ListView, Buttons and some other.
> I would like to move this Panel by mouse, resize it.
>
> So I think I can find a link to explain me how to code this new object
> and how to create it dynamically by a class or a module.
>
It is easy. The following sub will create two textboxes each time:
PUBLIC SUB Button2_Click()
DIM tb AS TextBox
tb = NEW TextBox(FMain)
tb.x = 10
tb.y = 20
tb = NEW TextBox(FMain)
tb.x = 10
tb.y = 60
END
The first line, "tb = NEW textBox...." creates a new object into FMain,
with default values. Then you change properties as you want. Changing X
or Y moves the item; changing W, its width, Text, its containt, and so
on. You can change properties of every object, even those you created
with the IDE GUI editor.
Note then, that the same variable is used again to create another
textbox. The result will be 2 more textboxes in FMain. You can also use
a for-next cycle, or other loops, to create many widgets: all the widget
will be owned by FMain. But, after the second creation, the variable
"tb" will not be bound anymore to the first textbox, only to the second.
This means that for dynamically created object, if you want to operate
on them later, you must retain the variable used to construct them,
perhaps in an array. There two other methods to do this, if you don't
want to use an array. First, you can bound events to your items, using
something like (tb = NEW textBox(FMain) as "DynText"), and then operate
on the object from an event handler through the LAST variable. Or, you
can use the Children properties of the container where you put the objects.
> But my question is, if I want to draw between each my new object,
> lines to links all of then, what kind of solution I can use
>
> - use a drawarea object, put it on the backgroud of all objects and
> draw lines to create illusion of links ?
>
This is an option, especially suited if there are many lines. But to
keep track of those lines you will need some data structure (array,
perhaps).
If the user can click on lines to operate on them, you must search your
structures to identify which lines is under editing.
> or
>
> - create one drawarea for each link and put it between 2 of 'my new
> object' ?
>
This is an options also, but a drawingarea is a rectangular region. You
could end up with having two or more lines in the same drawingarea, and
fall again in the problem stated before (the need to search in your
structures). It depends on what you want to do. May be you want to do
zoom and pan, like a CAD. In this case, perhaps a single drawing area is
better. May be your needs are simpler, just a single line between two
never-overlapping objects, in this case many drawingareas could be
easier. I don't know for sure, but it seems to me that a single
drawingarea is better.
Regards,
--
Doriano Blengino
"Listen twice before you speak.
This is why we have two ears, but only one mouth."
More information about the User
mailing list