I am trying to add instances of a custom class (that is based on a RoundRectangle) to the screen. But I can’t get it to work… If try simply to make a new instance in the paint or open handler of a canvas, that does not display the instance:
var pr as new ProtoRoundRectangle
In the canvas paint: g.DrawObject(pr, 0, 0) does only allow graphics, which the rectangle is not.
g.DrawObject (pr, 10, 10) does only allow object2D which the rectangle is also not.
How can I initiate these instances, so that they appear on the screen please?
You could add a ‘drawinto’ method to your class, taking a graphics parameter
The method holds all the drawing instructions
Add instances of your class to an array or dictionary when initiated.
In the canvas Paint event, loop through the array and call drawyourself on each instance.
dim pr as ProtoRoundRectangle
for x as integer = 0 to arrayofprotorects.ubound
pr = arrayofprotorects(x)
If it is a simple roundrectangle, then have Group2D property on the window grpThings.
Create objects which are round rects, and append them to the group2d object
Then in the canvas paint event, just g.drawobject grpThings,0,0
Are RoundRectangle 2D objects? I can get drawobject to work with RoundRectShape, but not with RoundRectangle.
Where does this draw into? and what? Wouldn’t that draw into my ProtoRoundRectangle instead of into canvas? Am I misinterpreting something? Applying this code does not do anything. I created a
‘drawinto’ method to my class, added a property
arrayofprotorects() type ProtoRoundRectangle to the window, and placed your code to canvas paint. What am I missing?
Use that then. a RoundRectangle is probably just a thin wrapper over just drawing a rounded rectangle in the paint event anyway.
Where does this draw into? and what?
If you have code in your class that takes G and draws your object onto it, then what this would do is to call that method and pass along the G from the canvas
So you would be drawing onto the canvas’ graphics
Wouldn’t that draw into my ProtoRoundRectangle instead of into canvas?
Am I misinterpreting something?
I created a
‘drawinto’ method to my class,
Did you add any code to it?
Try these small steps before doing the loop
Create a Class called MyThing
Add a method called DrawInto (g as graphics)
g.drawstring "Hello",0, 40 //just something simple… could be ‘draw a round rectangle’
The canvas paint event:
'Sub Paint (g as graphics)
dim t as new MyThing
Can I ask, what is your goal? Are you trying to get something to display on a canvas? Or are you trying to put a control on the window that the user can interact with?
RoundRectangle is a control you can place on the window. It has events and can respond to the keyboard and mouse. Did you intend for your ProtoRoundRectangle to have those features, in addition to some custom display? Or do you just want a class that can display itself on the screen in some way?
Tim, you are exactly right. I try to display several instances of a rectangle class on the window, so that the user can interact with it. So far, I did not manage to make the class visible on the screen, apart from Jeff’s method to paint into the class from a canvas… But then again, I did not have enough time to explore all Jeff wrote, as I first have to work on something different…
You cannot add a control out of nothing. There are 2 ways to accomplish what you want to do.
Control Sets. Add one instance of your subclassed control to the window and make it a control set. I think that’s under the gear icon in the inspector. Eg., drag your subclass control onto the window and name it ProtoRectangle. It’s super will still be ProtoRoundRectangle. Make it into a Control Set. You can set it off-window or make it not Visible if you like. Now in your code you can
dim pr as New ProtoRectangle // not ProtoRoundRectangle
pr.Top = 10
pr.Left = 20
pr.Visible = True
The new control will share event code with the original that you dragged onto the window. They are differentiated by an Index parameter that is automagically added to all the event signatures.
ContainerControl. You can create a container control, drop your ProtoRoundRectangle into it, and create them on the fly and use EmbedWithin to put it on the window. This doesn’t require an initial control being on the window like a control set does.
I personally find control sets to be easier to deal with, but YMMV.
Thanks Tim, that sounds very good. Strange, you can’t add a class by code to the screen… But I will try these two methods tomorrow. I have been fiddling with containercontrol earlier, but was not successful so far.
There’s a lot of code the compiler creates behind the scenes when you put a control on a window. That’s why the first control of a control set has to be there. A ContainerControl is a subclass of window, so that code is created in the container. That’s why you can’t just create a new control and put it on a window; the glue code won’t be there.