Addressing several objects on a form in 1 line of code

Post Reply
annisu
Posts: 1
Joined: Wednesday 15th January 2020 5:01am

Addressing several objects on a form in 1 line of code

Post by annisu » Wednesday 15th January 2020 5:11am

Greetings!
I have this code:
Separator1.Foreground = Color.Green
Separator2.Foreground = Color.Green
Separator3.Foreground = Color.Green
And so on. There are 15 of those separators. This looks excessive and ugly.

If it were VB5, I'd solve this problem easily:
Separator$.Foreground = Color.Green
But sadly this doesn't seem to work on Gambas...

Is there a way to call all the objects on the form with the same name but a different number?

Your help is much appreciated.

User avatar
stevedee
Posts: 183
Joined: Monday 20th March 2017 6:06pm

Re: Addressing several objects on a form in 1 line of code

Post by stevedee » Wednesday 15th January 2020 10:16am

annisu wrote:
Wednesday 15th January 2020 5:11am
...Is there a way to call all the objects on the form with the same name but a different number?
Yes, you probably need to use a Control Group.

Take a look at Quin's answer in this thread:-
viewtopic.php?t=664


Edit: I just tried this code based on Quin's:-
Public Sub Button1_Click()
Dim mycontrol As Control
  For Each mycontrol In Me.Children
    If mycontrol Is Separator Then Object.SetProperty(mycontrol, "BackGround", Color.green)
  Next

End
...which works fine. However, I couldn't see any change when I tried to set the foreground, but that may be a separate problem.

User avatar
cogier
Site Admin
Posts: 395
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Addressing several objects on a form in 1 line of code

Post by cogier » Wednesday 15th January 2020 4:56pm

Hi annisu and welcome to the forum and you are the 100th member "ta da!!"

I'm with Steve on this. I can't change the Foreground but the code works fine for the Background. I think you can simplify one line though to: -
If mycontrol Is Separator Then mycontrol.BackGround = Color.green

User avatar
Quincunxian
Posts: 68
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: Addressing several objects on a form in 1 line of code

Post by Quincunxian » Wednesday 15th January 2020 11:40pm

I think the reason that this is not working is simply that it is not implemented in QT
Gambas sometimes has a property (Foreground) listed in the panel for a control (Separator) that has no effect.
I've come up against this a few times.
If there is no 'hook' in the underlying QT object for the property then neither setting it manually or using SetObject will work.

Also: In my example that Stevedee linked to there is an error :oops:
The line to control elements of a Lable should read TextLable as below.
 If ControlElement Is TextLabel Then Object.SetProperty(ControlElement, "Foreground", InColour)
Cheers - Quin.
I code therefore I am

User avatar
Cedron
Posts: 104
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State
Contact:

Re: Addressing several objects on a form in 1 line of code

Post by Cedron » Thursday 16th January 2020 2:57pm

Call me old fashioned, but to me the simplest solution is to shove all those lines off to a Sub or Gosub and then you have one calling line.
.
.
        SetSeparatorColors( Color.Green )
.
.
'========================================================
Public Sub SetSeparatorColors( ArgColor as Integer )

	
        Separator1.Foreground = ArgColor
        Separator2.Foreground = ArgColor
        Separator3.Foreground = ArgColor

End
'========================================================
.... and carry a big stick!

Post Reply