Adding controls

Hi gang,

I was going to ask a question about adding controls at runtime but, as I usually do, I ran a search first and found an article from 2022 re adding controls to a container and adding events, methods etc. This was very good and I created and the project and, surprisingly understood most of it. And I learnt a lot of stuff I didn’t know.
However, what I want to do is have a Tab Control that I can dynamically add panels to (this I can do). But each panel will then have either a text area control or a list box (depending on certain factors).
I understood the project above and how you add controls to a single container control but, can all of that be used in some way to add to the panels of a tab control given that those panels won’t necessarily exist until I create them.

As always,
Thanks :slight_smile:

Once you’ve added your tab or panel you add the control in the normal way then set the Panel Index to the index of the panel you created.

Hi Wayne,
Sorry, I don’t follow. If I use the commands
tabXYZ.AddPanel
tabXYZ.SelectedPanelIndex = x
that will add a panel to the tab controls and set it to index x.

What are the commands to now add a ListBox (or TextArea) to that panel? Remember this is before runtime.

Use AddControl to add the controls. Use AddHandler to connect their events.

Hi Tim,
Thanks, but am I right though in saying that AddControl is a method of the DesktopWindow which means using it would add the control to the Window? I need to add it to a Panel (freshly created) on a Tab Control.

Maybe it can’t be done. I realise not everything can be achieved, sadly. What I might have to do is create a tab with a number of predefined panels and only make them visible as needed. Each one could already have a ListBox and a TextArea on the panel and then just also make the appropriate one of those visible, again as needed.

Which then leads me to my next dilemma. If the above is the only way to achieve it, then can the individual panels’ visibilities be turned on and off?

You set the control’s Parent to the TabPanel and its PanelIndex to the panel you want it on. I don’t know if that has to be done before or after AddControl.

Hi Tim,
Yeah, so that “almost” works. For the sake of the argument here, I’ve tried adding a Label. I’ve got:

tabXYZ.AddPanel(“Label”)
var testLabel As New DesktopLabel
tabXYZ.SelectedPanelIndex = tabXYZ.LastAddedPanelIndex
winMain.AddControl(testLabel)
testLabel.Parent = tabXYZ
testLabel.Left = 10
testLabel.Top = 10
testLabel.Width = 100
testLabel.Height = 20
testLabel.Text = “TEXT”
testLabel.Visible = True

With this code, it doesn’t appear anywhere.

I’ve tried these variations:
testLabel.Parent = tabXYZ.SelectedPanelIndex
and
testLabel.Parent = tabXYZ.LastPanelIndex

Both of these give an error that it’s looking for an object and I’ve given an integer.

You are setting testLabel.Parent, but you’re not setting testLabel.PanelIndex = tabXYZ.SelectedPanelIndex.

Hi Tim,
Thanks for that. Yes it’s “fixed” the problem.

It’s interesting how much you have to set on the control though from scratch. Once I had the label appearing, I changed the control to something interactive - a text field, and it initially appeared as a black “rectangle”. I then realised I had to set its background colour to white. I’ll just have to work out how much I have to set for each type of control from scratch.

Anyway, it’s all fun (when it’s not a commercial app as many of you guys are creating :slight_smile: ).

Next issue, is how I interact with the controls once they are on the screen. IE, what I call them programmatically etc. and how I interact with their events.

Back to it :slight_smile:

Thanks (again) for everybody’s help

Barry

Did you look at using a Control Set? That could make things a little easier. You would have to have the first one on the window, but can create the rest in code. The advantages would be that they share the properties you set in the IDE for the first one. They share events, where they are identified by their Index property. You could set the index to match the tabpanel index. You would refer to them like ListBox1(3).visible = true, where “3” is the Index value.

Alternately, you could write a method that takes the tabpanel and tabpanelindex and searches through all the controls on the window and returns the matching one.

Hi Tim,
Yeah, I originally looked at an exercise using a Control set. I understood most of it. Now that I know what I have so far, I’ll go back and have another look at how to put all this together some way. Like everything, there’s often multiple ways to achieve something, it’s just coming up with one I understand. I’ll get there.

Thanks for your help

All of the properties on a control default to the intrinsic value defaults. Integers = 0, Strings = “”, Booleans = False, Objects = Nil. When you drag a control onto a layout in the IDE, it’s the IDE that provides the defaults, and they are stored with that particular instance of the control. Creating a control from scratch does not go through any of those mechanisms.

1 Like