Page 1 of 1

Addressing several objects on a form in 1 line of code

Posted: Wednesday 15th January 2020 5:11am
by annisu
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.

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

Posted: Wednesday 15th January 2020 10:16am
by stevedee
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.

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

Posted: Wednesday 15th January 2020 4:56pm
by cogier
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

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

Posted: Wednesday 15th January 2020 11:40pm
by Quincunxian
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)

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

Posted: Thursday 16th January 2020 2:57pm
by Cedron
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
'========================================================

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

Posted: Tuesday 04th February 2020 1:50am
by grayghost4
it appears that foreground color does not work properly.

viewtopic.php?f=4&t=650&sid=6fc41c5bb40 ... 8aaac43ad7

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

Posted: Tuesday 04th February 2020 3:44am
by cage
I have noticed this too with the newer updates. Since QT4 is no longer used in the newer versions of Linux support for QT4 is no longer supported in Gamabs. For a while I was using the QT4 to Qt5 switcher and it worked fine. How ever lately I had to go to using GTK3 in order to change the foreground and background colors. When Ubuntu 20.04 comes out QT4 will be no longer be included only QT5 and GTK3.

Personally I use Arch and the latest update to Gambas QT4 is no longer part of Gambas. Currently it's Gambas 3.14.3. Another problem is some older programs that used strictly QT4 will no longer work with the latest version of Gambas. So my suggestion is to base your programs on GTK and not QT.

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

Posted: Tuesday 04th February 2020 5:13am
by grayghost4
I am a new gambas user ... I changed my program to GTk and had to change a few things ... but the printer no longer prints ? I just get a blank page.

So it is back to QT for now at least

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

Posted: Thursday 13th February 2020 7:43pm
by grayghost4
grayghost4 wrote:
Tuesday 04th February 2020 5:13am
I am a new gambas user ... I changed my program to GTk and had to change a few things ... but the printer no longer prints ? I just get a blank page.

So it is back to QT for now at least
So I tried again and the reason the print was a blank page is because the printing coradanates are different
QT using 1200 dpi on my printer ... But GTK uses 70 dpi on the same printer so everything was printing off of the page

Why would they uses such different values for the same printer ???