DrawInto should draw a control, right?

I’m trying to draw a TextField control inside a new Class that I created, that as a Super of type Canvas.
So, inside the Paint method, I placed the following test code:

#PRAGMA unused areas

g.DrawingColor = &cAA00000
g.FillRectangle(0,0,me.Width,me.Height)

var t_field as New TextField

t_field.Width = 80
t_field.Height = 16
t_field.Text = "12345"

t_field.DrawInto(p.Graphics,2,2)

It simply draws a red rectangle, and no TextField.
So, what is the way to “paint” a Control (TextField, or whatever), inside a Canvas Control?

For one thing you have p.Graphics in the routine, which should be g. That said that change doesn’t actually make it work. It’s likely due to the fact that the control has just been instantiated and isn’t on a window anywhere. For example if you place a TextField onto the window (over the right) and then change your code to:

TextField1.DrawInto(g,20,20)

It actually draws the control.

If, however, you trying to actually add a working text field to your layout, this isn’t the way to do it. You need to look at AddControl. For example, putting this in the Opening event will add a textfield:

Var t_field As New DesktopTextField
t_field.top = 2
t_field.Left = 2
t_field.Width = 80
t_field.Height = 22
t_field.Text = "12345"
t_field.BackgroundColor = &cffffff
t_field.HasBorder = True

Self.AddControl t_field

You cannot instantiate a TextField the way you do it.

You should have a textfield on your window dragged from the library, in the IDE.

The other way would be to put a TextField onto a DesktopContainer, which you can instantiate in code onto the window as such:
https://documentation.xojo.com/api/user_interface/desktop/desktopcontainer.html#desktopcontainer-embedwithin

Thank you both.
The p.Graphics is a leftover from an experiment and I did a copy/paste and didn’t edited the code to reflect the current state.
Anyway, I will look into a way to instantiate the TextField (and other Controls I need), the way you guys told me.
Oh, the AddControl is a method that belongs to what type of object?

AddControl and be used with Windows and Containers. It’s possible a Container Control would do everything you want. You can add one to your project and then add controls to it within the IDE. You can then embed the combination into other windows, either within the IDE or programmatically as Michel has said.

Interesting. So you’d make the DesktopContainer be exactly the same size as the Textfield that’s within it. Then you paint that into a window od container… Hmmm.

Drawinto puts an image of whatever is in the window/concatiner, into the graphics (e.g. your subclass of Canvas). It’s not actually adding a real control. You need AddControl or, I suggest, EmbedWithin, if you want to add an actual control/container into a window.

So, if I want to add a Control to an area that I want to be able to scroll up and down, left and right, what should I add it to?

Add it to the canvas. You’ll need to add a scrollbar too, which can in fact be anywhere, although it makes most sense for it to be adjacent to the canvas.

Read about the canvas’s .Scroll method in the docs.

Well, I get an error telling me…

Type “Canvas” has no member named “AddControl”

I was thinking he wanted more than just a simple text field on the “new control”. If he just wants to add a text field then just do that. No need for another layer. If it is a combination of controls as a “new control” which can be added etc then that would be when I suggested a Container Control.

If you just want to scroll a set of controls on a window then simply place a Canvas on the window and a scroll bar and look at the Cavas .scroll method as TimStreater has suggested.

No a Canvas cannot be used as a container in that way. You can drag and drop text fields onto a canvas in the IDE (make sure the canvas goes red before you drop, indicating that its parent is the canvas).

If you can provide a drawing or sketch of what you are trying to achieve it could help. There are many options that may do what you want depending on what it is.

Yes, that is what I want. But I want to be able to add more controls as needed. And they need to works (an is… if I add a TextField, one should be able to edit text in it). So, how can I create an area that can be populated with controls that are added in code, can be scrolled, and the controls are functional?

More of the same control type of control or additional controls of any type?

Different types of controls.

Ah, I was assuming you meant using the IDE. If the IDE, then just put the control on the canvas just like you’d put any control anywhere. If you’re adding them at runtime, then instantiate the control (which can be a container), and use EmbedWithin to add the newly instantiated control into the canvas. Thus:

Var  termPtr As MatchTerm

termPtr = new MatchTerm                        // Make new term
termPtr.EmbedWithin (MatchTermsCanvas)         // Put the term into the layout

Matchterm is a DesktopContainer with a number of controls in it.

Then I set the newly embedded container’s top and left to position it properly.

You could have a canvas on the window and embed a Container control on that canvas. You can add controls (any) to that container at runtime in any way you want. Make the canvas larger / smaller and pin the container to grow with it. scroll the canvas using a scrollbar?

Never though of embedding a Container control into the Canvas. Sounds the solution for me.
So, that is where I use the AddControl?

I’m suggesting you use EmbedWithin rather than AddControl.

As Tim said, EmbedWithin may be better as you don’t need as many layers. Container control is good if you want to reuse the composite control in many places.